Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 11 Sorting.

Similar presentations


Presentation on theme: "CHAPTER 11 Sorting."— Presentation transcript:

1 CHAPTER 11 Sorting

2 Introduction Sorting is the process of arranging a group of items into a defined order based on particular criteria Sequential sorts require approximately n2 comparisons to sort n elements. Logarithmic sorts typically require nlog2n comparisons to sort n elements

3 Assumptions Elements to sort are placed in arrays of length N.
Can be compared Sorting can be performed in main memory

4 Sorting Algorithms Simple sorts: O(N2) Bubble sort Selection sort
Insertion sort Advanced sorts: O(NlogN) Quick sort Merge sort Shell sort Heap sort Radix sort Binary search tree sort

5 Selection Sort Selection sort orders a list of values by repetitively putting a particular value into its final position More specifically: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places

6 Illustration of selection sort processing

7 Selection Sort : code for ( i = 0; i < N ; i++) { smallIndex = i;
for (j = i+1; j < N; j++) if (array[j] < array[smallIndex]) smallIndex = j; if (smallIndex != i) temp = array[j]; array[j] = array[smallIndex]; array[smallIndex] = temp; }

8 Insertion Sort Insertion sort orders a list of values by repetitively inserting a particular value into a sorted subset of the list More specifically: consider the first item to be a sorted sublist of length 1 insert the second item into the sorted sublist, shifting the first item if needed insert the third item into the sorted sublist, shifting the other items as needed repeat until all values have been inserted into their proper positions

9 Illustration of insertion sort processing

10 Insertion Sort: Algorithm
1. An array of one element only is sorted 2. Assume that the first p elements are sorted. For j = p to N-1 Take the j-th element and find a place for it among the first j sorted elements

11 Insertion Sort: Code int j, p; comparable tmp;
for ( p = 1; p < N ; p++) { tmp = a[p]; for (j = p; j > 0 && tmp < a[j-1]; j--) a[j] = a[j-1]; a[j] = tmp; }

12 Bubble Sort Bubble sort orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary More specifically: scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order

13 Complexity of Simple Sorts
Nested loops Perform approximately n2 comparisons Relatively inefficient Used for small input size

14 Quick Sort Quick sort orders a list of values by partitioning the list around one element, then sorting each partition More specifically: choose one element in the list to be the partition element organize the elements so that all elements less than the partition element are to the left and all greater are to the right apply the quick sort algorithm (recursively) to both partitions

15 Quick Sort The partition element is called pivot
The choice of the pivot is important For efficiency, it would be nice if the partition element divided the list roughly in half, but this is not guaranteed Choosing the pivot: the median-of-three algorithm

16 Merge Sort Merge sort orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining More specifically: divide the list into two roughly equal parts recursively divide each part in half, continuing until a part contains only one element merge the two parts into one sorted list continue to merge parts as the recursion unfolds

17 The Decomposition of Merge Sort

18 The merge portion of the merge sort algorithm

19 Efficiency of QuickSort and MergeSort
Both algorithms are O(nlogn) They use a recursive structure that takes log2n processing steps to decompose the original list into lists of length one At each step, both algorithms either compare or merge all n elements


Download ppt "CHAPTER 11 Sorting."

Similar presentations


Ads by Google