chapter 3: sorting and searching algorithms 3.2 simple sort: o(n 2 )
Post on 21-Dec-2015
224 views
TRANSCRIPT
Chapter 3: Sorting and Searching Algorithms
3.2 Simple Sort: O(n2)
2
Sorting means . . .
• Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
3
Sorting
Putting collections of things in order– Numerical order– Alphabetical order– An order defined by the domain of use
• E.g. color, …
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
5
Stable Sort
Sort by salary
Order of names within category doesn’t change
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)
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
8
Selection Sort
K
1. Swap smallest element with element # k2. Move the wall one element forward
9
Example of selection sort
10
Example of selection sort
11
Selection sort algorithm
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
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)
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
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
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
17
Example of bubble sort
18
Example of bubble sort
Requires at least n-1 passes
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]) ;
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 ]
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
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
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
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
26
Insertion sort
27
Example of insertion sort
28
Example of insertion sort
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)
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))
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; } } }