Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
-
date post
21-Dec-2015 -
Category
Documents
-
view
224 -
download
1
Transcript of Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
![Page 1: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/1.jpg)
Chapter 3: Sorting and Searching Algorithms
3.2 Simple Sort: O(n2)
![Page 2: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/2.jpg)
2
Sorting means . . .
• Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
![Page 3: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/3.jpg)
3
Sorting
Putting collections of things in order– Numerical order– Alphabetical order– An order defined by the domain of use
• E.g. color, …
![Page 4: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/4.jpg)
4
Stable Sort
• A concept that applies to sorting in general• What to do when two items have equal keys?• A sorting algorithm is stable if
– Two items A and B, both with the same key K, end up in the same order before sorting as after sorting
• Example: sorting in excel– Given name, salary, department– Fred, Sally, and Dekai: dept == shoes, salary == 50K– Juan and Donna: dept == shoes, salary == 70K– In a stable sort:
• If you first sort by salary, then by dept, the order of Fred, Sally, and Dekai doesn’t change relative to each other
• Same goes for Juan and Donna
![Page 5: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/5.jpg)
5
Stable Sort
Sort by salary
Order of names within category doesn’t change
![Page 6: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/6.jpg)
6
Simple Sorting AlgorithmsO(n2)
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
All these have computing time O(n2)
All these have computing time O(n2)
![Page 7: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/7.jpg)
7
Divides the array into two parts: already sorted, and not yet sorted.
On each pass, finds the smallest of the unsorted elements, and swaps it into its correct place, thereby increasing the number of sorted elements by one.
1. Selection Sort
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
23
78
45
8
32
56
![Page 8: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/8.jpg)
8
Selection Sort
K
1. Swap smallest element with element # k2. Move the wall one element forward
![Page 9: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/9.jpg)
9
Example of selection sort
![Page 10: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/10.jpg)
10
Example of selection sort
![Page 11: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/11.jpg)
11
Selection sort algorithm
![Page 12: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/12.jpg)
12
Selection Sort: How many comparisons?
23
78
45
8
32
56
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
5 compares for pass[1]
4 compares for pass [2]
3 compares for pass [3]
2 compares for pass [4]
1 compare for pass [5]
= 5 + 4 + 3 + 2 + 1
![Page 13: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/13.jpg)
13
For selection sort in general
• The number of comparisons when the array contains N elements is
Sum = (N-1) + (N-2) + . . . + 2 + 1
1( 1)
21
NN N
i
Sum i
(arithmetic series)
O(N2)
![Page 14: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/14.jpg)
14
/*SelectionSort Algorithm */ void SelectSort(Array A){
int k, x;for( k = 0; k < NElements; k++ ) {
x = GetMinPosition(A,k, NElements-1);Swap(A[x], A[k]);
}}
Swap (array A){ int temp = A(K);A(k) = A(x);A(x)= temp;
==========================================================
/*GetMaxPosition Method finds the element with the greatest value and returns its position */ int GetMinPosition (Array A, int i0, int i1){
int m = A[i0], x = i0;for( int i = i0 + 1; i <= i1; i++) {
if ( A[i] < m ) {m = A[i];x = i;
}}return x;
}
Selection Sort
![Page 15: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/15.jpg)
15
Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order.
On each pass, this causes the smallest element to “bubble up” to its correct place in the array.
2. Bubble Sort
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
23
78
45
8
32
56
![Page 16: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/16.jpg)
16
Bubble sort
In each pass:
1. Compare the first to the second, the second to the third, and so on.
2. Do swap to keep smallest of each compared pair to left
![Page 17: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/17.jpg)
17
Example of bubble sort
![Page 18: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/18.jpg)
18
Example of bubble sort
Requires at least n-1 passes
![Page 19: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/19.jpg)
19
Worst-case analysis: N+N-1+ …+1= O(N2)
Bubble-Sort (Array a, int n) for (pass=0; pass<n-1; pass++) {
for (j=n-2; j>=pass; j--) { if (a[j+1] < a[j]) {//compare the two neighbors
tmp = a[j]; // swap a[j] and a[j+1]a[j] = a[j+1]; a[j+1] = tmp;
} }
} }
Bubble Sort Analysis
if (a[j+1] < a[j])
Swap (a[j+1] , a[j]) ;
![Page 20: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/20.jpg)
21
One by one, each as yet unsorted array element is inserted into its proper place with respect to the already sorted elements.
On each pass, this causes the number of already sorted elements to increase by one.
3. Insertion Sort
23
78
45
8
32
56
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
![Page 21: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/21.jpg)
22
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 24
12
36
![Page 22: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/22.jpg)
23
6 10 24
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
36
12
![Page 23: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/23.jpg)
24
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 24 36
12
![Page 24: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/24.jpg)
25
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 12 24 36
![Page 25: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/25.jpg)
26
Insertion sort
![Page 26: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/26.jpg)
27
Example of insertion sort
![Page 27: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/27.jpg)
28
Example of insertion sort
![Page 28: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/28.jpg)
29
Insertion sort
1) Initially p = 1
2) Let the first p elements be sorted.
3) Insert the (p+1)st element properly in the list (go inversely from right to left) so that now p+1 elements are sorted.
4) increment p and go to step (3)
![Page 29: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/29.jpg)
30
Insertion sort
-
--
-
-
-
Inner loop is executed p times (pm shifts in worst Inner loop is executed p times (pm shifts in worst case), for each p=1..N case), for each p=1..N Overall: 1 + 2 + 3 + . . . + Overall: 1 + 2 + 3 + . . . +
N = O(NN = O(N22))
![Page 30: Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d575503460f94a35bb5/html5/thumbnails/30.jpg)
31
Insertion Sort: A program class Insertion_Sort { static void InsertionSort(int[] A, int n) { for (int i = 1; i < n; i++) { int v = A[i]; int j = i-1; while (j >= 0 && A[j] > v) { A[j + 1] = A[j]; j--; } A[j + 1] = v; } } }