Writing a program that first randomly generates 5,000,000 numbers (integers) tha
ID: 3768724 • Letter: W
Question
Writing a program that first randomly generates 5,000,000 numbers (integers) that are in the range [0, 5,000,000], using an array to hold the number, and then using quicksort as following steps: Sort the array using the middle element of the array and subarrays as the pivot. Sort the array using the median of the first, last and middle elements of the array and subarrays as the pivots. Sort the array using the middle element of the array and subarrays as the pivots. However, when the size of any subarrays reduces to less than 20, sort the subarray using insertion sort. Sort the array using the median of the first, last and middle elements of the array and subarrays as the pivots. However, when the size of any subarray reduces to less than 20, sort the subarray using insertion sort.Explanation / Answer
/*program to generate 5,000,000 random numbers between 0 to 5,000,000 using while loop*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
long int a[5000000],i=0;
clrscr();
printf("The random numbers between 0 to 5000000 are: ");
while(i<5000000)
{
a[i]=rand() % 100000;
printf(" %ld ",a[i]);
i=i+1;
}
getch();
}
Quick sort program :
Now select any 5 numbers in the random generated numbers between 0 to 5000000 and check the above given 4 conditions using pivot element
#include<stdio.h>
#include<conio.h>
#include<process.h>
void q_sort(int array[],int left,int right);
int array[22],n,i,j,temp;
void main()
{
clrscr();
printf("enter the size of the array,");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" enter %d elements:",i);
scanf("%d",&array[i]);
}
printf(" before sorting ");
for(i=0;i<n;i++)
printf(" %d",array[i]);
q_sort(array,0,n-1);
printf(" after sorting ");
for(i=0;i<n;i++)
printf(" %d",array[i]);
getch();
}
void q_sort(int array[],int left,int right)
{
int pivot,l_hold,r_hold;
l_hold=left;
r_hold=right;
pivot=array[left];
while(left<right)
{
while((array[right]>=pivot)&&(left<right))
right--;
if(left!=right)
{
array[left]=array[right];
left++;
}
while((array[left]<=pivot)&&(left<right))
left++;
if(left!=right)
{
array[right]=array[left];
right--;
}
}
array[left]=pivot;
pivot=left;
left=l_hold;
right=r_hold;
if(left<pivot)
q_sort(array,left,pivot-1);
if(right>pivot)
q_sort(array,pivot+1,right);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.