1. A test harness program fro testing sorting methods is provided with the rest
ID: 3764657 • Letter: 1
Question
1. A test harness program fro testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch10 package. The program includes a swap method that is used by all of the sorting methods to swap array elements.
Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method.
Implement your approach.
Test your new program by running the selectionSort method. Your program should report 49 swaps.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
int a[]={4,3,2,1};
int i,j,temp,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=a[i];
c++;
}
}
}
cout<<"Number of sorts needed are "<<c;
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.