You are on page 1of 31

Sorting

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

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
3

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

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

Round 1
45
30
77
20

30
45
77
20

30
45
77
20

Round 2
30
45
20
77

30
45
20
77

30
45
20
77
Round 3

30
20
45
77

30
20
45
77

20
30
45
77

Can you write the algorithm for this ?


6

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;
}
}
7

Selection Sort
METHOD
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.
8

45
30
77
20
20
30
77
45

45
30
77
20
20
30
77
45

45
30
77
20
20
30
77
45

20
30
77
45

The circled number is min

20
30
77
45

20
30
77
45

20
30
45
77

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?

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


10

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.
11

sub-list
45
30
77
20

30
45
77
20

30
45
77
20

45
30
77
20

45
30
30
77 Insert 30
20 into sub-list

77
Insert 77
into sub-list

Insert 20
into sub-list

30
45
77
20

20
30
45
77

30
45
77
20

Slide 45 one
down. Insert
30

If the number going to be inserted


is > than its the last element of
sub-list, leave it as it is and move
on to next iteration.

Slide 77, 45,30 one


down. Insert 20

12

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


Since the sub-list already has one
int temp,i,j;
elements we start from 1st index.
for (i = 1; i < n; i++) {
temp = a[i];
while (i > 0 && a[i - 1] > temp) {
a[i] = a[i - 1];
i--;
}
slide elements down to make room
a[i] = temp;
}
}
13

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.
14

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.
15

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
16

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.
17

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.

18

min:0 max:4

move pivot 5 to end

i=-1
Move i till you reach
i>=j or a[i]>pivot

j=4

i=2

Move j till you reach


i>=j or a[j]<pivot
Exchange

If i<j continue to move


i till you reach
i>=j or a[i]>pivot

j=4
1

i=2

j=3

i=2
1

j=3
9
i=j=3

5
19

Move j till you reach


i>=j or a[j]<pivot

If i<j NO. Restore pivot in i


And split at i

min:0
max:2
move
pivot 3
to end

i=-1

j=2

i=3

j=2

i=3

min:0
max:0

Since min>=max : STOP

j=2

Move i till i>=j 1


or a[i]>pivot
Move j till i>=j 1
or a[j]<pivot
If i<j
NO. Restore

i=j=2
2
3
j=1
i=2
2

20

min:0
max:1
move pivot

1
j=1

i=1

j=1

i=-1
Move i till i>=j
or a[i]>pivot

Move j till i>=j


or a[j]<pivot
If i<j
NO. Restore

i=j=1
1

Final sorted list


21

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


int k,mid,pivot;
int len=max;
if (min >= max) return;
Stop condition

pivot = a[min];
swap(a, min, max);

select the first element as pivot


move pivot to end

split the array after the necessary


swaps. Split function does this.

for(k=0;k<len;k++)
mid = split(a, min, max - 1, pivot);

swap(a, mid, max); restore the pivot to its proper location


qSort(a, min, mid - 1);
qSort(a, mid + 1, max);
}

recursively sort the left


and right partitions
22

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++;
adjust because the loops pre-increment
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); }
move index markers i, j toward centre until we
return i;
} Split position find a pair of mis-partitioned elements
23

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
24

24
24

43

43

23

15

23

33

12

15

33

15

33
33

12

splitting

24

43

23

24

43

23

15

24

43

23

15

12

33

12
12

merging
23
append left
over elements

24

43
12

12

15

15
23
24
33
43
No more elements : list exhausted

33

25

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


upperbound){
if (lowerbound==upperbound) return; 1 element
else {

int mid=(lowerbound+upperbound)/2; Get the mid


of the array
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

26

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
if(array[lowPtr]
temp[j++] =
else
temp[j++] =

&& highPtr <= upperBound)


< array[highPtr] )
array[lowPtr++];
array[highPtr++];

find the smallest elements from both the arrays and populate the
temp array. Continue until one of the arrays is exhausted.
27

Append the rest of the temp array with the elements of


the array for which there are elements left over.
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];
}

Copy the sorted array temp into the original array array

28

Comparing performance of
sorting algorithms

29

O(n2) sorting algorithms


Bubble Sort
Selection Sort
Insertion Sort

slowest
fastest

Empirical Analysis

30

O(n log n) sorting algorithms


Heap Sort
Merge Sort
Quick Sort

slowest
fastest

Empirical Analysis

31

You might also like