Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &"— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 9: Algorithm Efficiency and Sorting Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 2 Sorting Algorithms and Their Efficiency Sorting –A process that organizes a collection of data into either ascending or descending order –The sort key The part of a data item that we consider when sorting a data collection

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 3 Sorting Algorithms and Their Efficiency Categories of sorting algorithms –An internal sort Requires that the collection of data fit entirely in the computer’s main memory –An external sort The collection of data will not fit in the computer’s main memory all at once, but must reside in secondary storage

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 4 1) Selection Sort Strategy –Place the largest (or smallest) item in its correct place –Place the next largest (or next smallest) item in its correct place, and so on Does not depend on the initial arrangement of the data

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 5 Selection Sort Figure 9-4 A selection sort of an array of five integers

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Function Selection Sort int indexOfLargest(const DataType theArray[], int size) { int indexSoFar = 0 for (int currentIndex = 1; currentIndex < size; ++currentIndex) { if (theArray[currentIndex] > theArray[indexSoFar]) indexSoFar = currentIndex; } return indexSoFar; } void swap(DataType& x, DataType& y) { DataType temp = x; x = y; y = temp; } // end swap void selectionSort(DataType theArray[], int n) { for (int last = n-1; last >= 1; --last) { int largest = indexOfLargest(theArray, last+1); swap(theArray[largest], theArray[last]); } // end for } // end selectionSort 6

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 7 2) Bubble Sort Strategy –Compare adjacent elements and exchange them if they are out of order Moves the largest (or smallest) elements to the end of the array Repeating this process eventually sorts the array into ascending (or descending) order

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 8 Bubble Sort Figure 9-5 The first two passes of a bubble sort of an array of five integers: (a) pass 1; (b) pass 2

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 9 3) Insertion Sort Strategy –Partition the array into two regions: sorted and unsorted Take each item from the unsorted region and insert it into its correct order in the sorted region Consider array[0] = initial sorted region and the rest is unsorted.

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 10 Insertion Sort Figure 9-7 An insertion sort of an array of five integers.

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 11 4) Mergesort A recursive sorting algorithm Performance is independent of the initial order of the array items Strategy –Divide an array into halves –Sort each half –Merge the sorted halves into one sorted array –Divide-and-conquer

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 12 Mergesort Analysis –Advantage Mergesort is an extremely fast algorithm –Disadvantage Mergesort requires a second array as large as the original array

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 13 Mergesort Figure 9-8 A mergesort with an auxiliary temporary array

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 14 Mergesort Figure 9-9 A mergesort of an array of six integers

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 15 5) Quicksort A divide-and-conquer algorithm Strategy –Choose a pivot (an element from the list), usually the the first element –Partition the array about the pivot items < pivot is S 1 ( left) items >= pivot is S 2 (right) Pivot is now in correct sorted position –Sort the left section –Sort the right section

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 16 Quicksort Using an invariant to develop a partition algorithm –The items in region S 1 are all less than the pivot, and those in S 2 are all greater than or equal to the pivot Figure 9-14 Invariant for the partition algorithm

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 17 Quicksort Analysis –Quicksort is usually extremely fast in practice –Even if the worst case occurs, quicksort’s performance is acceptable for moderately large arrays –Arranging the array items around the pivot p generates two smaller sorting problems – sort the left section of the array (S 1 ) and sort the right section of the array (S 2).

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 18 Quicksort Figure 9-19 A worst-case partitioning with quicksort

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 19 6) Radix Sort Strategy –Treats each data element as a character string –Repeatedly organizes the data into groups according to the i th character in each element

20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. 20 Radix Sort Figure 9-21 A radix sort of eight integers

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Exercise Given the array [12,23,5,10,34] Apply the below sorting techniques to sort the array into descending order. 1) Selection sort 2) Bubble sort 3) Insertion sort 4) Mergesort 21


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &"

Similar presentations


Ads by Google