Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 210 Data Structures and Algorithms

Similar presentations


Presentation on theme: "CSCE 210 Data Structures and Algorithms"— Presentation transcript:

1 CSCE 210 Data Structures and Algorithms
Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms Prof. Amr Goneid, AUC

2 Sorting(1): Elementary Algorithms
General Selection Sort Bubble Sort Insertion Sort Prof. Amr Goneid, AUC

3 General The Sorting Problem:
Input: A sequence of keys {a1 , a2 , …, an} 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. Prof. Amr Goneid, AUC

4 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. Prof. Amr Goneid, AUC

5 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 kth 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. Prof. Amr Goneid, AUC

6 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. Prof. Amr Goneid, AUC

7 General We compare sorting methods on the bases of:
The time complexity of the algorithm 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(n2) Prof. Amr Goneid, AUC

8 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]). Prof. Amr Goneid, AUC

9 How it works 4 3 1 6 2 5 1 3 4 6 2 5 1 2 4 6 3 5 1 2 3 6 4 5 1 2 3 4 6 5 1 2 3 4 5 6 Prof. Amr Goneid, AUC

10 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]); } Prof. Amr Goneid, AUC

11 Demos Selection Sort Demo Prof. Amr Goneid, AUC

12 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]); } Prof. Amr Goneid, AUC

13 Analysis of Selection Sort
Prof. Amr Goneid, AUC

14 Performance of Selection Sort
The complexity of the selection sort is  (n2), independent of the initial data ordering. In-Place Sort Yes Stable Algorithm No This technique is satisfactory for small jobs, not efficient enough for large amounts of data. Prof. Amr Goneid, AUC

15 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 Prof. Amr Goneid, AUC

16 Bubble Sort Algorithm for each i = n-1…1
Compare adjacent elements aj and aj+1 , j = 0..i-1 and swap them if aj > aj+1 This will bubble the largest element in the sub-array a[0..i] to location (i). Prof. Amr Goneid, AUC

17 How it works 4 3 1 2 3 4 1 2 3 1 4 2 3 1 2 4 1 3 2 4 1 2 3 4 Prof. Amr Goneid, AUC

18 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]); } Prof. Amr Goneid, AUC

19 Demos Bubble Sort Demo Prof. Amr Goneid, AUC

20 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]); } Prof. Amr Goneid, AUC

21 Analysis of Bubble Sort
Prof. Amr Goneid, AUC

22 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; Prof. Amr Goneid, AUC

23 Analysis of Bubble Sort Variant
Prof. Amr Goneid, AUC

24 Performance of Bubble Sort
The worst case complexity of the bubble sort is when the array is inversely sorted: O(n2). 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 Sort Yes Stable Algorithm Yes For small jobs, not efficient enough for large amounts of data. Prof. Amr Goneid, AUC

25 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. Prof. Amr Goneid, AUC

26 How it works 2 7 1 5 8 6 3 4 1 2 7 5 8 6 3 4 1 2 5 7 8 6 3 4 1 2 5 6 7 8 3 4 1 2 3 5 6 7 8 4 1 2 3 4 5 6 7 8 Prof. Amr Goneid, AUC

27 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; } Prof. Amr Goneid, AUC

28 Demos Insert Sort Demo Prof. Amr Goneid, AUC

29 Insertion Sort Algorithm
InsertSort (a[0..n-1 ]) { for i =1 to n-1 { j = pointer to element ai; v = copy of ai; while( j > 0 && aj-1 > v) { move data right: aj ← aj-1 move pointer left: j-- } Insert v at last (j) location: aj ← v; } Prof. Amr Goneid, AUC

30 Analysis of Insertion Sort
Prof. Amr Goneid, AUC

31 Performance of Insertion Sort
The worst case complexity of the insertion sort is when the array is inversely sorted: O(n2). 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 Sort Yes Stable Algorithm Yes For modest jobs, not efficient enough for large amounts of data. Prof. Amr Goneid, AUC


Download ppt "CSCE 210 Data Structures and Algorithms"

Similar presentations


Ads by Google