Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms

Similar presentations


Presentation on theme: "Data Structures & Algorithms"— Presentation transcript:

1 Data Structures & Algorithms
Sorting 1

2 Sorting Recall Selection Sort Insertion Sort Merge Sort Now consider
Bubble Sort Shell Sort Quick Sort 2

3 Bubble Sort Keep passing through the array
Exchange adjacent elements that are out of order Until array is sorted void bubbleSort(Item a[], int l, int r) { for (int i = l; i < r; ++i) for (int j = r; j > i; --j) compExch(a[j-1], a[j]); } 3

4 Bubble Sort A L G O R I T H M * A G L H O I R M T A L G O R I H T M A
4

5 Bubble Sort Property 6.3: BubbleSort uses about N2/2 comparisons and N2/2 exchanges on the average and in the worst case. void bubbleSort(Item a[], int l, int r) { for (int i = l; i < r; ++i) for (int j = r; j > i; --j) compExch(a[j-1], a[j]); } 5

6 Shell Sort Make array h-sorted:
h-sorted means the h sub-arrays are sorted (with sub-array i being elements in a[j] where j%h == i) Exchange distant elements that are out of order Decrease h until array is sorted (h = 1) Step sequence is not obvious, and there are many interactions – geometric best Knuth recommended s[i+1] = 3*s[i]+1 6

7 Shell Sort Make array h-sorted for decreasing h:
void shellSort(Item a[], int l, int r) { int h; for (h = 1; h <= (r-l)/9; h = 3*h+1); for ( ; h > 0; h/= 3) for (int i = l+h; i <= r; ++i) { int j = i; Item v = a[i]; while (j >= l+h && v < a[j-h]) { a[j] = a[j-h]; j -= h; } a[j] = v; } 7

8 Shell Sort h=5 h=2 A L G O R I T H M A L G I R M T H O A L G O R I T H
8

9 Shell Sort h=1 A H G I O L R M T A G H I L O R M T A H G I O L R M T A
9

10 ShellSort Property 6.7: The result of h-sorting a file that is k-ordered is a file that is both h- and k-ordered. Property 6.8: ShellSort does less than N(h-1)(k-1)/g comparisons to g-sort a file that is h- and k-ordered, provided that h and k are relatively prime Property 6.9: ShellSort does less than O(N3/2) comparisons for the Knuth sequence. 10

11 Sorting with Linked Lists
Can sort with linked lists Need sorting algorithm to process sequentially – in same manner that link lists support e.g., selection sort, insertion sort, bubble sort Insertion is fast (adjust some links – constant time) Shift is fast (just insert!) Never change key or information in nodes, ONLY change links! 11

12 Key-Indexed Counting When keys are all in some small range (relative to the number of items) Can use the keys as indices e.g., sort N items with distinct keys in range 0 to N What if there are duplicate keys? 12

13 Key-Indexed Counting Sort N items with keys in range 0 to M-1
Let count[i] be the number of items with a key value = i (can find in one pass) Let rank[i] be the number of items with a key value < i (partial sums of counts) Put item with key k into array between rank[k] and rank[k+1] Algorithm due to Seward (1954) 13

14 Key-Indexed Counting void distcount(int a[], int l, int r)
{ int I, j, cnt[M]; // M = # of keys static int b[maxN]; for (j = 0; j < M; ++j) cnt[j] = 0; for (i = l; i <= r; ++i) ++cnt[a[i]+1]; // count keys for (j = 1; j < M; ++j) cnt[j] += cnt[j-1]; // comp ranks b[cnt[a[i]]++ = a[i]; // sort a[i] = b[i-l]; // copy back } 14

15 Key Indexed Sort for (j = 0; j < M; ++j) cnt[j] = 0;
1 2 3 4 5 1 2 3 4 5 6 7 cnt: a: 4 2 2 4 1 1 1 1 1 1 1 2 1 1 2 2 1 2 2 2 2 1 2 2 3 1 2 2 for (j = 0; j < M; ++j) cnt[j] = 0; for (i = l; i <= r; ++i) ++cnt[a[i]+1]; // count keys 15

16 Key Indexed Sort for (j = 1; j < M; ++j)
1 2 3 4 5 1 2 3 4 5 6 7 cnt: 3 1 2 2 a: 4 2 2 4 1 3 1 2 2 3 4 2 2 3 4 6 2 3 4 6 6 2 3 4 6 6 8 for (j = 1; j < M; ++j) cnt[j] += cnt[j-1]; // comp ranks 16

17 for (i = l; i <= r; ++i) b[cnt[a[i]]++ = a[i]; // sort
1 2 3 4 5 6 7 1 2 3 4 5 a: 4 2 2 4 1 cnt: 3 4 6 6 8 b: - - - - - - - - 3 4 6 7 8 - - - - - - 4 - 1 3 4 6 7 8 - - - - - 4 - 1 3 5 6 7 8 - - - 2 - 4 - 2 3 5 6 7 8 - - 2 - 4 - 2 3 6 6 7 8 - - 2 2 4 - 2 3 6 6 8 8 - - 2 2 4 4 2 4 6 6 8 8 - 1 2 2 4 4 3 4 6 6 8 8 1 2 2 4 4 for (i = l; i <= r; ++i) b[cnt[a[i]]++ = a[i]; // sort 17

18 Key-Indexed Counting Prop. 6.12: Key-Indexed Counting is a linear-time sort, provided the range of distinct key values is within a constant factor of the file size. Prf: Each item is moved twice (once to the temp array b, once back to a). Each key is referenced twice (once for counts, once for distribution). The other two loop that build the counts and ranks are linear in M. 18


Download ppt "Data Structures & Algorithms"

Similar presentations


Ads by Google