Hello, Everyone:
I am trying to program the following:
The bubble sort is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort:
a. After the first pass, the largest number is guaranteed to be in the highest-number element of the array; after the second pass, the two highest numbers are "in place," and so on. Instead of making nince comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on.
b. The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any sway have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.
Here is the code I come up with:
Code:
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
double time, motion;
ifstream sensin;
ofstream sensout;
sensin.open("sensin.dat");
sensout.open("sensout.dat");
sensin >> time >> motion;
int num_data_pts = 10;
double max = motion, min = motion, sum = 0;
while(!sensin.eof())
{
sum += motion;
if( motion > max);
max = motion;
if( motion < min);
min = motion;
sensin >> time >> motion;
}
cout << "Number of Readings: " << num_data_pts << endl;
cout << "Average Reading: " << sum/(num_data_pts) << endl;
cout << "Maximum Reading: " << max << endl;
cout << "Minimum Reading: " << min << endl;
sensin.close();
return 1;
}
When I compile the array, it does not show me anything. What did I do wrong in the program. Please anyone help me, I really would appreciate any help.
Thank you..