cop 3503 fall 2012 shayan javed lecture 16
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 PresentationTRANSCRIPT
1/ 137
1
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 16
Programming Fundamentals using Java
2/ 137
Sorting
Another crucial problem.
Used everywhere: Sorting numbers (prices/grades/ratings/etc..) Names Dates Rating
3/ 137
Sorting
When shopping online (Amazon for ex.):
4/ 137
Sorting
We designed a RedBox system where you can sort by different criteria
5/ 137
Sorting
We designed a RedBox system where you can sort by different criteria
How can you sort in Java?
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?
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.
8/ 137
Sorting
Let’s see if we can come up with one on our own...
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
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
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
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
13/ 137
Sorting
Compare values at indices 1 and 2.
i 0 1 2 3 4 5
A 19 55 100 45 87 33
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
15/ 137
Sorting
Compare values at indices 2 and 3.
i 0 1 2 3 4 5
A 19 55 100 45 87 33
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
17/ 137
Sorting
Keep repeating this process until you reach the end of the array.
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
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
20/ 137
Sorting
What do we do next?
i 0 1 2 3 4 5
A 19 55 45 87 33 100
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
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
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
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
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
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
27/ 137
Bubble Sort
This algorithm is known as “Bubble Sort”
28/ 137
Bubble Sort
This algorithm is known as “Bubble Sort”
The max value “bubbles” to the end of the list at each pass.
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.
30/ 137
Bubble Sort Let’s write the code for it:
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;
}}
}}
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;
}}
}}
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;
}}
}}
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;
}}
}}
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;
}}
}}
36/ 137
Bubble Sort Efficiency
How efficient is it?
37/ 137
Bubble Sort Efficiency
How efficient is it?
What is the O(..) of the algorithm?
38/ 137
Bubble Sort Efficiency
Let’s look at one pass.
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
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
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
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
43/ 137
Bubble Sort Efficiency
How many of these passes do we have?
44/ 137
Bubble Sort Efficiency
How many of these passes do we have?
N passes.
45/ 137
Bubble Sort Efficiency
How many of these passes do we have?
N passes.
Efficiency: O(N) * N = O(N2)
46/ 137
Bubble Sort Efficiency
How can we make the algorithm a little more efficient/faster?
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;
}}
}}
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
49/ 137
Bubble Sort Efficiency
But efficiency is still O(N2)
50/ 137
Bubble Sort Efficiency
But efficiency is still O(N2)
Thus bubble sort isn’t really a good sorting algorithm...
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.
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
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]
54/ 137
Find the minimum in an array
What can we do with the minimum value?
55/ 137
Find the minimum in an array
What can we do with the minimum value?
Swap it with the value at index 0
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...
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!
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); }
}
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); }
}
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); }
}
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); }
}
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); }
}
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); }
}
64/ 137
Selection Sort Efficiency
O(N2)
65/ 137
Selection Sort Efficiency
O(N2)
Better than Bubble Sort
66/ 137
Selection Sort Efficiency
O(N2)
Better than Bubble Sort
But rarely used
67/ 137
Insertion Sort
Concept:
Go through every element, insert it in it’s correct position in the sub-array before it
68/ 137
Insertion Sort
array A (N = A.length):
i 0 1 2 3 4 5
A 55 19 100 45 87 33
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
90/ 137
Insertion Sort
Exercise: Write the code by yourselves.
91/ 137
Insertion Sort
Exercise: Write the code by yourselves.
(Without looking it up online of course...) Try to challenge yourself
92/ 137
Insertion Sort Efficiency
Also O(N2)
93/ 137
Insertion Sort Efficiency
Also O(N2)
But:
94/ 137
Insertion Sort Efficiency
Also O(N2)
But: Very fast for sorted/partially sorted arrays.
95/ 137
Insertion Sort Efficiency
Also O(N2)
But: Very fast for sorted/partially sorted arrays. Efficient for small data sets
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
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
98/ 137
Sorting
None of the algorithms seen so far are “good enough”
99/ 137
Sorting
None of the algorithms seen so far are “good enough”
Especially Bubble and Selection
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
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
102/ 137
Merge Sort
Concept: Divide a list equally into two
103/ 137
Merge Sort
Concept: Divide a list equally into two
Sort the two lists
104/ 137
Merge Sort
Concept: Divide a list equally into two
Sort the two lists
Merge them
105/ 137
Merge Sort
Concept: Divide a list equally into two
Sort the two lists
Merge them
Repeat process recursively
106/ 137
Merge Sort
i 0 1 2 3 4 5
A 55 19 100 45 87 33
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
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
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
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
111/ 137
Merge Sort
0
55
1 2
19 100
112/ 137
Merge Sort
Already sorted Already Sorted
0
55
1 2
19 100
113/ 137
Merge Sort
Already sorted Already Sorted
Merge the two in the proper order:
0
55
1 2
19 100
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
115/ 137
Merge Sort
0
55
1 2
19 100
3
45
4 5
87 33
116/ 137
Merge Sort
Already sorted Sort it:
0
55
1 2
19 100
3
45
4 5
87 33
117/ 137
Merge Sort
Already sorted Sort it:
0
55
1 2
19 100
3
45
4 5
87 33
4 5
33 87
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
119/ 137
Merge Sort
3 4 5
33 45 87
0 1 2
19 55 100
120/ 137
Merge Sort
Both already sorted.
3 4 5
33 45 87
0 1 2
19 55 100
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
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
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
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
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
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
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
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
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
130/ 137
Merge Sort
How to merge two sorted lists?
131/ 137
Merge Sort
How to merge two sorted lists?
Figure that out yourself
132/ 137
Merge Sort
How to merge two sorted lists?
Figure that out yourself Try to to do it on paper first
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?
134/ 137
Merge Sort Efficiency
O(NlogN)
135/ 137
Merge Sort Efficiency
O(NlogN)
Much faster than the previous algorithms
136/ 137
Merge Sort Efficiency
O(NlogN)
Much faster than the previous algorithms
Used in java.util.Arrays.sort(…)
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