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

In this problem you are required to test the implementation of the sorting algor

ID: 3629031 • Letter: I

Question

In this problem you are required to test the implementation of the sorting algorithms on the test input files of 25000 and 50000 random integers. The algorithms to be implemented are Bubble sort (discussed in class) and Selection sort (read from the book). You have to compare the time required to complete the sorting for the given file with each algorithm.
Algorithms to be implemented:
1. Selection sort
2. Bubble sort.
For each input file and for each algorithm print on the screen, the number of comparisons in inner loop, number of exchanges (swaps) and time taken to produce the result. Please also refer to example below to learn how to calculate time.

time code:
#include<iostream>
#include<sys/timeb.h>
#include<time.h>

Using namespace std;
void work()
{
for(unsigned long l=42949672; l!=0; l--){
//counting back word;
}
}

Main(){
_timeb startTime,endTime;
_ftime(&startTime);
Work();//the total time for work is difference between two time
_ftime(&endTime);
Unsigned long Timeinmilisec= (endTime.time – startTime.time)*1000+(endTime.militm-startTime.militm);
Cout<<”Elapsed time: ”<< Timeinmilisec<<”ms”<<endl;
}
// you can also use another way for time if you have…

Explanation / Answer

please rate - thanks

 

used a different timer-your didn't work

#include <iostream>
#include <fstream>

using namespace std;
void selectionsort(int[],int);
void bubblesort(int[],int); 
int main()
{int a[50000],n=0,b[50000];
clock_t start;
char filename[80];
double diff1,diff2;
srand(time(0));
 cout<<"what is the name of the file you are using? ";
   cin>>filename;
   ifstream input;
   input.open(filename);           //open file
   if(input.fail())             //is it ok?
       { cout<<"file did not open please check it ";
        system("pause");
        return 1;
        }
input>>a[n];
while(input)
 {b[n]=a[n];
 n++;
 input>>a[n];
 }

cout<<"sort comparison of "<<n<<" numbers ";
cout<<"Bubble sort ";
start = clock();

bubblesort(a,n);
diff1 = (clock() - start ) ;
cout<<" time of execution: "<<diff1<<" clock ticks or "
         <<diff1/ (double)CLOCKS_PER_SEC<<" seconds "; 

cout<<" Selection sort ";
start = clock();
selectionsort(b,n);  
diff2 = (clock() - start ) ;
cout<<" time of execution: "<<diff2<<" clock ticks or "
         <<diff2/ (double)CLOCKS_PER_SEC<<" seconds ";
diff2-=diff1;
if(diff2>0)
        cout<<" Selection sort faster by "<<diff2<<" clock ticks or "
         <<diff2/ (double)CLOCKS_PER_SEC<<" seconds ";
else
     cout<<" Bubble sort faster by "<<diff2*-1<<" clock ticks or "
         <<(diff2*-1)/ (double)CLOCKS_PER_SEC<<" seconds ";         
    system("pause");
    return 0;
}
void selectionsort(int a[], int n)
{ int i,j,min,index,temp,k,compare=0,exchange=0;
    for(i=0;i<n-1; i++)
    {index=i;
     min=a[i];
      for(j=i+1;j<n;j++)
         {compare++;
          if(min>a[j]) 
          {index=j;
           min=a[j];
          }
      }
      temp=a[i];
      a[i]=a[index];
      a[index]=temp;
      exchange++;
        
    }
cout<<"comparisons="<<compare<<endl;
cout<<"exchanges="<<exchange<<endl;  
}
void bubblesort(int a[],int n)
{int i,j,temp,compare=0,exchange=0;

for(i=0;i<n-1; i++)
  for(j=0;j<n-1-i; j++)
    {compare++;
     if(a[j+1]<a[j]) 
      {temp=a[j];     
       a[j]=a[j+1];
       a[j+1]=temp;
       exchange++;
       }
      }
 cout<<"comparisons="<<compare<<endl;
cout<<"exchanges="<<exchange<<endl;        
}
void print(int array[],int n)
{int i;
for(i=0;i<n;i++)
   cout<<array[i]<<"   ";
  cout<<" ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote