implementation

A project report submitted in partial fulfillment of the requirements of Algorithms Design Course (ICS353) for the completion of degree of Bachelor of Science in Computer Engineering

The Computer Science Department

King Fahd University of Petroleum and Minerals

January, 2011

ABSTRACT

Sorting and selection is widely used by computer applications intermediary to finish tasks like searching. QUICKSELECT algorithm is known to be among fastest selection algorithms. It has linear time as average expected time to select. This report shows QUICKSELECT algorithm implemented with sample output provided.

INTRODUCTION

This report shows implementation of QUICKSELECT algorithm using C language on Linux platform. The code represents some algorithms mentioned in “Algorithms Design Techniques and Analysis” which is the textbook of ICS353 course.

METHODOLOGY

Algorithms: SPLIT and QUICKSELECT have been C implemented on Linux platform. Then, corresponding functions to finish testing of robustness of the implementation have been implemented. Functions are: swap(),printlist(), and main(). Function: split

int split(int list[], int low, int high){

int i, j, x, w;

i = low;

x = list[low];

for (j = low + 1; j<= high; j++){

if (list[j] <= x) {

i++;

if (i != j) swap(&list[i], &list[j]);

}

}

swap(&list[low], &list[i]);

w = i;

return w;

}

Function: quicksort

void quicksort(int list[], int low, int high){

int w;

if (low < high){

w = split(list, low, high);

quicksort(list, low, w-1);

quicksort(list, w+1, high);

}

}

Function: swap

void swap(int *x,int *y)

{

int temp;

temp = *x;

*x = *y;

*y = temp;

}

Function: printlist

void printlist(int list[],int high){

int i;

for(i=0;i<high;i++)

printf("%d\t",list[i]);

}

Function: quickselect

int quickselect(int numbers[], int low, int high, int rank) { if (low == high)

return numbers[low];

int split = partition(numbers,low,high);

if (rank == split-low+1)

return numbers[split];

else if (rank < split-low+1)

return quickselect(numbers, low, split-1, rank);

else

return quickselect(numbers, split+1, high, rank-(split-low+1)); }

Function: partition

int partition(int* vals, int low, int high) {

int temp;

int i, lowpos;

if (low == high) return low;

i = low + rand()%(high-low+1);

temp = vals[i];

vals[i] = vals[low];

vals[low] = temp;

lowpos = low;

low++;

while (low <= high) {

while (low <= high && vals[low] <= vals[lowpos]) low++;

while (high >= low && vals[high] > vals[lowpos]) high--;

if (low < high)

swap(&vals[low], &vals[high]);

}

swap(&vals[lowpos], &vals[high]);

return high;

}

Function: main

int main(){

const int MAX_ELEMENTS = 10;

int list[MAX_ELEMENTS];

int i = 0;

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

list[i] = 1 + (int) (100.0 * (rand() / (RAND_MAX + 1.0)));

}

printf("The list before sorting is:\n");

printlist(list,MAX_ELEMENTS);

// sort the list using quicksort

quicksort(list,0,MAX_ELEMENTS-1);

// print the result

printf("\nThe list after sorting using quicksort algorithm:\n");

printlist(list,MAX_ELEMENTS);

printf("\n");

return 0;

}

Sample Output:

> quickselect

Generated list of 25 elements is:

92 20 34 77 28 56 48 63 37 52 96 92 64 72 15 61 2 25 14 81 16 41 13 11 100 The 4th smallest element is: 14

The 7th smallest element is: 20

The 5th smallest element is: 15

The 8th smallest element is: 25

The list after sorting using quicksort algorithm:

2 11 13 14 15 16 20 25 28 34 37 41 ...