1 / 30

Chapter 3: Sorting and Searching Algorithms

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

cheryl
Télécharger la présentation

Chapter 3: Sorting and Searching Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) • Selection Sort • Bubble Sort • Insertion Sort All these have computing time O(n2)

  7. 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.

  8. Selection Sort K • Swap smallest element with element # k • Move the wall one element forward

  9. Example of selection sort

  10. Example of selection sort

  11. Selection sort algorithm

  12. 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

  13. 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)

  14. 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; }

  15. 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.

  16. 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

  17. Example of bubble sort

  18. Example of bubble sort Requires at least n-1 passes

  19. 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]) ;

  20. 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.

  21. 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

  22. 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

  23. 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. 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

  25. Insertion sort

  26. Example of insertion sort

  27. Example of insertion sort

  28. 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)

  29. 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)

  30. 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; } } }

More Related