cop 3503 fall 2012 shayan javed lecture 16

137
1/ 137 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16 Programming Fundamentals using Java 1

Upload: brina

Post on 25-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 16. Programming Fundamentals using Java. Sorting. Another crucial problem. Used everywhere: Sorting numbers (prices/grades/ratings/etc..) Names Dates Rating. Sorting. When shopping online (Amazon for ex.):. Sorting. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 16

1/ 137

1

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 16

Programming Fundamentals using Java

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 16

2/ 137

Sorting

Another crucial problem.

Used everywhere: Sorting numbers (prices/grades/ratings/etc..) Names Dates Rating

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 16

3/ 137

Sorting

When shopping online (Amazon for ex.):

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 16

4/ 137

Sorting

We designed a RedBox system where you can sort by different criteria

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 16

5/ 137

Sorting

We designed a RedBox system where you can sort by different criteria

How can you sort in Java?

Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 16

6/ 137

Sorting

We designed a RedBox system where you can sort by different criteria

How can you sort in Java? Arrays.sort(..) – but how does it work?

Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 16

7/ 137

Sorting

We designed a RedBox system where you can sort by different criteria

How can you sort in Java? Arrays.sort(..) – but how does it work?

Going to look at a few different algorithms.

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 16

8/ 137

Sorting

Let’s see if we can come up with one on our own...

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 16

9/ 137

Sorting

Let’s see if we can come up with one on our own... array A (N = A.length):

sorted array A:

i 0 1 2 3 4 5

A 19 33 45 55 87 100

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 16

10/ 137

Sorting

Compare values at indices 0 and 1 first.

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 16

11/ 137

Sorting

Compare values at indices 0 and 1 first.

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 16

12/ 137

Sorting

Compare values at indices 0 and 1 first.

Swap if A[1] < A[0]:

i 0 1 2 3 4 5

A 55 19 100 45 87 33

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 16

13/ 137

Sorting

Compare values at indices 1 and 2.

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 16

14/ 137

Sorting

Compare values at indices 1 and 2.

Is A[2] < A[1]? No:

i 0 1 2 3 4 5

A 19 55 100 45 87 33

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 16

15/ 137

Sorting

Compare values at indices 2 and 3.

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 16

16/ 137

Sorting

Compare values at indices 2 and 3.

Is A[3] < A[2]? Yes! Swap.

i 0 1 2 3 4 5

A 19 55 100 45 87 33

i 0 1 2 3 4 5

A 19 55 45 100 87 33

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 16

17/ 137

Sorting

Keep repeating this process until you reach the end of the array.

Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 16

18/ 137

Sorting

Keep repeating this process until you reach the end of the array.

Result:

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 16

19/ 137

Sorting

Largest value in the array is now at the end of the array.

Result:

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 16

20/ 137

Sorting

What do we do next?

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 16

21/ 137

Sorting

What do we do next? Repeat the process.

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 16

22/ 137

Sorting

What do we do next? Repeat the process.

Result:

i 0 1 2 3 4 5

A 19 45 55 33 87 100

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 16

23/ 137

Sorting

Have the second largest value at index N - 2

Result:

i 0 1 2 3 4 5

A 19 45 55 33 87 100

Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 16

24/ 137

Sorting

Have the second largest value at index N - 2 How many more times to sort?

Result:

i 0 1 2 3 4 5

A 19 45 55 33 87 100

Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 16

25/ 137

Sorting

Have the second largest value at index N - 2 How many more times to sort? Total of N times

Result:

i 0 1 2 3 4 5

A 19 45 55 33 87 100

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 16

26/ 137

Sorting

Have the second largest value at index N - 2 How many more times to sort? Total of N times

Result:

i 0 1 2 3 4 5

A 19 55 45 33 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 16

27/ 137

Bubble Sort

This algorithm is known as “Bubble Sort”

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 16

28/ 137

Bubble Sort

This algorithm is known as “Bubble Sort”

The max value “bubbles” to the end of the list at each pass.

Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 16

29/ 137

Bubble Sort

This algorithm is known as “Bubble Sort”

The max value “bubbles” to the end of the list at each pass.

Simplest sorting algorithm.

Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 16

30/ 137

Bubble Sort Let’s write the code for it:

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 16

31/ 137

