Download presentation
Presentation is loading. Please wait.
1
Sorting
2
Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. An internal sort requires that the collection of data fit entirely in the computer’s main memory. We can use an external sort when the collection of data cannot fit in the computer’s main memory all at once but must reside in secondary storage such as on a disk. We will analyze only internal sorting algorithms. Sorting also has indirect uses. An initial sort of the data can significantly enhance the performance of an algorithm. Majority of programming projects use a sort somewhere, and in many cases, the sorting cost determines the running time. A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons.
3
Sorting Algorithms Selection Sort Insertion Sort Bubble Sort
There are many sorting algorithms, such as: Selection Sort Insertion Sort Bubble Sort Shell Sort Merge Sort Quick Sort Heap sort Radix sort Bucket sort Median Sort ….
4
Selection Sort Sorted Unsorted 23 78 45 8 32 56 Original List
After pass 1 After pass 2 After pass 3 After pass 4 After pass 5
5
O(n2) 8 20 12 4 5 18 2
7
Insertion Sort Sorted Unsorted 23 78 45 8 32 6 Original List
After pass 1 After pass 2 After pass 3 After pass 4 After pass 5
8
Insertion Sort – Analysis
Running time depends on not only the size of the array but also the contents of the array. Best-case: (n) Array is already sorted in ascending order. Inner loop will not be executed. The number of moves: 2*(n-1) (n) The number of key comparisons: (n-1) (n) Worst-case: (n2) Array is in reverse order: Inner loop is executed i-1 times, for i = 2,3, …, n The number of moves: 2*(n-1)+( n-1)= (n2) The number of key comparisons: ( n-1)= n*(n-1)/2 (n2) Average-case: O(n2) We have to look at all possible initial data organizations.
9
Shell sort slides
11
Mergesort Mergesort algorithm is one of two important divide-and-conquer sorting algorithms (the other one is quicksort). It is a recursive algorithm. Divides the list into halves, Sort each halve separately, and Then merge the sorted halves into one sorted array.
13
Mergesort – Example2
15
Mergesort - Example
17
5th Week
23
Example: Partition Developing the first partition of an array
when the pivot is the first item
24
Quicksort – Analysis
44
NEXT WEEK
45
Linear Sort Algorithms
46
How fast can we sort? All the sorting algorithms we have seen so far are comparison-based sorts: only use comparisons to determine the relative order of elements. • E.g., insertion sort, merge sort, quicksort, heapsort. The best worst-case running time that we’ve seen for comparison sorting is O(nlgn). Is O(nlgn) the best we can do? Lower Bound Theory (e.i., Decision trees) can help us answer this question.
47
Sorting in linear time Counting sort: No comparisons between elements. •Input: A[1.. n], where A[ j]{1, 2, …, k} . •Output: B[1.. n], sorted. Auxiliary storage: C[1,k] .
51
Bucket Sort Bucket sort runs in linear time on the average. Like counting sort, bucket sort is fast since it assumes something about the input. Counting sort assumes that the input consists of integers in a small range. Bucket sort assumes that the input is generated by a random process that distributes elements uniformly over the interval [0,1].
52
Bucket Sort The idea of bucket sort is to divide the interval [0,1) into n equal-sized sub-intervals, or buckets, and then distribute the n-input numbers into the buckets. To produce the output, we simply sort the numbers in each bucket and then go through the buckets in order, listing the elements in each.
53
Bucket Sort BUCKET-SORT (A) n length(A) For i 0 to n do insert A[i] into list B[nA[i]] For i 0 to n-1 do insert B[i] with insertion sort Concatenate the lists B[0], B[1], ..., B[n-1] together in order
54
Bucket Sort A B 1 .78 2 .17 3 .39 4 .26 5 .72 6 .94 7 .21 8 .12 9 .23 10 .68 .12 .17 .21 .23 .26 .39 .68 .72 .78 .94
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.