Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 – SIMPLE SORTING ALGORITHMS

Similar presentations


Presentation on theme: "3 – SIMPLE SORTING ALGORITHMS"— Presentation transcript:

1 3 – SIMPLE SORTING ALGORITHMS

2 SORTING TECHNIQUES We need to do sorting for the following reasons :
By keeping a data file sorted, we can do binary search on it. Doing certain operations, like matching data in two different files, become much faster.

3 SORTING TECHNIQUES There are various methods for sorting: Bubble sort
Insertion sort Selection sort Quick sort Heap sort Merge sort, etc. They have different average and worst case behaviours:

4 BUBBLE SORT A simple sorting technique in which we arrange elements of the list by forming pairs of adjacent elements. That means we form the pair of the ith and (i+1)th element. Swap elements of the pair if first element is greater than second element. Continue down the list this way until you reach the top end of the list this puts the largest item at the top end Repeat procedure for the remaining n-1 elements, and then for remaining n-2 elements, and so on

5 BUBBLE SORT Continue this process until all the data items are in order This is why it’s called the bubble sort: As the algorithm progresses, the biggest items “bubble up” to the top end of the array

6 BUBBLE SORT Bubble sort: beginning of first pass

7 BUBBLE SORT

8 BUBBLE SORT Bubble sort: end of First pass

9 Code for BUBBLE SORT void bsort(int list[], int n) {
int outer, inner, temp; for(outer=n-1; outer>0; outer--) ‘counting down for(inner=0; inner<outer; inner++) ‘bubbling up if(list[inner] > list[inner+1]) ‘if out of order temp = list[inner]; ‘then Swap list[inner] = list[inner + 1]; list[inner + 1] = temp; }

10 Example of bubble sort 7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done) 2 7 5 8 4 2 5 4 7 8 2 7 5 4 8

11 Analysis of bubble sort
for (outer=n-1; outer>0; outer--) for (inner=0; inner<outer; inner++) if (list[inner] > list[inner+1]) // code for swap omitted Let n = size of the array The outer loop is executed n-1 times (call it n, that’s close enough) Each time the outer loop is executed, the inner loop is executed Inner loop executes n-1 times at first, linearly dropping to just once On average, inner loop executes about n/2 times for each execution of the outer loop In the inner loop, the comparison is always done (constant time), the swap might be done (also constant time) Result is n * n/2 * k, that is, O(n2/2 + k) = O(n2)

12 SELECTION SORT Selection sort is a simplicity sorting algorithm. It works as its name as it is. 1. Find the minimum element in the list 2. Swap it with the element in the first position of the list 3. Repeat the steps above for all remainder elements of the list starting at the second position.

13 SELECTION SORT

14 SELECTION SORT

15 Selection sort Given an array of length n,
Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 Search elements 3 through n-1 and select the smallest Swap it with the element in location 3 Continue in this fashion until there’s nothing left to search

16 SELECTION SORT void selection_sort(int list[], int n){ int i, j, min;
for (i = 0; i < n - 1; i++) { min = i; for (j = i+1; j < n; j++) { if (list[j] < list[min]) { min = j; } swap(list[i], list[min]); The index of the minimum value is stored in min. At end of N-1 comparisons, value pointed to by min is swap with current position

17 Example and analysis of selection sort
The outer loop executes n-1 times The inner loop executes about n/2 times on average (from n to 2 times) Work done in the inner loop is constant (swap two array elements) Time required is roughly (n-1)*(n/2) You should recognize this as O(n2) 7 2 8 5 4 2 7 8 5 4 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8

18 INSERTION SORT Used for data that is already sorted or almost sorted
Append data to be sorted to sorted data and then sort Basic Idea: Insert an element into a sequence of ordered elements of size i , such that, the resulting sequence of size i+1 is also ordered This means: Find the element’s proper place in the sorted data Make room for the element to be inserted (by shifting over other elements) Insert the element

19 INSERTION SORT

20 INSERTION SORT

21 Code for INSERTION SORT

22 One step of insertion sort
3 4 7 12 14 20 21 33 38 10 55 9 23 28 16 sorted next to be inserted 10 temp less than 10 12 14 14 20 21 33 38 10 3 4 7 55 9 23 28 16 sorted

23 Analysis of insertion sort
We run once through the outer loop, inserting each of n elements; this is a factor of n On average, there are n/2 elements already sorted An inner loop looks at (and moves) half of these This gives a second factor of n/4 Hence, the time required for an insertion sort of an array of n elements is proportional to n2/4 Discarding constants, we find that insertion sort is O(n2)

24 Summary Bubble sort, selection sort, and insertion sort are all O(n2)
As we will see later, we can do much better than this with somewhat more complicated sorting algorithms Within O(n2), Bubble sort is very slow, and should probably never be used for anything Selection sort is intermediate in speed Insertion sort is usually the fastest of the three--in fact, for small arrays (say, 10 or 15 elements), insertion sort is faster than more complicated sorting algorithms Selection sort and insertion sort are “good enough” for small arrays


Download ppt "3 – SIMPLE SORTING ALGORITHMS"

Similar presentations


Ads by Google