Question: Need help with my Quicksort program. I need it to generate arrays of 1000 to 10000, 3 or 4 digit integers using the random() function
Need help with my Quicksort program. I need it to generate arrays of 1000 to 10000, 3 or 4 digit integers using the random() function and then sort them via Quicksort. Here is my code so far.
#include
#include
void quick_sort(int arr[], int left, int right) {
int i, j, pivot;
i = left;
j = right;
pivot = arr[(left + right) / 2];
int temp;
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
if (left < j)
quick_sort(arr, left, j);
if (i < right)
quick_sort(arr, i, right);
}
}
void print_arr(int arr[]) {
for (int i = 0; i < 20; i++)
printf("%d ", arr[i]);
printf(" ");
}
int main(int argc, const char * argv[]) {
int or_arr[20] = {3,11,5,43,66,191,25,18,169,85,126,220,0,144,35,248,500,280,148,400};
printf("Before quicksort: ");
print_arr(or_arr);
clock_t begin = clock();
quick_sort(or_arr, 0, 19);
clock_t end = clock() - begin;
printf("After quicksort: ");
print_arr(or_arr);
double time_elapsed = ((double) end) / CLOCKS_PER_SEC;
printf("Elapsed time is: %f seconds ", time_elapsed);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
