0% found this document useful (0 votes)
6 views

13 - Merge & Quick Sort

The document defines 6 functions: read data into an array, print an array, fill an array with random numbers, merge two sorted arrays, implement merge sort, and implement quick sort. It then tests these functions by filling an array randomly, printing it unsorted, sorting it with quick sort, and printing the sorted array.

Uploaded by

Youssef Ebrahim
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

13 - Merge & Quick Sort

The document defines 6 functions: read data into an array, print an array, fill an array with random numbers, merge two sorted arrays, implement merge sort, and implement quick sort. It then tests these functions by filling an array randomly, printing it unsorted, sorting it with quick sort, and printing the sorted array.

Uploaded by

Youssef Ebrahim
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Write 6 functions: read data and store it in an array,

//print data from array, fill array with random numbers,


//merge two sorted arrays in a single array, merge sort,
//quick sort. Then write a program to test these functions

void read_array(int *ptr , int size)


{
for(int k = 0 ; k < size ; k++)
{
cout << "array["<<k<<"] = ";
cin >> ptr[k] ;
}
}
void fill_array(int *ptr , int size , int min , int max)
{
for(int k = 0 ; k < size ; k++)
{ ptr[k] = min + rand() % (max - min +1); }
}
void print_array(int *ptr , int size)
{
for(int k = 0 ; k < size ; k++)
cout << ptr[k] << "\t";
cout << "\n";
}

void merrge_two_lists(int *arr , int f1 , int e1 , int f2 , int e2)


{
int *ptr ;
ptr = new int[e2-f1+1];
int k1 , k2 , k3;
k1 = f1; k2 = f2; k3 = 0 ;
while((k1 <= e1) && (k2 <= e2))
{
if(arr[k1] < arr[k2]) { ptr[k3] = arr[k1] ; k3++; k1++; }
else { ptr[k3] = arr[k2] ; k3++; k2++; }
}
while(k1 <= e1) { ptr[k3] = arr[k1] ; k3++; k1++; }
while(k2 <= e2) { ptr[k3] = arr[k2] ; k3++; k2++; }

for(int k = 0 ; k <= e2-f1 ; k++)


arr[k+f1] = ptr[k];
}

void merge_sort(int *arr , int first , int last)


{
int mid ;
if(first == last) return;
mid = (first + last) / 2 ;
merge_sort(arr,first,mid);
merge_sort(arr,mid+1,last);
merrge_two_lists(arr,first,mid,mid+1,last);
}

void quick_sort(int *arr , int first , int last)


{
int pivot , temp , L , R ;
if(first >= last) return;
pivot = arr[first] ;
L = first +1 ;
R = last ;
while(L <= R)
{
while((L <= last) && (arr[L] <= pivot)) L++;
while((R > first) && (arr[R] >= pivot)) R--;
if(L < R) { temp = arr[L] ; arr[L] = arr[R] ; arr[R] = temp ; }
}

arr[first] = arr[R] ; arr[R] = pivot ;


quick_sort( arr , first , R-1 );
quick_sort( arr , R+1 , last);
}

void main()
{
int *arr , size , max , min;
cout << "Enter size: ";
cin >> size ;
arr = new int[size];
cout << "Enter maximum value: ";
cin >> max ;
cout << "Enter minimum value: ";
cin >> min ;
fill_array(arr,size,min,max);
cout<< "\n Numbers before sorting: \n";
print_array(arr,size);
quick_sort(arr,0,size-1);
cout<< "\n Numbers after sorting: \n";
print_array(arr,size);
getch();
}

You might also like