You are on page 1of 9

Sorting Algorithms

Haider Ali
http://faculty.ksu.edu.sa/haiderali
April 2009

Bubble Sort
• Divide list into sorted and unsorted sublists
• Smallest element in the unsorted sublist is
“bubbled” up into the sorted sublist
• Repeat until done:
– Compare adjacent pairs of records/nodes
– If the pair of nodes are out of order, exchange them,
and continue with the next pair of records/nodes
– If the pair is in order, ignore them and continue with
the next pair of nodes

Sorting Algorithms by Haider.ppt 1


Bubble Sort - Algorithm
void Sort(a[], N)
{

FOR P=1 to N-1


FOR i=0 TO N-P
if (a[i]>a[i+1])
swap(a[i],a[i+1]);
};
3

Bubble Sort – in C++


void Sort(int a[],int N)
{
int p, i;
for (p=0;p<N;p++)
for(i=0 ; i <N-p ; i++)
if (a[i]>a[i+1])
swap(a[i],a[i+1]);
};

void swap(int &x, int &y)


{
int tmp;
tmp=x;
x=y;
y=tmp;
}

Sorting Algorithms by Haider.ppt 2


Selection Sorts
• In each pass, the smallest item in the
unsorted sublist is placed at the begining
of the sorted sublist

Selection Sort
• Divide the list into two sublists: sorted and
unsorted, with the sorted sublist preceding
the unsorted sublist
• In each pass,
– Find the smallest item in the unsorted sublist
– Exchange the selected item with the first item
in the sorted sublist

Sorting Algorithms by Haider.ppt 3


Selection Sort - Algorithm
void selectionSort(int a[], int N)
{
int i, j, midIndex;

for (i = 0; i < (N - 1); i++)


{
minIndex = i;

// Find the index of the minimum element


for (int j = i + 1; j < N; j++)
if (a[j] < a[minIndex])
minIndex = j;
swap(a[i], a[minIndex]);
} 7
}

Insertion Sorts
• Commonly used by card players: As each
card is picked up, it is placed into the
proper sequence in their hand
• Divide the list into a sorted sublist and an
unsorted sublist
• In each pass, one or more pieces of data
are removed from the unsorted sublist and
inserted into their correct position in a
sorted sublist
8

Sorting Algorithms by Haider.ppt 4


Insertion Sort – in C++
void insertionSort(int a[], int N)
{
int p, i, k, position;
for(p=1;p<N;p++)
{
int tmp=a[p];
for (i=p; i > 0 && tmp < a[i-1]; i--) /* compare */
a[i] = a[i-1]; /*move */
a[i] = tmp; /* insert */
}
}
9

Merge-Sort
Merge-sort(S, n)
• Divide S into two sequences S1 and S2
• Recursively sort S1 and S2
• Merge S1 and S2 into a unique sorted
sequence

10

Sorting Algorithms by Haider.ppt 5


Merge Sort - Algorithm
mergeSort(S, N)
if S.size() > 1
(S1, S2) ← divide(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S ← merge(S1, S2)

11

Merge - Algorithm
Void merge(a[], left, mid, right)
{
for (i = mid+1; i > left; i--)
b[i-1] = a[i-1];
for (j = mid; j < right; j++)
b[right+mid-j] = a[j+1];

for (k = left; k <= right; k++)


if (ITEMless(b[i], b[j]))
a[k] = aux[i++];
else
a[k] = aux[j--];
}

12

Sorting Algorithms by Haider.ppt 6


Merge – in C++
void merge(int a[], int left, int mid, int for(i=left;i<right;i++)
right) {
{ if(l>mid-1)
int i, j, k, l, r; {
int b[11]; a[i]=b[r]; r++; continue;
}
// Copy Array to Temporary Array if(r>right)
for(i=left;i<=right;i++) {
b[i]=a[i]; a[i]=b[l]; l++; continue;
l=left; }
r=mid; if(b[l]<b[r])
{
a[i]=b[l]; l++;
}
else
{
a[i]=b[r]; r++;
} 13
}
};

Merge Sort – in C++


void sort(int a[], int left, int right)
{
int mid = (right + left) / 2;
if (right <= left)
return;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, mid, right);
}

14

Sorting Algorithms by Haider.ppt 7


Quick Sort
Quicksort(S)
{
if size of S is 0 or 1 then return;
pick a pivot v
divide S - {v} into S1 and S2 such that
• every element in S1 is < v
• every element in S2 is >= v
return { Quicksort(S1) v Quicksort(S2) }
}

15

Partition - Algorithm
int partition(a, left, right)
{
p = a[left]
L = left + 1,
R = right;
while (L < R)
{
while (L < right && a[L] < p)
L++;
while (R > left && a[R] >= p)
R--;
if (L<R)
swap(a[R],a[P])
}

a[left] = a[R];
a[R] = p;
}

16

Sorting Algorithms by Haider.ppt 8


Partition – in C++
int partition(int a[],int left, int right)
{
int p=a[left], L=left+1, R=right ;

while(l<=r)
{
while(L<right && a[L]<p) L++;
while (R>left && a[R]>=p) R--;

if(L<R)
swap(a[L],a[R]);
};
a[left]=a[R];
a[R]=p;
return R;
};
17

Quick Sort – in C++


void quicksort(int a[],int left, int right)
{
int i;
if (right<=left) // Base Criteria
return;

i=partition(a,left,right);

quicksort(a,left,i-1); // Sort Left Side List


quicksort(a,i+1,right); // Sort Right Side List
};
18

Sorting Algorithms by Haider.ppt 9

You might also like