1 / 35

An Introduction to Sorting

An Introduction to Sorting. Chapter 11. Chapter Contents. Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection Sort Insertion Sort Iterative Insertion Sort Recursive Insertion Sort The Efficiency of Insertion Sort

makana
Download Presentation

An Introduction to Sorting

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. An Introduction to Sorting Chapter 11

  2. Chapter Contents • Selection Sort • Iterative Selection Sort • Recursive Selection Sort • The Efficiency of Selection Sort • Insertion Sort • Iterative Insertion Sort • Recursive Insertion Sort • The Efficiency of Insertion Sort • Insertion Sort of a Chain of Linked Nodes

  3. Chapter Contents • Shell Sort • The Java Code • The Efficiency of Shell Sort • Comparing the Algorithms

  4. Selection Sort • Task: rearrange books on shelf by height • Shortest book on the left • Approach: • Look at books, select shortest book • Swap with first book • Look at remaining books, select shortest • Swap with second book • Repeat …

  5. Selection Sort Fig. 11-2 Before and after exchanging shortest book and the first book.

  6. Selection Sort Fig. 11-3 A selection sort of an array of integers into ascending order.

  7. Selection Sort • There are two parts in the array: • Unsorted zone • Sorted zone • In a loop, the lowest element from the unsorted zone is selected (hence the name Selection Sort) and placed at the beginning of the unsorted zone. After that, the sorted zone expands to cover the recently moved element, and the unsorted zone shrinks to lose that element. Eventually, the sorted zone becomes the entire array, and the unsorted zone disappears. The array is then considered to be sorted.

  8. Iterative Selection Sort • View iterative algorithm for selection sort • View class for sorting an array using selection sort • Search for index of smallest element • Swap values in array elements • Puts smallest at start of current sub array

  9. Iterative Selection Sort public static void selectionSort(int[] array) // pre: array is full, all elements are valid integers (not null) // post: array is sorted in ascending order (lowest to highest) { int N = array.length; for(inti= 0; i <= N-2; i++)// start at the beginning of the array { intlowestIndex= i; // default value of the lowest element index for(int j = i+1; j <= N-1; j++) // loop from the beginning of unsorted zone { if(array[j] < array[lowestIndex]) // compare current to lowest lowestIndex= j; } if (lowestIndex != i) // swap the two values { inttemp = array[i]; array[i] = array[lowestIndex]; array[lowestIndex] = temp; } } }

  10. The Efficiency of Selection Sort • Iterative method for loop executes n – 1 times • For each of n-1calls, inner loop executes (n-2)/2 times average • (n-1)(n-2)/2 = (n2 - 3n + 2)/2 = O(n2) • Recursive selection sort performs same number of operations • Also O(n2)

  11. Insertion Sort • If first two books are out of order • Remove second book • Slide first book to right • Insert removed book into first slot • Then look at third book. If it is out of order: • Remove that book • Slide 2nd book to right • Compare removed book to the first book • If removed book smaller slide first book to right • Insert removed book into found slot • Then look at fourth book. If it is out of order: • Etc.

  12. Insertion Sort Fig. 11-5 An insertion sort of books

  13. Iterative Insertion Sort • View iterative algorithm for insertion sort • Note the algorithm for the insert of the item into the array Fig. 11-6 An insertion sort inserts the next unsorted element into its proper location within the sorted portion of an array

  14. Iterative Insertion Sort public static void insertionSort(int array[], int n) { for (inti = 1; i < n; i++) // the unsorted part { inttemp = array[i]; // removefirstunsorted element int j = i; // start shifting at i while((j>0) && (array[j-1]>temp)) // until one is smaller { array[j] = array[j-1]; // shiftitemright j--; // goleftoneposition } array[j] = temp; // insert newsorteditem } }

  15. Iterative Insertion Sort Fig. 11-7 An insertion sort of an array of integers into ascending order

  16. Efficiency of Insertion Sort • Best time efficiency is O(n) • Worst time efficiency is O(n2) • If array is closer to sorted order • Less work the insertion sort does • More efficient the sort is • Insertion sort is acceptable for small array sizes

  17. Shell Sort • A variation of the insertion sort • But faster than O(n2) • Done by sorting subarrays of equally spaced indices (using insertion sort) • Instead of moving to an adjacent location an element moves several locations away • Results in an almost sorted array • This array sorted efficiently with ordinary insertion sort

  18. Shell Sort Fig. 11-12 An array and the subarrays formed by grouping elements whose indices are 6 apart.

  19. Shell Sort Fig. 11-13 The subarrays of Fig. 11-12 after they are sorted, and the array that contains them.

  20. Shell Sort Fig. 11-14 The subarrays of the array in Fig. 11-13 formed by grouping elements whose indices are 3 apart

  21. Shell Sort Fig. 11-15 The subarrays of Fig. 11-14 after they are sorted, and the array that contains them. View source code for incrementalInsertionSort andshellSort

  22. Efficiency of Shell Sort • Efficiency is O(n2) for worst case • If n is a power of 2 • Average-case behavior is O(n1.5) • Any time the variable space (see source code) is even, add 1 • This results in O(n1.5) for worst case

  23. Comparing the Algorithms Best Average Worst Case Case Case Selection sort O(n2) O(n2) O(n2) Insertion sort O(n) O(n2) O(n2) Shell sort O(n) O(n1.5) O(n1.5) Fig. 11-16 The time efficiencies of three sorting algorithms, expressed in Big Oh notation.

  24. Searching Chapter 16

  25. The Problem Fig. 16-1 Searching is an every day occurrence.

  26. Searching an Unsorted Array Fig. 16-2 An iterative sequential search of an array that (a) finds its target

  27. Searching an Unsorted Array Fig. 16-2 An iterative sequential search of an array that (b) does not find its target

  28. Recursive Sequential Search an Unsorted Array • Pseudocode for a recursive algorithm to search an array. • View source code of Java implementation

  29. Recursive Sequential Search an Unsorted Array Fig. 16-3 A recursive sequential search of an array that (a) finds its target; (b) does not find its target.

  30. Efficiency of a Sequential Search • Best case O(1) • Locate desired item first • Worst case O(n) • Must look at all the items • Average case O(n) • Must look at half the items • O(n/2) is still O(n) • Discuss this: Do you agree or not? Why?

  31. Searching a Sorted Array • A sequential search can be more efficient if the data is sorted Fig. 16-4 Coins sorted by their mint dates.

  32. Binary Search of Sorted Array Fig. 16-5 Ignoring one-half of the data when the data is sorted.

  33. Binary Search of Sorted Array Fig. 16-6 A recursive binary search of a sorted array that (a) finds its target;

  34. Binary Search of Sorted Array Fig. 16-6 A recursive binary search of a sorted array that (b) does not find its target. Click to view Java version of method binarySearch in context

  35. Efficiency of a Binary Search • Best case O(1) • Locate desired item first • Worst case O(log n) • Must look at all the items • Average case O(log n)

More Related