Professional Documents
Culture Documents
Sorting
Sorting
A process that organizes a collection of data
into either ascending or descending order.
Can be used as a first step for searching the
data.
Binary Search required a sorted array.
Sorting Algorithms
Selection Sort
Insertion Sort
Bubble Sort
Quick Sort
Merge Sort
Heap Sort
Summary of Algorithms
unsorted.
At each step, take the first item from
need to be compared.
Bubble Sort
void bubbleSort (int a[ ], int n)
{
int i, j, temp, flag;
for(i=n-1; i>0; i- -)
{
flag = 1;
for(j=0; i>j; j++)
{
if(a[j]>a[j+1])
{
flag = 0;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
} //out this block when flag is true, i.e. inner loop
performed no swaps, so the list is already sorted
if(flag)
break;
}
}
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6,
Bubblesort
when 9,
Bubblesortcompares
2,
comparesthe
12,
thenumbers
numbersininpairs
11, 9, 3, 7
pairsfrom
fromleft
lefttotoright
rightexchanging
when necessary. Here the first number is compared to the secondand
necessary.
isislarger
Here the first number is compared to the
exchanging
second andasasitit
largerthey
theyare
areexchanged.
exchanged.
Now
so 6,
Nowthe
this pair2,
thenext
nextpair
is also 9,
pairofofnumbers
exchanged.
so this pair is also exchanged. 12,
numbersare
11, 9, 3, 7
arecompared.
compared. Again
Againthethe99isisthe
thelarger
largerand
and
In
6,
Inthe
thethird
made.
made. We 2,
thirdcomparison,
Wemove
moveon 9,
comparison,the
ontotocompare
comparethe 12,
the99isisnot
notlarger
thenext
nextpair 11, 9, 3, 7
largerthan
thanthe
the12
pairwithout
12so
withoutany
sono
noexchange
exchangeisis
anychange
changetotothe
thelist.
list.
The6,
The12 2,
12isislarger
largerthan 9,
thanthe
the11
11so 11,
sothey
theyare 12, 9, 3, 7
areexchanged.
exchanged.
The
Theend
The
6,
Thetwelve
endofofthe
thelist
2,
twelveisisgreater
listhas
hasbeen
9,
greaterthan
thanthe
beenreached
reachedso
11,
the99sosothey
sothis
9, 12, 3, 7
theyare
thisisisthe
areexchanged
theend
exchanged
endofofthe
thefirst
firstpass.
pass. The
Thetwelve
twelveatatthe
the
6, 2, 9, 11, 9, 3, 12, 7
end
endofofthe
thelist
listmust
mustbe belargest
largestnumber
numberininthe thelist
listand
andso
soisisnow
nowininthe
thecorrect
correctposition.
position.
We now The
start12a is
newgreater
pass than
from the
left 3
to so they
right. are exchanged.
We nowThe start12a is
newgreater than the
pass from left 3tosoright.
they are exchanged.
The
6,
The12 2,
12isisgreater
greaterthan 9,
thanthe
the77so 11,
sothey
theyare 9, 3, 7, 12
areexchanged.
exchanged.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
6, 6,
2, 2, 9, 9,
11, 3,
11,
9, 7,
11,
3, 11,
7, 12
Notice
Noticethat
thatthis
thistime
timewe
wedodonot
nothave
havetotocompare
comparethe
thelast
lasttwo
twonumbers
numbersas
aswe
we
know the 12 is in position. This pass therefore only requires 6 comparisons.
know the 12 is in position. This pass therefore only requires 6 comparisons.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3,
9, 7,
3, 9,
9, 7, 11, 12
This
Thistime
timethe
the11
11and
and12
12are
areininposition.
position. This
Thispass
passtherefore
thereforeonly
onlyrequires
requires55
comparisons.
comparisons.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 9,
3, 3, 9,
9,
7, 7, 9, 11, 12
Each
Eachpass
passrequires
requiresfewer
fewercomparisons.
comparisons. This
Thistime
timeonly
only44are
areneeded.
needed.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
The
Thelist
listisisnow
nowsorted
sortedbut
butthe
thealgorithm
algorithmdoes
doesnot
notknow
knowthis
thisuntil
untilititcompletes
completesaa
pass
passwith
withno noexchanges.
exchanges.
Merge Sort
Merge sort is a sorting algorithm for
rearranging lists (or any other data
structure that can only be accessed
sequentially) into a specified order.
It is a particularly good example of the
}
void combine(int A[10], int low, int mid, int high )
{
int i,j,k;
int temp[10]; //this array is used to merge the two arrays in one array
k= low;
i= low;
j= mid+1;
while( i <= mid && j <= high)
{
if ( A[i] <= A[j] ) //if the number in one array is greater than the number in the second array then swapping
{
temp[k] = A[i];
i++;
k++;
}
else
{
temp[k] = A[j];
j++;
k++;
}
}
while ( i <= mid){
temp[k] = A[i];
i++;
k++;
}
while ( j < = high){
temp[k] = A[j];
j++;
k++;
}
for ( k = low ; k < = high ; k++)
A[k] = temp [k];
}
Quick Sort
Quicksort sorts by employing a divide and
conquer strategy to divide a list into two sub-
lists.
The steps are:
Pick an element, called a pivot, from the list.
Reorder the list so that all elements which are
less than the pivot come before the pivot and
so that all elements greater than the pivot
come after it (equal values can go either way).
After this partitioning, the pivot is in its final
position. This is called the partition operation.
Recursively sort the sub-list of lesser elements
and the sub-list of greater elements.
Quick Sort
Choose the appropriate pivot, either
randomly or near the median of the array
elements.
Avoid a pivot which makes either of the two
halves empty.