Download presentation
Presentation is loading. Please wait.
1
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf (ab_sarraf@aut.ac.ir) Internal Sorting
2
Dept. of Computer Eng. & IT, Amirkabir University of Technology Objectives Bubble Sort (reviewed ) Insertion Sort (reviewed ) Merge Sort Quick Sort Heap Sort Radix Sort Bucket Sort, Selection Sort, Shell Sort, BST and many others Efficiency
3
Dept. of Computer Eng. & IT, Amirkabir University of Technology Bubble Sort void bubbleSort (Array S, length n) { boolean isSorted = false; while(!isSorted) { isSorted = true; for(i = 0; i<n; i++) { if(S[i] > S[i+1]) { int aux = S[i]; S[i] = S[i+1]; S[i+1] = aux; isSorted = false; }
4
Dept. of Computer Eng. & IT, Amirkabir University of Technology Bubble Sort Why Bubble Sort ? 11 23 2 56 9 8 10 100 21 2 23 56 9 8 10 100 31 2 23 9 56 8 10 100 41 2 23 9 8 56 10 100 51 2 23 9 8 10 56 100 ---- finish the first traversal ---- ---- start again ---- 11 2 23 9 8 10 56 100 21 2 9 23 8 10 56 100 31 2 9 8 23 10 56 100 41 2 9 8 10 23 56 100 ---- finish the second traversal ---- ---- start again ---- ………………….
5
Dept. of Computer Eng. & IT, Amirkabir University of Technology Insertion Sort void insert(const Element e, Element* list, int i) // Insert element e with key e.key into the ordered sequence list[0], …, list[i] such that the // resulting sequence is also ordered. Assume that e.key ≥ list[0].key. The array list must // have space allocated for at least i + 2 elements { while (i > 0 && e.getKey() < list[i].getKey()) { list[i+1] = list[i]; i--; } list[i+1] = e; }
6
Dept. of Computer Eng. & IT, Amirkabir University of Technology Insertion Sort void InsertSort(Element* list, const int n) // Sort list in nondecreasing order of key { list[0].setKey(MININT); for (int j = 2; j <= n; j++) insert(list[j], list, j-1); }
7
Dept. of Computer Eng. & IT, Amirkabir University of Technology Insertion Sort
8
Dept. of Computer Eng. & IT, Amirkabir University of Technology Merge Sort – non recursive (familiar?) 265771611159154819 5 261 7711 6115 5919 48 1 5 26 7711 15 59 6119 48 1 5 11 15 26 59 61 7719 48 1 5 11 15 19 26 48 59 61 77
9
Dept. of Computer Eng. & IT, Amirkabir University of Technology Merge Sort – recursive 265771611159154819 5 2611 5919 48 5 26 7711 15 5919 48 1 5 26 61 7711 15 19 48 59 1 5 11 15 19 26 48 59 61 77 1 61
10
Dept. of Computer Eng. & IT, Amirkabir University of Technology Merge Sort - recursive List mergesort(List inlist) { if (inlist.length() <= 1)return inlist; List l1 = half of the items from inlist; List l2 = other half of items from inlist; return merge(mergesort(l1), mergesort(l2)); }
11
Dept. of Computer Eng. & IT, Amirkabir University of Technology Quick Sort 1) Select: pick an element 2) Divide: rearrange elements so that x goes to its final position E 3) Recurse and Conquer: recursively sort
12
Dept. of Computer Eng. & IT, Amirkabir University of Technology Quick Sort void QuickSort(Element* list, const int left, const int right) // Sort records list[left], …, list[right] into nondecreasing order on field key. Key pivot = list[left].key is // arbitrarily chosen as the pivot key. Pointers I and j are used to partition the sublist so that at any time // list[m].key pivot, m j. It is assumed that list[left].key ≤ list[right+1].key. { if (left < right) { int i = left, j = right + 1, pivot = list[left].getKey(); do { do i++; while (list[i].getKey() < pivot); do j--; while (list[j].getKey() > pivot); if (i<j) InterChange(list, i, j); } while (i < j); InterChange(list, left, j); QuickSort(list, left, j–1); QuickSort(list, j+1, right); }
13
Dept. of Computer Eng. & IT, Amirkabir University of Technology Quick Sort Divide step: l scans the sequence from the left, and r from the right. A swap is performed when l is at an element larger than the pivot and r is at one smaller than the pivot.
14
Dept. of Computer Eng. & IT, Amirkabir University of Technology A final swap with the pivot completes the divide step
15
Dept. of Computer Eng. & IT, Amirkabir University of Technology Quick Sort Worst case: when the pivot does not divide the sequence in two At each step, the length of the sequence is only reduced by 1 Total running time General case: Time spent at level i in the tree is O(n) Running time: O(n) * O(height) Average case: O(n log n) Worst case: O(n^2)
16
Dept. of Computer Eng. & IT, Amirkabir University of Technology Heap Sort 26 154819 5 161 77 1159 77 1515 61 4819 59 1126 [1] [2] [3] [5] [6] [7] [8] [9] [10] [2] [3] [4] [5] [6] [7] [8] [9] [10] (a) Input array (b) Initial heap [1] [4]
17
Dept. of Computer Eng. & IT, Amirkabir University of Technology Heap Sort void adjust (Element* tree, const int root, const int n) // Adjust the binary tree with root root to satisfy the heap property. The left and right subtrees of root // already satisfy the heap property. No node has index greater than n. { Element e = tree[root]; int k = e.getKey(); for (int j = 2*root; j <= n; j *= 2) { if (j < n) if (tree[j].getKey() < tree[j+1].getKey()) j++; // compare max child with k. If k is max, then done. if (k >= tree[j].getKey()) break; tree[j/2] = tree[j]; // move jth record up the tree } tree[j/2] = e; }
18
Dept. of Computer Eng. & IT, Amirkabir University of Technology Heap Sort void HeapSort (Element* list, const int n) // The list list = (list[1], …, list[n]) is sorted into nondecreasing order of the field key. { for (int i = n/2; i >= 1; i--) // convert list into a heap adjust(list, i, n); for (i = n-1; i >= 1; i--) // sort list { Element t = list[i+1]; // interchange list 1 and list i+1 list[i+1] = list[1]; list[1] = t; adjust(list, 1, i); }
19
Dept. of Computer Eng. & IT, Amirkabir University of Technology Heap Sort
20
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort In a radix sort, we decompose the sort key using some radix r. The number of bins needed is r. Assume the records R1, R2, …, Rn to be sorted based on a radix of r. Each key has d digits in the range of 0 to r-1. Assume each record has a link field. Then the records in the same bin are linked together into a chain: f[i], 0 ≤ i ≤ r (the pointer to the first record in bin i) e[i], (the pointer to the end record in bin i) The chain will operate as a queue. Each record is assumed to have an array key[d], 0 ≤ key[i] ≤ r, 0 ≤ i ≤ d.
21
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort void RadixSort (Element* list, const int d, const int n) { int e[radix], f[radix]; // queue pointers for (int i = 1; i <= n; i++) list[i].link = i + 1; // link into a chain starting at current list[n].link = 0; int current = 1; for (i = d – 1; i >= 0; i--) // sort on key key[i] { for (int j = 0; j < radix; j++) f[j] = 0; // initialize bins to empty queues for (; current; current = list[current].link) { // put records into queues int k = list[current].key[i]; if (f[k] == 0) f[k] = current; else list[e[k]].link = current; e[k] = current; } for (j = 0; f[j] == 0; j++); // find the first non-empty queue current = f[j]; int last = e[j]; for (int k = j+1; k < radix; k++){ // concatenate remaining queues if (f[k]){ list[last].link = f[k]; last = e[k]; } list[last].link = 0; } O(n) O(r) d passes O(d(n+r))
22
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort 1792083069385998455927133 list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10] e[0]e[1]e[2]e[3]e[4]e[5]e[6]e[7]e[8]e[9] f[0]f[1]f[2]f[3]f[4]f[5]f[6]f[7]f[8]f[9] 179 859 9 2083065598493 33 271 9333984553062081798599 list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
23
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort e[0]e[1]e[2]e[3]e[4]e[5]e[6]e[7]e[8]e[9] f[0]f[1]f[2]f[3]f[4]f[5]f[6]f[7]f[8]f[9] 179859 9 208 306559849333271 9333984553062081798599 list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10] 3062089335585927117998493 list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
24
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort e[0]e[1]e[2]e[3]e[4]e[5]e[6]e[7]e[8]e[9] f[0]f[1]f[2]f[3]f[4]f[5]f[6]f[7]f[8]f[9] 179 55 33 9 859984 306 271 9335593179208271306859948 list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10] 3062089335585927117998493 list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10] 93 208
25
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort
26
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort Example Problem: sort 1 million 64-bit numbers Treat as four-digit radix 2^16 numbers Can sort in just four passes with radix sort! Running time: 4( 1 million + 2^16 ) 4 million operations Compare with typical O(n lg n) comparison sort Requires approx lg n = 20 operations per number being sorted Total running time 20 million operations
27
Dept. of Computer Eng. & IT, Amirkabir University of Technology Radix Sort In general, radix sort based on bucket sort is Asymptotically fast (i.e., O(n)) Simple to code A good choice Fast, Stable, Simple Can radix sort be used on floating-point numbers?
28
Dept. of Computer Eng. & IT, Amirkabir University of Technology Summary
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.