//Yap Hui Wen //99006448 //Data Structures 2 Assignment 3 //Lecturer: Mr. Elsadig /*********************************************************************************************/ /*Write a C++ program that computes the time required to sort a randomly generated list using*/ /*Quick Sort and Binary Search Tree Sort */ /*-Generate the largest possible list of random float values */ /*-Sort the list using BST & QS */ /*-Compute the time needed in each case */ /*Write a short note */ /*********************************************************************************************/ #include #include #include const int MAX=6000; //maximum list size typedef float DataType; struct node{ DataType data; node* LChild; node* RChild; }; /******************************************************************/ template void quickSort(ElementType x[], int first, int last) { int pos; //position of pivot if(first void Split(ElementType y[],int first, int last, int &pos) { ElementType pivot=y[first]; //pivot element int left=first; //index for left search bool onCorrectSide; ++first; do{ onCorrectSide=true; while (onCorrectSide){ //move from left to right if(y[first]>pivot) onCorrectSide=false; else{ ++first; onCorrectSide=(first<=last); } } onCorrectSide=(first<=last); while(onCorrectSide){ //move from right to left if(y[last]<=pivot) onCorrectSide=false; else{ --last; onCorrectSide=(first<=last); } } if(first void Swap(ElementType &a,ElementType &b) { ElementType temp=a; a=b; b=temp; } /******************************************************************/ template void exchange (DataType a[], int i, int j) { DataType temp=a[i]; a[i]=a[j]; a[j]=temp; } /******************************************************************/ int insert (node* &tree, DataType num) { if (tree == NULL) //tree is empty { tree = new node; tree->LChild = NULL; tree->RChild= NULL; tree->data= num; return 0; } else if (num == tree->data) return insert(tree->LChild,num); // tree is not empty else if (num <= tree->data) return insert(tree->LChild,num); else return insert(tree->RChild,num); } /******************************************************************/ void Inorder(node* tree) { if(tree!=NULL) { Inorder(tree->LChild); Inorder(tree->RChild); } } /******************************************************************/ int main() { clock_t QS1, QS2, BST1, BST2; //variable for computing time used DataType a[MAX]; //array for quick sorting DataType value; node* tree=NULL; for(int j=0;j