Bubble Sort Let’s write the code for it:static void bubbleSort(int[] A, int N) {

for(int i = 0; i < N; i++) {for (int j = 1; j < N; j++) {

if (A[j] < A[j-1]) {// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 16

32/ 137

Bubble Sort Let’s write the code for it:static void bubbleSort(int[] A, int N) {

for(int i = 0; i < N; i++) {for (int j = 1; j < N; j++) {

if (A[j] < A[j-1]) {// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 16

33/ 137

Bubble Sort Let’s write the code for it:static void bubbleSort(int[] A, int N) {

for(int i = 0; i < N; i++) {for (int j = 1; j < N; j++) {

if (A[j] < A[j-1]) {// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 16

34/ 137

Bubble Sort Let’s write the code for it:static void bubbleSort(int[] A, int N) {

for(int i = 0; i < N; i++) {for (int j = 1; j < N; j++) {

if (A[j] < A[j-1]) {// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 16

35/ 137

Bubble Sort Let’s write the code for it:static void bubbleSort(int[] A, int N) {

for(int i = 0; i < N; i++) {for (int j = 1; j < N; j++) {

if (A[j] < A[j-1]) {// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 16

36/ 137

Bubble Sort Efficiency

How efficient is it?

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 16

37/ 137

Bubble Sort Efficiency

How efficient is it?

What is the O(..) of the algorithm?

Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 16

38/ 137

Bubble Sort Efficiency

Let’s look at one pass.

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 16

39/ 137

Bubble Sort Efficiency

Let’s look at one pass.i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 16

40/ 137

Bubble Sort Efficiency

Let’s look at one pass.

To:

i 0 1 2 3 4 5

A 55 19 100 45 87 33

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 16

41/ 137

Bubble Sort Efficiency

Let’s look at one pass.

To:

What’s the efficiency of this pass?

i 0 1 2 3 4 5

A 55 19 100 45 87 33

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 16

42/ 137

Bubble Sort Efficiency

Let’s look at one pass.

To:

What’s the efficiency of this pass? O(N)

i 0 1 2 3 4 5

A 55 19 100 45 87 33

i 0 1 2 3 4 5

A 19 55 45 87 33 100

Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 16

43/ 137

Bubble Sort Efficiency

How many of these passes do we have?

Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 16

44/ 137

Bubble Sort Efficiency

How many of these passes do we have?

N passes.

Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 16

45/ 137

Bubble Sort Efficiency

How many of these passes do we have?

N passes.

Efficiency: O(N) * N = O(N2)

Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 16

46/ 137

Bubble Sort Efficiency

How can we make the algorithm a little more efficient/faster?

Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 16

47/ 137

Bubble Sort Efficiency

static void bubbleSort(int[] A, int N) {for(int i = 0; i < N; i++) {

for (int j = 1; j < (N-i); j++) {if (A[j] < A[j-1]) {

// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 16

48/ 137

Bubble Sort Efficiency

static void bubbleSort(int[] A, int N) {for(int i = 0; i < N; i++) {

for (int j = 1; j < (N-i); j++) {if (A[j] < A[j-1]) {

// swapint temp = A[j];A[j] = A[j-1];A[j-1] = temp;

}}

}}

We know the end keeps getting sorted properly

Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 16

49/ 137

Bubble Sort Efficiency

But efficiency is still O(N2)

Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 16

50/ 137

Bubble Sort Efficiency

But efficiency is still O(N2)

Thus bubble sort isn’t really a good sorting algorithm...

Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 16

51/ 137

Bubble Sort Efficiency

But efficiency is still O(N2)

Thus bubble sort isn’t really a good sorting algorithm...

Going to look at other alternatives.

Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 16

52/ 137

Find the minimum in an array

How will you find the minimum number in an array?

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 16

53/ 137

Find the minimum in an array

min = A[0]

for i = 1 to A.length – 1:if A[i] < min:

min = A[i]

Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 16

54/ 137

Find the minimum in an array

What can we do with the minimum value?

Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 16

55/ 137

Find the minimum in an array

What can we do with the minimum value?

Swap it with the value at index 0

Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 16

56/ 137

Find the minimum in an array

What can we do with the minimum value?

Swap it with the value at index 0

Repeat for the rest of the indices...

Page 57: COP 3503 FALL 2012 Shayan Javed Lecture 16

57/ 137

Find the minimum in an array

What can we do with the minimum value?

Swap it with the value at index 0

Repeat for the rest of the indices...

Result: Sorted array!

Page 58: COP 3503 FALL 2012 Shayan Javed Lecture 16

58/ 137

Selection Sort

static void selectionSort(int[] A) {for (int i = 0; i < A.length; i++) {

int min = i; // index of minimum valuefor(int j = i; j <= A.length; j++) {

if ( A[j] < A[min])min = j;

}// swap if (j != i)

swap(A, i, j); }

}

Page 59: COP 3503 FALL 2012 Shayan Javed Lecture 16

59/ 137

Selection Sort

static void selectionSort(int[] A) {for (int i = 0; i < A.length - 1; i++) {

int min = i; // index of minimum valuefor(int j = i; j <= A.length; j++) {

if ( A[j] < A[min])min = j;

}// swap if (j != i)

swap(A, i, j); }

}

Page 60: COP 3503 FALL 2012 Shayan Javed Lecture 16

60/ 137

Selection Sort

static void selectionSort(int[] A) {for (int i = 0; i < A.length - 1; i++) {

int min = i; // index of minimum valuefor(int j = i; j <= A.length; j++) {

if ( A[j] < A[min])min = j;

}// swap if (j != i)

swap(A, i, j); }

}

Page 61: COP 3503 FALL 2012 Shayan Javed Lecture 16

61/ 137

Selection Sort

static void selectionSort(int[] A) {for (int i = 0; i < A.length - 1; i++) {

int min = i; // index of minimum valuefor(int j = i + 1; j < A.length; j++) {

if ( A[j] < A[min])min = j;

}// swap if (j != i)

swap(A, i, j); }

}

Page 62: COP 3503 FALL 2012 Shayan Javed Lecture 16

62/ 137

Selection Sort

static void selectionSort(int[] A) {for (int i = 0; i < A.length - 1; i++) {

int min = i; // index of minimum valuefor(int j = i + 1; j < A.length; j++) {

if ( A[j] < A[min])min = j;

}// swap if (j != i)

swap(A, i, j); }

}

Page 63: COP 3503 FALL 2012 Shayan Javed Lecture 16

63/ 137

Selection Sort

static void selectionSort(int[] A) {for (int i = 0; i < A.length - 1; i++) {

int min = i; // index of minimum valuefor(int j = i + 1; j < A.length; j++) {

if ( A[j] < A[min])min = j;

}// swap if (min != i)

swap(A, i, min); }

}

Page 64: COP 3503 FALL 2012 Shayan Javed Lecture 16

64/ 137

Selection Sort Efficiency

O(N2)

Page 65: COP 3503 FALL 2012 Shayan Javed Lecture 16

65/ 137

Selection Sort Efficiency

O(N2)

Better than Bubble Sort

Page 66: COP 3503 FALL 2012 Shayan Javed Lecture 16

66/ 137

Selection Sort Efficiency

O(N2)

Better than Bubble Sort

But rarely used

Page 67: COP 3503 FALL 2012 Shayan Javed Lecture 16

67/ 137

Insertion Sort

Concept:

Go through every element, insert it in it’s correct position in the sub-array before it

Page 68: COP 3503 FALL 2012 Shayan Javed Lecture 16

68/ 137

Insertion Sort

array A (N = A.length):

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 69: COP 3503 FALL 2012 Shayan Javed Lecture 16

69/ 137

Insertion Sort

array A (N = A.length): Value = 55

Look at sub-array between indices 0 and 0

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 70: COP 3503 FALL 2012 Shayan Javed Lecture 16

70/ 137

Insertion Sort

array A (N = A.length): Value = 55

Look at sub-array between indices 0 and 0

Already sorted, let’s look at next index

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 71: COP 3503 FALL 2012 Shayan Javed Lecture 16

71/ 137

Insertion Sort

array A (N = A.length): Value = 19

Look at sub-array between indices 0 and 1

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 72: COP 3503 FALL 2012 Shayan Javed Lecture 16

72/ 137

Insertion Sort

array A (N = A.length): Value = 19

Look at sub-array between indices 0 and 1

Is Value < A[0]? Yes!

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 73: COP 3503 FALL 2012 Shayan Javed Lecture 16

73/ 137

Insertion Sort

array A (N = A.length): Value = 19

Look at sub-array between indices 0 and 1

Is Value < A[0]? Yes! Move 55 forward by 1 index.

i 0 1 2 3 4 5

A 55 55 100 45 87 33

Page 74: COP 3503 FALL 2012 Shayan Javed Lecture 16

74/ 137

Insertion Sort

array A (N = A.length): Value = 19

Look at sub-array between indices 0 and 1

Is Value < A[0]? Yes! Move 55 forward by 1 index. Put 19 at index 0 (reached the beginning)

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 75: COP 3503 FALL 2012 Shayan Javed Lecture 16

75/ 137

Insertion Sort

array A (N = A.length): Value = 100

Look at sub-array between indices 0 and 2

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 76: COP 3503 FALL 2012 Shayan Javed Lecture 16

76/ 137

Insertion Sort

array A (N = A.length): Value = 100

Look at sub-array between indices 0 and 2

Is Value < A[1]? No.

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 77: COP 3503 FALL 2012 Shayan Javed Lecture 16

77/ 137

Insertion Sort

array A (N = A.length): Value = 100

Look at sub-array between indices 0 and 2

Is Value < A[1]? No. Continue

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 78: COP 3503 FALL 2012 Shayan Javed Lecture 16

78/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 79: COP 3503 FALL 2012 Shayan Javed Lecture 16

79/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[2]? Yes.

i 0 1 2 3 4 5

A 19 55 100 45 87 33

Page 80: COP 3503 FALL 2012 Shayan Javed Lecture 16

80/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[2]? Yes. Move A[2] forward by one position

i 0 1 2 3 4 5

A 19 55 100 100 87 33

Page 81: COP 3503 FALL 2012 Shayan Javed Lecture 16

81/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[1]? Yes.

i 0 1 2 3 4 5

A 19 55 100 100 87 33

Page 82: COP 3503 FALL 2012 Shayan Javed Lecture 16

82/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[1]? Yes.

i 0 1 2 3 4 5

A 19 55 100 100 87 33

Page 83: COP 3503 FALL 2012 Shayan Javed Lecture 16

83/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[1]? Yes. Move A[1] forward by one position

i 0 1 2 3 4 5

A 19 55 55 100 87 33

Page 84: COP 3503 FALL 2012 Shayan Javed Lecture 16

84/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[0]? No.

i 0 1 2 3 4 5

A 19 55 55 100 87 33

Page 85: COP 3503 FALL 2012 Shayan Javed Lecture 16

85/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[0]? No.

i 0 1 2 3 4 5

A 19 55 55 100 87 33

Page 86: COP 3503 FALL 2012 Shayan Javed Lecture 16

86/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[0]? No. STOP!

i 0 1 2 3 4 5

A 19 55 55 100 87 33

Page 87: COP 3503 FALL 2012 Shayan Javed Lecture 16

87/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Is Value < A[0]? No. STOP! Put Value at index 1 (where we stopped)

i 0 1 2 3 4 5

A 19 45 55 100 87 33

Page 88: COP 3503 FALL 2012 Shayan Javed Lecture 16

88/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Note how this sub-array is sorted.

i 0 1 2 3 4 5

A 19 45 55 100 87 33

Page 89: COP 3503 FALL 2012 Shayan Javed Lecture 16

89/ 137

Insertion Sort

array A (N = A.length): Value = 45

Look at sub-array between indices 0 and 3 Note how this sub-array is sorted. Repeat for Value = 87

& sub-array between 0 and 4

i 0 1 2 3 4 5

A 19 45 55 100 87 33

Page 90: COP 3503 FALL 2012 Shayan Javed Lecture 16

90/ 137

Insertion Sort

Exercise: Write the code by yourselves.

Page 91: COP 3503 FALL 2012 Shayan Javed Lecture 16

91/ 137

Insertion Sort

Exercise: Write the code by yourselves.

(Without looking it up online of course...) Try to challenge yourself

Page 92: COP 3503 FALL 2012 Shayan Javed Lecture 16

92/ 137

Insertion Sort Efficiency

Also O(N2)

Page 93: COP 3503 FALL 2012 Shayan Javed Lecture 16

93/ 137

Insertion Sort Efficiency

Also O(N2)

But:

Page 94: COP 3503 FALL 2012 Shayan Javed Lecture 16

94/ 137

Insertion Sort Efficiency

Also O(N2)

But: Very fast for sorted/partially sorted arrays.

Page 95: COP 3503 FALL 2012 Shayan Javed Lecture 16

95/ 137

Insertion Sort Efficiency

Also O(N2)

But: Very fast for sorted/partially sorted arrays. Efficient for small data sets

Page 96: COP 3503 FALL 2012 Shayan Javed Lecture 16

96/ 137

Insertion Sort Efficiency

Also O(N2)

But: Very fast for sorted/partially sorted arrays. Efficient for small data sets Online: Sort a list as it receives input

Page 97: COP 3503 FALL 2012 Shayan Javed Lecture 16

97/ 137

Insertion Sort Efficiency

Also O(N2)

But: Very fast for sorted/partially sorted arrays. Efficient for small data sets Online: Sort a list as it receives input Better than Bubble and Selection Sort

Page 98: COP 3503 FALL 2012 Shayan Javed Lecture 16

98/ 137

Sorting

None of the algorithms seen so far are “good enough”

Page 99: COP 3503 FALL 2012 Shayan Javed Lecture 16

99/ 137

Sorting

None of the algorithms seen so far are “good enough”

Especially Bubble and Selection

Page 100: COP 3503 FALL 2012 Shayan Javed Lecture 16

100/ 137

Sorting

None of the algorithms seen so far are “good enough”

Especially Bubble and Selection

Merge Sort and QuickSort are the best algorithms

Page 101: COP 3503 FALL 2012 Shayan Javed Lecture 16

101/ 137

Sorting

None of the algorithms seen so far are “good enough”

Especially Bubble and Selection

Merge Sort and QuickSort are the best algorithms Going to look at Merge Sort

Page 102: COP 3503 FALL 2012 Shayan Javed Lecture 16

102/ 137

Merge Sort

Concept: Divide a list equally into two

Page 103: COP 3503 FALL 2012 Shayan Javed Lecture 16

103/ 137

Merge Sort

Concept: Divide a list equally into two

Sort the two lists

Page 104: COP 3503 FALL 2012 Shayan Javed Lecture 16

104/ 137

Merge Sort

Concept: Divide a list equally into two

Sort the two lists

Merge them

Page 105: COP 3503 FALL 2012 Shayan Javed Lecture 16

105/ 137

Merge Sort

Concept: Divide a list equally into two

Sort the two lists

Merge them

Repeat process recursively

Page 106: COP 3503 FALL 2012 Shayan Javed Lecture 16

106/ 137

Merge Sort

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 107: COP 3503 FALL 2012 Shayan Javed Lecture 16

107/ 137

Merge Sort

Split

i 0 1 2 3 4 5

A 55 19 100 45 87 33

0 1 2

55 19 100

3 4 5

45 87 33

Page 108: COP 3503 FALL 2012 Shayan Javed Lecture 16

108/ 137

Merge Sort

Split

i 0 1 2 3 4 5

A 55 19 100 45 87 33

0 1 2

55 19 100

3 4 5

45 87 33

0

55

1 2

19 100

Page 109: COP 3503 FALL 2012 Shayan Javed Lecture 16

109/ 137

Merge Sort

Split

i 0 1 2 3 4 5

A 55 19 100 45 87 33

0 1 2

55 19 100

3 4 5

45 87 33

0

55

1 2

19 100

3

45

4 5

87 33

Page 110: COP 3503 FALL 2012 Shayan Javed Lecture 16

110/ 137

Merge Sort

Split

Now Sort and Merge

i 0 1 2 3 4 5

A 55 19 100 45 87 33

0 1 2

55 19 100

3 4 5

45 87 33

0

55

1 2

19 100

3

45

4 5

87 33

Page 111: COP 3503 FALL 2012 Shayan Javed Lecture 16

111/ 137

Merge Sort

0

55

1 2

19 100

Page 112: COP 3503 FALL 2012 Shayan Javed Lecture 16

112/ 137

Merge Sort

Already sorted Already Sorted

0

55

1 2

19 100

Page 113: COP 3503 FALL 2012 Shayan Javed Lecture 16

113/ 137

Merge Sort

Already sorted Already Sorted

Merge the two in the proper order:

0

55

1 2

19 100

Page 114: COP 3503 FALL 2012 Shayan Javed Lecture 16

114/ 137

Merge Sort

Already sorted Already Sorted

Merge the two in the proper order:

s

Sorted!

0

55

1 2

19 100

0 1 2

19 55 100

Page 115: COP 3503 FALL 2012 Shayan Javed Lecture 16

115/ 137

Merge Sort

0

55

1 2

19 100

3

45

4 5

87 33

Page 116: COP 3503 FALL 2012 Shayan Javed Lecture 16

116/ 137

Merge Sort

Already sorted Sort it:

0

55

1 2

19 100

3

45

4 5

87 33

Page 117: COP 3503 FALL 2012 Shayan Javed Lecture 16

117/ 137

Merge Sort

Already sorted Sort it:

0

55

1 2

19 100

3

45

4 5

87 33

4 5

33 87

Page 118: COP 3503 FALL 2012 Shayan Javed Lecture 16

118/ 137

Merge Sort

Already sorted Sort it:

Now Merge:

0

55

1 2

19 100

3

45

4 5

87 33

4 5

33 87

3 4 5

33 45 87

Page 119: COP 3503 FALL 2012 Shayan Javed Lecture 16

119/ 137

Merge Sort

3 4 5

33 45 87

0 1 2

19 55 100

Page 120: COP 3503 FALL 2012 Shayan Javed Lecture 16

120/ 137

Merge Sort

Both already sorted.

3 4 5

33 45 87

0 1 2

19 55 100

Page 121: COP 3503 FALL 2012 Shayan Javed Lecture 16

121/ 137

Merge Sort

Both already sorted. Merge:

3 4 5

33 45 87

0 1 2

19 55 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 122: COP 3503 FALL 2012 Shayan Javed Lecture 16

122/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 123: COP 3503 FALL 2012 Shayan Javed Lecture 16

123/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 124: COP 3503 FALL 2012 Shayan Javed Lecture 16

124/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 125: COP 3503 FALL 2012 Shayan Javed Lecture 16

125/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 126: COP 3503 FALL 2012 Shayan Javed Lecture 16

126/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 127: COP 3503 FALL 2012 Shayan Javed Lecture 16

127/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 128: COP 3503 FALL 2012 Shayan Javed Lecture 16

128/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 129: COP 3503 FALL 2012 Shayan Javed Lecture 16

129/ 137

Merge Sort

mergeSort(A[]):if size(A) <= 1:

return A// split into two listsmiddleIndex = size(A)/2left[] = A[0…middleIndex-1]right[] = A[middleIndex…size(A)-1]left = mergeSort(left)right = mergeSort(right)result[] = merge(left, right)return result

Page 130: COP 3503 FALL 2012 Shayan Javed Lecture 16

130/ 137

Merge Sort

How to merge two sorted lists?

Page 131: COP 3503 FALL 2012 Shayan Javed Lecture 16

131/ 137

Merge Sort

How to merge two sorted lists?

Figure that out yourself

Page 132: COP 3503 FALL 2012 Shayan Javed Lecture 16

132/ 137

Merge Sort

How to merge two sorted lists?

Figure that out yourself Try to to do it on paper first

Page 133: COP 3503 FALL 2012 Shayan Javed Lecture 16

133/ 137

Merge Sort

How to merge two sorted lists?

Figure that out yourself Try to to do it on paper first

Exercise: Implement Merge Sort Can it be done iteratively?

Page 134: COP 3503 FALL 2012 Shayan Javed Lecture 16

134/ 137

Merge Sort Efficiency

O(NlogN)

Page 135: COP 3503 FALL 2012 Shayan Javed Lecture 16

135/ 137

Merge Sort Efficiency

O(NlogN)

Much faster than the previous algorithms

Page 136: COP 3503 FALL 2012 Shayan Javed Lecture 16

136/ 137

Merge Sort Efficiency

O(NlogN)

Much faster than the previous algorithms

Used in java.util.Arrays.sort(…)

Page 137: COP 3503 FALL 2012 Shayan Javed Lecture 16

137/ 137

Summary

Sorting is a very important problem.

Bubble, Selection and Insertion Sort.

Insertion Sort great for small arrays.

Merge Sort much faster than others