31
1 Sorting

5.Sorting

Embed Size (px)

DESCRIPTION

sortings

Citation preview

1

Sorting

2

Sorting

Definition

Rearranging data in an ordered fashion .

Organizing data in sorted manner helps in

searching. For example, finding address from

a telephone directory becomes easier if data

is arranged in a sorted manner

3

Types of Sorting Methods

• Sort based on exchange ::

This involves exchanging elements depending on some conditions. Examples-

bubble sort

quick sort

selection sort

• Sort based on tree ::

It involves selection of successive elements & placing them in proper sorted positions

heap sort

Discussed in Trees part

4

• Sort based on insertion in the sorted list ::

This method involves inserting the element at correct position in the group of elements

insertion sort

• Merging two sorted lists

In this method two or more sorted data groups are combined to form a third data group.

merge sort

5

Bubble Sort

METHOD

• Scan the list, exchanging adjacent elements if

they are not in relative order; this bubbles the

highest value to the top

• scan the list again, bubbling up the second

highest value

• repeat until all elements have been placed in

their proper order

7

void sort(int array[], int n)

{

int temp = 0,j,pass;

for( pass=0;pass<n;pass++)

for( j=0;j<(n-pass-1);j++)

if(array[j]>array[j+1])

{

temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;

}

}

8

Selection Sort

• Assume the first element in the array be the smallest(or largest based on sort direction) value in the array.

• Loop through the rest of the elements and if there is a element smaller than the smallest value assign that element as the smallest value.

• At the end of the loop exchange the positions of the smallest value with the first element.

• Now we have the smallest element right at the top.

• Repeat the same process continuing from the second element until you reach the last element.

METHOD

9

45

30

77

20

45

30

77

20

20

30

77

45

20

30

77

45

20

30

77

45

45

30

77

20

The circled number is min

20

30

77

45

20

30

77

45

20

30

45

77

20

30

77

45

One notable improvement of this over bubble sort is that the exchange happens at most only once in an iteration.

Have you already finished coding for this?

10

void selsort(int a[],int n) {

int i, j, min,temp,

for(i=0;i<n;i++){

min = i;

for( j = i+1;j<n;j++)

if(a[min]>a[j]) min = j;

temp=a[min];

a[min]=a[i];

a[i]=temp;

}

} gets the position of smallest number

11

Insertion Sort

This sorting method must be used when we want to insert an element into a sorted array.

METHOD:

• Consider a sub-array of length 1 containing the first

element. Since there is only one element it is already

sorted.

• Next insert the second item into the sorted array created

above, shifting the first element if needed

• Similarly insert the third item into the sorted array, shifting

the other elements if necessary.

• Continue this process until all values have been inserted

in the place.

12

45

30

77

20

sub-list

45

30

77

20

45

30

77

20

30

45

77

20

30

45

77

20

Insert 30

into sub-list

Insert 77

into sub-list

30

45

77

20

Slide 45 one

down. Insert

30

Insert 20

into sub-list

20

30

45

77

Slide 77, 45,30 one

down. Insert 20

30

30

45

77

20

77 If the number going to be inserted

is > than it’s the last element of

sub-list, leave it as it is and move

on to next iteration.

13

void sort(int a[], int n) {

int temp,i,j;

for (i = 1; i < n; i++) {

temp = a[i];

while (i > 0 && a[i - 1] > temp) {

a[i] = a[i - 1];

i--;

}

a[i] = temp;

}

}

slide elements down to make room

Since the sub-list already has one

elements we start from 1st index.

14

Divide and Conquer paradigm

• Divide and conquer paradigm is a technique adopted to solve a problem. The general approach is:

a) Divide a problem into sub-problems.

b) Solve each sub-problem separately.

c) Combine the solutions of the original problem.

• The next two sorting approaches we will be discussing use this strategy.

• This strategy is simplest if recursions are used.

15

Quick Sort

•Very efficient sorting algorithm

•Strategy used here is divide & conquer

SIMPLIFIED VIEW:

•From the array select an element and let us call it

the pivot .

•Arrange elements such that all elements less than

the pivot are to its left forming sub-array 1 and all

greater are to its right forming sub-array 2.

•Continue the same process for the sub-array 1 and

sub-array 2.

16

How to choose a pivot • first element

• bad if input is sorted or in reverse sorted order

• bad if input is nearly sorted

• variation: particular element (e.g. middle element)

• random element

• even a malicious agent cannot arrange a bad input

• median of three elements

• choose the median of the left, right, and center elements

17

METHOD

1. Let a be an array of size n which needs to be

sorted.

2. min=0, max=n-1

3. If min>=max stop.

4. Select a first element as pivot element.

5. Move the pivot to the rightmost position.

6. Starting from the left, find an element pivot.

Call the position i.

7. Starting from the right, find an element

pivot. Call the position j.

18

Cont...

8. Exchange a[i] and a[j]

