c For this exercise create 2 programs defined in files slowc

c++

For this exercise, create 2 programs defined in files slow.cpp and fast.cpp.

In slow.cpp, generate a random array of size 200,000 and pass it to your sorting algorithm. Run the program and observe that it never ends. Press CTRL C to terminate the process.

In fast.cpp, generate a random array of size 200,000 and pass it to the sort routine that is part of the C++ standard library. The following code shows how you would call the sort function on an array a or size 200,000.

If the above line fails to compile, you may need to include the algorithm header as follows.

Run fast.cpp and notice how quickly it terminates. The reason for this is that the sorting function in the standard library is using is more efficient than the one you produced. How quickly algorithms terminate is within an area of computer science referred to as complexity theory.

Solution

slow.cpp

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
int arr[200000],temp;
long i,j;
for(i=0;i<200000;i++)
arr[i]=rand()%100;
for(i=0;i<200000;i++)
{
for(j=i;j<200000;j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
  
}
}

}
for(i=0;i<200000;i++)
cout<<\"\\t\"<<arr[i];
}

fast.cpp

#include<iostream>
#include<algorithm>
#include<cstdlib>

using namespace std;

int main()
{
int arr[200000],temp;
long i,j;
for(i=0;i<200000;i++)
arr[i]=rand()%100;

sort(arr,arr+200000) ;
for(i=0;i<200000;i++)
cout<<\"\\t\"<<arr[i];
  
return 0;
}

for further queries kindly get back

c++ For this exercise, create 2 programs defined in files slow.cpp and fast.cpp. In slow.cpp, generate a random array of size 200,000 and pass it to your sortin
c++ For this exercise, create 2 programs defined in files slow.cpp and fast.cpp. In slow.cpp, generate a random array of size 200,000 and pass it to your sortin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site