1 / 50

Data Structures and Algorithms CSE 246

Data Structures and Algorithms CSE 246. Fall-2012 Lecture#14. The Selection Sort Description.

beulah
Download Presentation

Data Structures and Algorithms CSE 246

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. Data Structures and AlgorithmsCSE 246 Fall-2012 Lecture#14 Muhammad Usman Arif

  2. The Selection SortDescription The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first element in the list. Next it finds the smallest of the remaining elements, and “swaps” it with the second element. Repeat this process until you compare only the last two elements in the list. Muhammad Usman Arif

  3. The Selection SortAlgorithm • For each index position i • Find the smallest data value in the array from positions i through length - 1, where length is the number of data values stored. • Exchange (swap) the smallest value with the value at position i. Muhammad Usman Arif

  4. A Selection Sort Example Smallest ? We start by searching for the smallest element in the List. Muhammad Usman Arif

  5. A Selection Sort Example Smallest ? Muhammad Usman Arif

  6. A Selection Sort Example Smallest ! Muhammad Usman Arif

  7. A Selection Sort Example ! Smallest ? Muhammad Usman Arif

  8. A Selection Sort Example ! Smallest ? Muhammad Usman Arif

  9. A Selection Sort Example ! Smallest ? Muhammad Usman Arif

  10. A Selection Sort Example Swap Muhammad Usman Arif

  11. A Selection Sort Example Swapped Muhammad Usman Arif

  12. A Selection Sort Example Smallest ? After the smallest element is in the first position, we continue searching with the second element and look for the next smallest element. Muhammad Usman Arif

  13. A Selection Sort Example Smallest ! In this special case, the next smallest element is in the second position already. Swapping keeps it in this position. Muhammad Usman Arif

  14. A Selection Sort Example Swapped Swapping keeps it in this position. Muhammad Usman Arif

  15. A Selection Sort Example Smallest ? After the next smallest element is in the second position, we continue searching with the third element and look for the next smallest element. Muhammad Usman Arif

  16. A Selection Sort Example Smallest ! Muhammad Usman Arif

  17. A Selection Sort Example Swap Muhammad Usman Arif

  18. A Selection Sort Example Swapped Muhammad Usman Arif

  19. A Selection Sort Example Smallest ? Muhammad Usman Arif

  20. A Selection Sort Example Smallest ? Muhammad Usman Arif

  21. A Selection Sort Example Smallest ! Muhammad Usman Arif

  22. A Selection Sort Example Swap Muhammad Usman Arif

  23. A Selection Sort Example Swapped Muhammad Usman Arif

  24. A Selection Sort Example The last two elements are in order, so no swap is necessary. Muhammad Usman Arif

  25. What “Swapping” Means TEMP 6 Place the first element into the Temporary Variable. Muhammad Usman Arif

  26. What “Swapping” Means TEMP 6 Replace the first element with the value of the smallest element. Muhammad Usman Arif

  27. What “Swapping” Means TEMP 6 Replace the third element (in this example) with the Temporary Variable. Muhammad Usman Arif

  28. Java Source Code: Selection Sort Muhammad Usman Arif

  29. Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system.Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Selection Sort is O(n2), because it takes approximately n2 passes to sort the elements. Muhammad Usman Arif

  30. Insertion Sort Description The insertion sort takes advantage an array’s partial ordering and is the most efficient sort to use when you know the array is already partially ordered. On the kth pass, the kth item should be inserted into its place among the first k items in the vector. After the kth pass (k starting at 1), the first k items of the vector should be in sorted order. This is like the way that people pick up playing cards and order them in their hands. When holding the first (k - 1) cards in order, a person will pick up the kth card and compare it with cards already held until its sorted spot is found. Muhammad Usman Arif

  31. Insertion Sort Algorithm For each k from 1 to n - 1 (k is the index of vector element to insert) Set item_to_insert to v[k] Set j to k - 1 (j starts at k - 1 and is decremented until insertion position is found) While (insertion position not found) and (not beginning of vector) If item_to_insert < v[j] Move v[j] to index position j + 1 Decrement j by 1 Else The insertion position has been found item_to_insert should be positioned at index j + 1 Muhammad Usman Arif

  32. Java Code For Insertion Sort Muhammad Usman Arif

  33. Insertion Sort Example The Unsorted Vector: For each pass, the index j begins at the (k - 1)st item and moves that item to position j + 1 until we find the insertion point for what was originally the kth item. We start with k = 1 and set j = k-1 or 0 (zero) Muhammad Usman Arif

  34. The First Pass K = 2 40 80 80 Insert 40 Insert 40, compare & move 80 40 80 32 32 32 84 84 84 61 61 61 item_to_insert 40 Muhammad Usman Arif

  35. The Second Pass K = 3 40 40 40 32 Compare & move Insert 32 80 80 40 40 Insert 32, compare & move 32 80 80 80 84 84 84 84 61 61 61 61 item_to_insert 32 Muhammad Usman Arif

  36. The Third Pass K = 4 32 40 80 Insert 84? compare & stop 84 61 item_to_insert 84 Muhammad Usman Arif

  37. The Fourth Pass K = 5 32 32 32 32 Compare & stop Insert 61 40 40 40 40 80 80 80 61 Compare & move 84 84 Insert 61, compare & move 80 80 61 84 84 84 item_to_insert 61 Muhammad Usman Arif

  38. What “Moving” Means item_to_insert 40 Place the second element into the variable item_to_insert. Muhammad Usman Arif

  39. What “Moving” Means item_to_insert 40 Replace the second element with the value of the first element. Muhammad Usman Arif

  40. What “Moving” Means item_to_insert 40 Replace the first element (in this example) with the variable item_to_insert. Muhammad Usman Arif

  41. Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system.Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Insertion Sort is O(n2), because it takes approximately n2 passes to sort the “n” elements. Muhammad Usman Arif

  42. Comparison of Quadratic Sorts • None good for large arrays! Muhammad Usman Arif

  43. Problems with insertion sort • Suppose a small item is on the far right, where the large items should be. To move this small item to its proper place on the left, all the intervening items (between where it is and where it should be) must be shifted one space right. • This is close to N copies, just for one item. Not all the items must be moved a full N spaces, but the average item must be moved N/2 spaces, which takes N times N/2 shifts for a total of N 2/2 copies. Thus the performance of insertion sort is O(N2). Muhammad Usman Arif

  44. Shell Sort: A Better Insertion Sort • Shell sort is a variant of insertion sort • Average performance: O(n3/2) or better • Divide and conquer approach to insertion sort • Sort many smaller sub-arrays using insertion sort • Sort progressively larger arrays • Finally sort the entire array • These arrays are elements separated by a gap • Start with large gap • Decrease the gap on each “pass” Muhammad Usman Arif

  45. Shell Sort: The Varying Gap 4-Sort (0,4,8), (1,5,9), (2,6), and (3,7) Notice that, in this particular example, at the end of the 4-sort no item is more than 2 cells from where it would be if the array were completely sorted. The interval is then repeatedly reduced until it becomes 1. Muhammad Usman Arif

  46. Shell Sort Algorithm • Set gap to (n-1)/3 • while gap > 0 • for each element from gap to end, by gap • Insert element in its gap-separated sub-array • if gap is 2, set it to 1 Muhammad Usman Arif

  47. Shell Sort Algorithm: Inner Loop 3.1 set nextPos to position of element to insert 3.2 set nextVal to value of that element 3.3 while nextPos > gap and element at nextPos-gap is > nextVal 3.4 Shift element at nextPos-gap to nextPos 3.5 Decrement nextPos by gap 3.6 Insert nextVal at nextPos Muhammad Usman Arif

  48. Shell Sort Code Muhammad Usman Arif

  49. Shell Sort Code (2) Muhammad Usman Arif

  50. Analysis of Shell Sort • Intuition: Reduces work by moving elements farther earlier • Performance depends on sequence of gap values • Sequence 2k-1, performance is O(n3/2) • We start with n/2 and repeatedly divide by 2.2 • Empirical results show this is O(n5/4) or O(n7/6) • No theoretical basis (proof) that this holds Muhammad Usman Arif

More Related