Internal Sorting

 

Objectives

The sorting Problem

Definition. Given a set of records r1,r2,r3,...,rn with key values k1,k2,k3,...,kn, arrange the records into sequence ri1,ri2,ri3,...,rin such that the keys are ordered, that is ki1<=ki2<=ki3<=...<=kin.

Example to be used:

5, 10, 7, 12, 4, 9, 6, 3, 15, 11

Quadratic Time Sorting

  1. Insertion Sort

The idea:

 

void InsSort(Element* array, int n){

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

for(int j=i;

(j>0)&&(array[j]<array[j-1]); j--)

swap(array[j], array[j-1]);

}

}

Analysis:

Comparisons:

Swaps:

The idea:

 

void BubbSort(Element* array, int n){

for (int i=0; i<n-1; i++)

for (int j=n-1; j>i; j--)

if (array[j] < array[j-1])

swap(array[j], array[j-1]);

}

Analysis:

Comparisons:

Swaps:

The idea:

 

void SelSort(Element* array, int n){

for (int i=0; i<n-1; i++){

int low = i;

for (int j=n-1; j>i; j--)

if (array[j]<array[low])

low = j;

swap(array[i], array[low]);

}

}

Analysis:

Comparisons:

Swaps:

Comparing quadratic sort algorithms

Insertion Bubble Selection

Comparisons:

Best n n2 n2

Average n2 n2 n2

Worst n2 n2 n2

 

Swaps:

Best 0 0 n

Average n2 n2 n

Worst n2 n2 n

 

Fast Internal Sorting

  1. Shellsort

The idea:

 

void ShellSort(Element* array, int n){

for (int i=n/2; i>2; i/=2)

for (int j=0; j<i; j++)

VarInsertSort(&array[j],n-j,i);

VarInsertSort(array,n,1);

}

void InsSort(Element *array, int n, int inc){

for (int i=inc; i<n; i+=inc)

for (int j=i; (j>=inc)&&

(array[j]<array[j-inc]); j-=inc)

swap(array[j], array[j-inc]);

}

The idea:

 

void QSort(Element* array, int i, int j){

int pivot = getpivot(i,j);

swap(array[pivot],array[j]);

int k = partition(array,i-1,j,array[j]);

swap(array[k],array[j]);

if ((k-i) > 1) QSort(array,i,k-1);

if ((j-k) > 1) Quicksort(k+1,j);

}

int part(Element* array, int l, int r, int p){

do {

while (array[++l] < pivot);

while (r && array[--r] > pivot);

swap(array[l], array [r]);

} while (l<r);

swap(array[l], array[r]);

return l;

}

 

Improvements for Quicksort

The idea:

void MSort(Element* array, int l, int r){

Element* temp = new Element[r - l +1];

if (l == r)

return;

else{

int mid=(left+right)/2;

MergeSort(array,left,mid);

MergrSort(array,mid+1,right);

merge_halves(array,r-l+1);

}

}

  1. Heapsort

The idea:

void HeapSort(Element* array, int n){

heap H(array, n, n);

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

H.removemax();

}

 

Non-comparison sorting

  1. Binsort

The idea:

void Binsort(Element* array, int n){

list Bin[MaxKey];

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

Bin[array[i]].append(array[i]);

for (int i=0; i<MaxKey; i++)

for (Bin[i].first(); Bin[i].isLast();

Bin[i].next())

output(B[i].currValue());

}

void RadSort(Element *A, Element B*,

int n, int k, int r, Element * count){

for (int i=0, rtok = 1; i<k; i++, rtok *= r)

for (int j=0; j<r; j++)

count[j] = 0;

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

count[(A[j]/rtok)%r]++;

for (int j=1; j<r; j++)

count[j] = count[j-1] + count[j];

for (int j=n-1; j>=0; j--)

B[--count[(A[j]/rtok)%r]] = A[j];

for(int j=0; j<n; j++) A[j] = B[j];

}

}

Lower Bounds on Sorting

Theorem. No sorting algorithm based on key comparison can be faster than W(n) in the worst case

Proof. The idea: