24
3 – SIMPLE SORTING ALGORITHMS

3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

Embed Size (px)

Citation preview

Page 1: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

3 – SIMPLE SORTING ALGORITHMS

Page 2: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

SORTING TECHNIQUES

We need to do sorting for the following reasons :

a)By keeping a data file sorted, we can do binary search on it.

a)Doing certain operations, like matching data in two different files, become much faster.

Page 3: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

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:

Page 4: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

BUBBLE SORTA 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

Page 5: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

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

Page 6: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

BUBBLE SORT

Bubble sort: beginning of first pass

Page 7: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

BUBBLE SORT

Page 8: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

BUBBLE SORT

Bubble sort: end of First pass

Page 9: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

Code for BUBBLE SORTvoid 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;

}

Page 10: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

10

Example of bubble sort

7 2 8 5 4

2 7 8 5 4

2 7 8 5 4

2 7 5 8 4

2 7 5 4 8

2 7 5 4 8

2 5 7 4 8

2 5 4 7 8

2 7 5 4 8

2 5 4 7 8

2 4 5 7 8

2 5 4 7 8

2 4 5 7 8

2 4 5 7 8

(done)

Page 11: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

11

Analysis of bubble sortfor (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)

Page 12: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

SELECTION SORT

Selection sort is a simplicity sorting algorithm. It works as its name as it is.

1. Find the minimum element in the list2. Swap it with the element in the first position

of the list3. Repeat the steps above for all remainder

elements of the list starting at the second position.

Page 13: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

SELECTION SORT

Page 14: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

SELECTION SORT

Page 15: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

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

Page 16: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

SELECTION SORTvoid 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

Page 17: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

17

Example and analysis of selection sort

• Analysis:– 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

Page 18: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

INSERTION SORTUsed for data that is already sorted or almost sortedAppend 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

Page 19: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

INSERTION SORT

Page 20: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

INSERTION SORT

Page 21: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

Code for INSERTION SORT

Page 22: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

22

One step of insertion sort

3 4 7 12 14 14 20 21 33 38 10 55 9 23 28 16

sorted next to be inserted

3 4 7 55 9 23 28 16

10

temp

3833212014141210

sorted

less than 10

Page 23: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

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)

Page 24: 3 – SIMPLE SORTING ALGORITHMS. SORTING TECHNIQUES We need to do sorting for the following reasons : a)By keeping a data file sorted, we can do binary

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