Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting II.

Similar presentations


Presentation on theme: "CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting II."— Presentation transcript:

1 CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting II

2 2 Topics Sorting Bubble Sort Selection Sort Insertion Sort

3 3 Bubble Sort Idea: Begin with the whole array. Compare two adjacent items. Exchange if they are out of order. Proceed until you reach the end of the array. In the second pass, consider the first (n-1) items only and repeat the process. At most (n-1) passes are required.

4 4 Bubblesort public static void bubbleSort(Comparable[] theArray, int n) { for (int pass = 1; pass < n; ++pass) { for (int index = 0; index < n-pass; ++index) { int nextIndex = index + 1; if (theArray[index].compareTo(theArray[nextIndex])>0){ // exchange items Comparable temp = theArray[index]; theArray[index] = theArray[nextIndex]; theArray[nextIndex] = temp; } // end if } // end for } // end bubbleSort

5 5 Bubblesort Analysis Basic loop structure: for ( int pass = 1 ; pass < n ; ++pass )... for ( int index = 0 ; index < n-pass ; ++index )... The outer loop iterates n - 1 times. Inner loop iterations depends on which iteration of the outer loop. So to derive the growth rate function f(n) we must look at each outer loop iteration.

6 6 Bubblesort Analysis (2) Make a table of inner loop iterations for each outer loop iteration: Value of pass Number of inner loop iterations 1 2 3 4... n -1 n -2 n -3 n -4... 1

7 7 BubbleSort : Analysis (3) Now sum the column containing the number of inner loop iterations: (n -1) + (n -2) + (n -3) +... + 1 = n (n -1) / 2 And thus the growth rate function f(n) is: f(n) = n 2 / 2 - n / 2 and the dominant term is n 2 So BubbleSort has time complexity O (n 2 ) Are there any special cases (best, worst)?

8 8 Selection Sort Idea: 1. Find the index of the largest item 2. Swap the largest item with the item in the last position 3. Repeat steps 1 & 2 for the next largest item, reducing the size of the array by the last item 4. Continue until you have selected & swapped n-1 of the n items in the array

9 9 Selection Sort public static void selectionSort(Comparable[ ] theArray, int n) { // --------------------------------------------------- // last = index of the last item in the subarray of items yet to be sorted // largest = index of the largest item found for (int last = n-1; last >= 1; last--) { // Invariant: theArray[last+1..n-1] is sorted and > theArray[0..last] // select largest item in theArray[0..last] int largest = indexOfLargest(theArray, last+1); // swap largest item theArray[largest] with theArray[last] Comparable temp = theArray[largest]; theArray[largest] = theArray[last]; theArray[last] = temp; } // end for } // end selectionSort

10 10 Selection Sort : indexOfLargest private static int indexOfLargest(Comparable[ ] theArray, int size) { // --------------------------------------------------- // Finds the largest item in an array. // Precondition: theArray is an array of size items; size >= 1. // Postcondition: Returns the index of the largest item in the array. // -------------------------------------------------- int indexSoFar = 0; // index of largest item found so far // Invariant: theArray[indexSoFar]>=theArray[0..currIndex-1] for (int currIndex = 1; currIndex < size; ++currIndex) { if (theArray[currIndex].compareTo( theArray[indexSoFar] ) > 0) { indexSoFar = currIndex; } // end if } // end for return indexSoFar; // index of largest item } // end indexOfLargest

11 11 Selection Sort : Analysis The outer loop: for (int last = n-1; last >= 1; last--) Iterates n -1 times and each iteration calls indexOfLargest: int largest = indexOfLargest(theArray, last+1); The inner loop (in indexOfLargest): Number of iterations depends on which outer loop call (last + 1 is passed into size : for (int currIndex = 1; currIndex < size; ++currIndex)

12 12 Selection Sort : Analysis (2) Make a table of inner loop iterations for each outer loop iteration: Value of last Number of inner loop iterations n -1 n -2 n -3 n -4... 1 n -1 n -2 n -3 n -4... 1

13 13 Selection Sort : Analysis (3) Now sum the column containing the number of inner loop iterations: (n -1) + (n -2) + (n -3) +... + 1 = n (n -1) / 2 And thus the growth rate function f(n) is: f(n) = n 2 / 2 - n / 2 and the dominant term is n 2 So Selection Sort has time complexity O (n 2 ) Are there any special cases (best, worst)?

14 14 Insertion Sort Idea: Divide the array into two regions; Sorted & Unsorted Take the first item of the unsorted region & place it into its correct position in the sorted region Initially, the sorted region is the first element of the array and the Unsorted is the rest of the array At each step, the size of the sorted region grows by 1 & of the unsorted region shrinks by 1 The algorithm terminates when the Sorted region equals the size of the array

15 15 Insertion Sort public static void insertionSort(Comparable[] theArray, int n) { // unsorted = first index of the unsorted region, // loc = index of insertion in the sorted region, // nextItem = next item in the unsorted region for (int unsorted = 1; unsorted < n; ++unsorted) { // Invariant: theArray[0..unsorted-1] is sorted Comparable nextItem = theArray[unsorted]; int loc = unsorted; while ((loc > 0) && (theArray[loc-1].compareTo(nextItem) > 0)) { // shift theArray[loc-1] to the right theArray[loc--] = theArray[loc-1]; } // end while theArray[loc] = nextItem; // insert nextItem into sorted region } // end for } // end insertionSort

16 16 Insertion Sort : Analysis The outer loop: for (int unsorted = 1; unsorted < n; ++unsorted) Iterates n -1 times and each iteration selects nextItem to be moved into position. The inner loop: Iterates until nextItem is in its correct position or until the postion into which nextItem will be placed (loc) is at the top of the array: while ((loc > 0) && (theArray[loc-1].compareTo(nextItem) > 0))

17 17 Insertion Sort : Analysis (2) The number of inner loop iterations depends on the value of nextItem: Worst case?

18 18 Insertion Sort : Analysis (2) Worst case inner loop iterations: Value of unsorted Number of inner loop iterations 1 2 3 4... n -1 1 2 3 4... n -1

19 19 Insertion Sort : Analysis (3) Now sum the column containing the number of inner loop iterations: (n -1) + (n -2) + (n -3) +... + 1 = n (n -1) / 2 And thus the growth rate function f(n) is: f(n) = n 2 / 2 - n / 2 and the dominant term is n 2 So Insertion Sort has time complexity O (n 2 ) in the worst case.

20 20 Insertion Sort : Analysis (4) The number of inner loop iterations depends on the value of nextItem: Best case? Time complexity?

21 21 Review A growth-rate function of ______ implies a problem whose time requirement is constant. 1 n 2 n log 2 n

22 22 Review A linear algorithm has the growth-rate function ______. O(log 2 n) O(2 n ) O(n) O(1)

23 23 Review In the worst case, a binary search is ______. O(n) O(1) O(log 2 n) O(n 2 )

24 24 Review The selection sort is continued until ______ of the n items in an array have been swapped. n/2 n – 2 n – 1 n

25 25 Review Given the following array: 4 15 8 3 28 21 which of the following represents the array after the second swap of the selection sort? 4 3 8 15 21 28 4 15 8 3 21 28 3 4 8 15 21 28 21 4 3 8 15 28

26 26 Review Given the fact that a selection sort of n items requires n 2 /2 + 5 * n/2 – 3 major operations, the selection sort is ______. O(n) O(1) O(n 2 ) O(n 2 /2)

27 27 Review The ______ compares adjacent items and exchanges them if they are out of order. selection sort binary search bubble sort quicksort

28 28 Review A bubble sort requires at most ______ passes to sort an array of n items. n/2 n – 2 n – 1 n


Download ppt "CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting II."

Similar presentations


Ads by Google