Download presentation
Presentation is loading. Please wait.
Published byBeryl Campbell Modified over 8 years ago
1
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms
2
Prof. Amr Goneid, AUC2 Sorting(1): Elementary Algorithms General Selection Sort Bubble Sort Insertion Sort
3
Prof. Amr Goneid, AUC3 General The Sorting Problem: Input: A sequence of keys {a 1, a 2, …, a n } output: A permutation (re-ordering) of the input, {a’ 1, a’ 2, …, a’ n } such that a’ 1 ≤ a’ 2 ≤ …≤ a’ n Sorting is the most fundamental algorithmic problem in computer science Sorting can efficiently solve many problems Different sorting algorithms have been developed, each of which rests on a particular idea.
4
Prof. Amr Goneid, AUC4 Some Applications of Sorting Efficient Searching Ordering a set so as to permit efficient binary search. Uniqueness Testing Test if the elements of a given collection of items are all distinct. Deleting Duplicates Remove all but one copy of any repeated elements in a collection.
5
Prof. Amr Goneid, AUC5 Some Applications of Sorting Closest Pair Given a set of n numbers, find the pair of numbers that have the smallest difference between them Median/Selection Find the k th largest item in set S. Can be used to find the median of a collection. Frequency Counting Find the most frequently occurring element in S, i.e., the mode. Prioritizing Events Sorting the items according to the deadline date.
6
Prof. Amr Goneid, AUC6 Types of Sorts In-Place Sort: An in-place sort of an array occurs within the array and uses no external storage or other arrays In-place sorts are more efficient in space utilization Stable Sorts: A sort is stable if it preserves the ordering of elements with the same key. i.e. If elements e1 and e2 have the same key, and e1 appears earlier than e2 before sorting, then e1 is located before e2 after sorting.
7
Prof. Amr Goneid, AUC7 General We compare sorting methods on the bases of: 1. The time complexity of the algorithm 2. If the algorithm is In-Place and Stable or not We examine here 3 elementary sorting algorithms: Selection Sort Bubble Sort Insertion Sort All of these algorithms have a worst case complexity of O(n 2 )
8
Prof. Amr Goneid, AUC8 1. Selection Sort The general idea of the selection sort is that for each slot, find the element that belongs there Assume elements to be in locations 0..n-1 Let (i) be the start of a sub-array of at least 2 elements, i.e. i = 0.. n-2 for each i = 0.. n-2 find smallest element in sub-array a[i..n-1] swap that element with that at the start of the sub-array (i.e. with a[i]).
9
Prof. Amr Goneid, AUC9 How it works 431625 134625 124635 123465 123645 123456
10
Prof. Amr Goneid, AUC10 Selection Sort (code) void selectsort (itemType a[ ], int n) { int i, j, m; for (i = 0; i < n-1; i++) { m = i ; for ( j = i+1; j < n; j++) if (a[j] a[m]) m = j ; swap (a[i], a[m]); }
11
Prof. Amr Goneid, AUC11 http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/SSort.html http://coderaptors.com/?SelectionSortDemos
12
Prof. Amr Goneid, AUC12 Selection Sort Algorithm SelectSort (a[0..n-1 ]) { for i = 0 to n-2 { m = index_of_minimum (a, i, n-1) ; swap (a[i], a[m]); }
13
Prof. Amr Goneid, AUC13 Analysis of Selection Sort
14
Prof. Amr Goneid, AUC14 Performance of Selection Sort The complexity of the selection sort is (n 2 ), independent of the initial data ordering. In-Place SortYes Stable AlgorithmNo This technique is satisfactory for small jobs, not efficient enough for large amounts of data.
15
Prof. Amr Goneid, AUC15 2. Bubble Sort The general idea is to compare adjacent elements and swap if necessary Assume elements to be in locations 0..n-1 Let (i) be the index of the last element in a sub- array of at least 2 elements, i.e. i = n-1.. 1
16
Prof. Amr Goneid, AUC16 Bubble Sort Algorithm for each i = n-1…1 Compare adjacent elements a j and a j+1, j = 0..i-1 and swap them if a j > a j+1 This will bubble the largest element in the sub-array a[0..i] to location (i).
17
Prof. Amr Goneid, AUC17 How it works 4312 3412 3142 1324 3124 1234
18
Prof. Amr Goneid, AUC18 Bubble Sort (code) void bubbleSort (itemType a[ ], int n) { int i, j; for (i = n-1; i >= 1; i--) for (j = 0; j < i; j++ ) if (a[j] > a[j+1] ) swap(a[j], a[j+1]); }
19
Prof. Amr Goneid, AUC19 http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSort.html http://coderaptors.com/?BubbleSortDemos
20
Prof. Amr Goneid, AUC20 Bubble Sort Algorithm BubbleSort (a[ 0..n-1]) { for i = n-1 down to 1 for j = 0 to i-1 if (a[j] > a[j+1] ) swap (a[j], a[j+1]); }
21
Prof. Amr Goneid, AUC21 Analysis of Bubble Sort
22
Prof. Amr Goneid, AUC22 Bubble Sort (a variant) void bubbleSort (itemType a[ ], int n) { int i, j; bool swapped; for (i = n-1; i >= 1; i--) { swapped = false; for (j = 0; j < i; j++ ) { if (a[j] > a[j+1] ) { swap(a[j], a[j+1]); swapped = true; } } if (!swapped) return; }
23
Prof. Amr Goneid, AUC23 Analysis of Bubble Sort Variant
24
Prof. Amr Goneid, AUC24 Performance of Bubble Sort The worst case complexity of the bubble sort is when the array is inversely sorted: O(n 2 ). Best case when the array is already sorted in ascending order: Ω(n). Outer loop works for only i = n-1 and there will be no swaps. In-Place SortYes Stable Algorithm Yes For small jobs, not efficient enough for large amounts of data.
25
Prof. Amr Goneid, AUC25 3. Insertion Sort The general idea of the insertion sort is that for each element, find the slot where it belongs. Performs successive scans through the data. When an element is out of sequence (less than its predecessor), it is pulled out and then inserted where it should belong. Array elements have to be shifted to the right to make space for the insertion.
26
Prof. Amr Goneid, AUC26 How it works 271586 127586 125786 123567 125678 123456 34 34 34 34 84 78
27
Prof. Amr Goneid, AUC27 Insertion Sort (code) void insertion (itemType a[ ], int n) { int i, j ;itemType v ; for (i =1; i < n; i++) { v = a[i]; j = i; while(j > 0 && a[j-1] > v) { a[j] = a[j-1]; j--; } a[j] = v; }
28
Prof. Amr Goneid, AUC28 http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/ISort.html http://coderaptors.com/?InsertionSortDemos
29
Prof. Amr Goneid, AUC29 Insertion Sort Algorithm InsertSort (a[0..n-1 ]) {for i =1 to n-1 { j = pointer to element a i ; v = copy of a i ; while( j > 0 && a j-1 > v) { move data right: a j ← a j-1 move pointer left: j-- } Insert v at last (j) location: a j ← v; }
30
Prof. Amr Goneid, AUC30 Analysis of Insertion Sort
31
Prof. Amr Goneid, AUC31 Performance of Insertion Sort The worst case complexity of the insertion sort is when the array is inversely sorted: O(n 2 ). Best case when the array is already sorted in ascending order: Ω(n). While loop will not execute and there will be no data movement. In-Place SortYes Stable Algorithm Yes For modest jobs, not efficient enough for large amounts of data.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.