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)