9. Continue 6 to 8 as long as i>=j.

10. When i>=j, restore the pivot => swap a[i] and

a[max]

11. Split the list into two: (min,i-1) and (i+1, max)

and sort them separately - apply steps 3 to 11.

19

5 2 9 1 3 min:0 max:4

i=-1

3 2 9 1 5

j=4

move pivot 5 to end

Move i till you reach

i>=j or a[i]>pivot

i=2

3 2 9 1 5

j=4

Move j till you reach

i>=j or a[j]<pivot

i=2

3 2 9 1 5

j=3

Exchange

i=2

3 2 1 9 5

j=3

If i<j continue to move

i till you reach

i>=j or a[i]>pivot

3 2 1 9 5

i=j=3

20

3 2 1 9 5

i=3

Move j till you reach

i>=j or a[j]<pivot

j=2

If i<j NO. Restore pivot in i

And split at i 3 2 1 5 9

i=3 j=2

5 3 2 1 9 min:0

max:2

move

pivot 3

to end

1 2 3

Move i till i>=j

or a[i]>pivot

i=-1 j=2

1 2 3

i=j=2

Move j till i>=j

or a[j]<pivot 1 2 3

j=1 i=2

If i<j

NO. Restore 1 2 3

min:0

max:0

Since min>=max : STOP

21

1 2 5 9 3 min:0

max:1

move pivot 2 1

i=-1 j=1

Move i till i>=j

or a[i]>pivot

2 1

i=1 j=1

Move j till i>=j

or a[j]<pivot

2 1

i=j=1

1 2

1 2 3 5 9

If i<j

NO. Restore

Final sorted list

22

void qSort(int a[], int min, int max){

int k,mid,pivot;

int len=max;

if (min >= max) return;

pivot = a[min];

swap(a, min, max);

for(k=0;k<len;k++)

mid = split(a, min, max - 1, pivot);

swap(a, mid, max);

qSort(a, min, mid - 1);

qSort(a, mid + 1, max);

}

Stop condition

select the first element as pivot

move pivot to end

split the array after the necessary

swaps. Split function does this.

restore the pivot to its proper location

recursively sort the left

and right partitions

23

void swap(int a[], int i, int j){

int temp;

temp=a[i];

a[i]=a[j];

a[j]=temp; }

int split(int a[], int i, int j, int pivot){

i--; j++;

while (1) {

do { i++; } while (i < j && a[i] < pivot);

do { j--; } while (i < j && a[j] > pivot);

if (i >= j) break;

else swap(a, i, j); }

return i;

}

adjust because the loops pre-increment

move index markers i, j toward centre until we

find a pair of mis-partitioned elements Split position

24

Merge Sort

• Strategy used is divide & conquer

METHOD

Splitting Phase

• Split the array into two equal( or nearly equal) parts.

• Continue splitting until each part each contains only

one element.

Merging Phase

• merge the two parts containing one element into one

sorted list

• continue merging parts until finally there is only one

list

25

24 43 23 15 33 12

24 43 23 15 33 12

24 43 23

43 23 24 15 33 12

12 15 33

24 43 23 12

23 24 43

15 33

12 15 33

splitting

merging

12 15 23 24 33 43 append left

over elements No more elements : list exhausted

26

void mergesort(int array[],int lowerbound,int

upperbound){

if (lowerbound==upperbound) return;

else {

int mid=(lowerbound+upperbound)/2;

mergesort(a,lowerbound,mid);

mergesort(a,mid+1,upperbound);

merge(a,lowerbound,mid,upperbound);

}

recursively keep splitting the array

until there is only one element

Merge the

split arrays

Get the mid

of the array

1 element

27

void merge(int array[], int lowPtr,int highPtr,

int upperBound)

{

int j = 0;

int temp[10];

int lowerBound = lowPtr;

int mid = highPtr-1;

int n = upperBound-lowerBound+1;

while(lowPtr <= mid && highPtr <= upperBound)

if(array[lowPtr] < array[highPtr] )

temp[j++] = array[lowPtr++];

else

temp[j++] = array[highPtr++];

find the smallest elements from both the arrays and populate the

temp array. Continue until one of the arrays is exhausted.

28

while(lowPtr <= mid)

temp[j++] = array[lowPtr++];

while(highPtr <= upperBound)

temp[j++] = array[highPtr++];

for(j=0; j<n; j++)

array[lowerBound+j] = temp[j];

}

Append the rest of the temp array with the elements of

the array for which there are elements left over.

Copy the sorted array ‘temp’ into the original array ‘array’

29

Comparing performance of

sorting algorithms

30

O(n2) sorting algorithms Bubble Sort

Selection Sort

Insertion Sort

Empirical Analysis

slowest

fastest

31

O(n log n) sorting algorithms Heap Sort

Merge Sort

Quick Sort

Empirical Analysis

slowest

fastest