Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Efficiency and Sorting

Similar presentations


Presentation on theme: "Algorithm Efficiency and Sorting"— Presentation transcript:

1 Algorithm Efficiency and Sorting
Chapter 10 (continued) Algorithm Efficiency and Sorting © 2011 Pearson Addison-Wesley. All rights reserved

2 Quicksort Alg. pseudo-code
void Quicksort(arr, low, high){ int j = partition(arr,low,high); Quicksort(arr, low, j-1); Quicksort(arr, j+1, high); } int Partition(arr, low, high){ int i = low, j = high, pivot = arr[low]; 0. while(i<j) 1. Increment i until (arr[i]>pivot or i>high) 2. Decrement j until (arr[j]< pivot or j<low) 3. if(i<j) swap(arr[i],arr[j]) 4. swap(arr[low],arr[j]) 5. return j } © 2011 Pearson Addison-Wesley. All rights reserved

3 Quicksort Analysis quicksort is usually extremely fast in practice
Even if the worst case occurs, quicksort’s performance is acceptable for moderately large arrays © 2011 Pearson Addison-Wesley. All rights reserved

4 Radix Sort Radix sort Analysis
Treats each data element as a character string Strategy Repeatedly organize the data into groups according to the ith character in each element Analysis Radix sort is O(n) © 2011 Pearson Addison-Wesley. All rights reserved

5 Radix Sort Figure 10-21 A radix sort of eight integers
© 2011 Pearson Addison-Wesley. All rights reserved

6 A Comparison of Sorting Algorithms
Figure 10-22 Approximate growth rates of time required for eight sorting algorithms © 2011 Pearson Addison-Wesley. All rights reserved

7 Summary Order-of-magnitude analysis and Big O notation measure an algorithm’s time requirement as a function of the problem size by using a growth-rate function To compare the inherit efficiency of algorithms Examine their growth-rate functions when the problems are large Consider only significant differences in growth-rate functions © 2011 Pearson Addison-Wesley. All rights reserved

8 Summary Worst-case and average-case analyses
Worst-case analysis considers the maximum amount of work an algorithm requires on a problem of a given size Average-case analysis considers the expected amount of work an algorithm requires on a problem of a given size Order-of-magnitude analysis can be used to choose an implementation for an abstract data type Selection sort, bubble sort, and insertion sort are all O(n2) algorithms Quicksort and mergesort are two very efficient sorting algorithms © 2011 Pearson Addison-Wesley. All rights reserved


Download ppt "Algorithm Efficiency and Sorting"

Similar presentations


Ads by Google