330 likes | 576 Views
Chapter 3: Sorting and Searching Algorithms. 3.2 Simple Sort: O(n 2 ). Sorting means. Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.). Sorting. Putting collections of things in order Numerical order Alphabetical order
E N D
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n2)
Sorting means . . . • Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Sorting Putting collections of things in order • Numerical order • Alphabetical order • An order defined by the domain of use • E.g. color, …
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
Stable Sort Sort by salary Order of names within category doesn’t change
Simple Sorting AlgorithmsO(n2) • Selection Sort • Bubble Sort • Insertion Sort All these have computing time O(n2)
23 78 45 8 32 56 1. Selection Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 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.
Selection Sort K • Swap smallest element with element # k • Move the wall one element forward
Selection sort algorithm
23 78 45 8 32 56 Selection Sort: How many comparisons? 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
For selection sort in general • The number of comparisons when the array contains N elements is Sum = (N-1) + (N-2) + . . . + 2 + 1 O(N2) (arithmetic series)
Selection Sort /*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; }
23 78 45 8 32 56 2. Bubble Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 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.
Bubble sort • In each pass: • Compare the first to the second, the second to the third, and so on. • Do swap to keep smallest of each compared pair to left
Example of bubble sort Requires at least n-1 passes
Bubble Sort Analysis 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; } } } } Worst-case analysis: N+N-1+ …+1= O(N2) if (a[j+1] < a[j]) Swap (a[j+1] , a[j]) ;
23 78 45 8 32 56 3. Insertion Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 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.
24 10 6 Insertion Sort 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. 36 12
24 10 6 Insertion Sort 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. 36 12
24 36 Insertion Sort 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. 10 6 12
24 36 Insertion Sort 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. 12 10 6
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)
Insertion sort - - - - - - Inner loop is executed p times (pm shifts in worst case), for each p=1..N Overall: 1 + 2 + 3 + . . . + N = O(N2)
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; } } }