Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3 .Write a computer program that prompts the user for one number, n for the numb

ID: 3870462 • Letter: 3

Question

3 .Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find the time Use bubble sort to sort the array Get the time and set this to end-time Subtract start-time from end-time and add the result to total_time Once the program has run, note The number of items sorted The average running time for each array (total_time/1000) Repeat the process six times, using 50, 250 and 500 as the size of the array for each of the two algorithms.

4. Repeat 3 using selection sort.

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
   int n,i,temp,j,iteration,avg_time;
   double total_time=0;
   //prompt user to enter n value
   printf("Enter n value: ");
   scanf("%d",&n);
   //n size of arr[]
   int arr[n];
   time_t t;//to generate random numbers
   srand((unsigned)time(&t));//initiazing random number generator
   //iterating 1000 times
   for(iteration=0;iteration<1000;iteration++)
   {
       //start timer
       clock_t begin=clock();
       //generating n random items between 0 to 10000
       for(i=0;i<n;i++)
           arr[i]=rand()%(10000);
       //bubble sort
       for(i=0;i<n;i++)
       {
           for(j=i+1;j<n;j++)
           {
               if(arr[i]<arr[j])
               {
                   temp=arr[i];
                   arr[i]=arr[j];
                   arr[j]=temp;
               }
           }
       }
       //end timer
       clock_t end=clock();
       //time span
       double time_spent=(double)(end-begin)/CLOCKS_PER_SEC;
       //total time
       total_time=total_time+time_spent;
   }
   //avg_time
   avg_time=total_time/1000;
   printf("%f ",avg_time);  
}

4)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
   int n,i,temp,j,iteration,avg_time,min_index;
   double total_time=0;
   //prompt user to enter n value
   printf("Enter n value: ");
   scanf("%d",&n);
   //n size of arr[]
   int arr[n];
   time_t t;//to generate random numbers
   srand((unsigned)time(&t));//initiazing random number generator
   //iterating 1000 times
   for(iteration=0;iteration<1000;iteration++)
   {
       //start timer
       clock_t begin=clock();
       //generating n random items between 0 to 10000
       for(i=0;i<n;i++)
           arr[i]=rand()%(10000);
       //selection sort
       for(i=0;i<n;i++)
       {
           min_index=i;
           for(j=i+1;j<n;j++)
           {
               if(arr[i]<arr[min_index])
               {
                   min_index=j;
               }
           }
           temp=arr[i];
           arr[i]=arr[min_index];
           arr[min_index]=temp;
       }
       //end timer
       clock_t end=clock();
       //time span
       double time_spent=(double)(end-begin)/CLOCKS_PER_SEC;
       //total time
       total_time=total_time+time_spent;
   }
   //avg_time
   avg_time=total_time/1000;
   printf("%f ",avg_time);  
}