In: Computer Science
MUST BE DONE IN C++
Use qsort( ) function in Visual Studio to sort the following three arrays:
int array1 [] = { 3, 4, 2, 1, 7};
float array2 [] = {0.3, 0.1, 5.5, 4.3, 7.8};
char array3 [] = {‘c’, ‘d’, ‘a’, ‘b’, ‘f’};
Develop a driver function to print out the sorted results and put the screenshot into the
word document.
Note that you have to use qsort( ) provided by Visual Studio.
What is the time complexity of quick sort?
RUN THIS IN VISUAL STUDIO, IT WILL GIVE THE SAME OUTPUT
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int compare(const void* a, const void* b)
{
const int* x = (int*) a;
const int* y = (int*) b;
if (*x > *y)
return 1;
else if (*x < *y)
return -1;
return 0;
}
int cmpfunc( const void *a, const void *b) {
return *(char*)a - *(char*)b;
}
int main(int argc, char const *argv[])
{
int array1 [] = { 3, 4, 2, 1, 7};
float array2 [] = {0.3, 0.1, 5.5, 4.3, 7.8};
char array3 [] = {'c', 'd', 'a', 'b', 'f'};
clock_t time;
time = clock();
qsort(array1, 5, sizeof(int), compare);
time = clock() -time;
for(int i=0 ;i<5;i++)
cout<<array1[i]<<" ";
cout<<endl;
cout<<"Time for array1 -
"<<time<<endl<<endl;
time = clock();
qsort(array2, 5, sizeof(float), compare);
time = clock() -time;
for(int i=0 ;i<5;i++)
cout<<array2[i]<<" ";
cout<<endl;
cout<<"Time for array2 -
"<<time<<endl<<endl;
time = clock();
qsort(array3, 5, sizeof(char), cmpfunc);
time = clock() -time;
for(int i=0 ;i<5;i++)
cout<<array3[i]<<" ";
cout<<endl;
cout<<"Time for array3 -
"<<time<<endl<<endl;
}