350 likes | 424 Views
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
E N D
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 • Insertion Sort of a Chain of Linked Nodes
Chapter Contents • Shell Sort • The Java Code • The Efficiency of Shell Sort • Comparing the Algorithms
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 …
Selection Sort Fig. 11-2 Before and after exchanging shortest book and the first book.
Selection Sort Fig. 11-3 A selection sort of an array of integers into ascending order.
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.
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
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; } } }
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)
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.
Insertion Sort Fig. 11-5 An insertion sort of books
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
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 } }
Iterative Insertion Sort Fig. 11-7 An insertion sort of an array of integers into ascending order
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
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
Shell Sort Fig. 11-12 An array and the subarrays formed by grouping elements whose indices are 6 apart.
Shell Sort Fig. 11-13 The subarrays of Fig. 11-12 after they are sorted, and the array that contains them.
Shell Sort Fig. 11-14 The subarrays of the array in Fig. 11-13 formed by grouping elements whose indices are 3 apart
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
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
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.
Searching Chapter 16
The Problem Fig. 16-1 Searching is an every day occurrence.
Searching an Unsorted Array Fig. 16-2 An iterative sequential search of an array that (a) finds its target
Searching an Unsorted Array Fig. 16-2 An iterative sequential search of an array that (b) does not find its target
Recursive Sequential Search an Unsorted Array • Pseudocode for a recursive algorithm to search an array. • View source code of Java implementation
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.
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?
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.
Binary Search of Sorted Array Fig. 16-5 Ignoring one-half of the data when the data is sorted.
Binary Search of Sorted Array Fig. 16-6 A recursive binary search of a sorted array that (a) finds its target;
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
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)