Hi,
I'm trying to end a project that should do next points:
-generating a random vector ( for that I have the function generareVector);
-reading the vector from the file where it was generated ( citireVector);
-sorting the vector using different methods of sort;
-checking if the resulted vector was well sorted (verificareCorectitudine);
-timing each method of sort ();
I implemented 6 methods separately and everything works fine ( I am declaring the vector as a global variable) but when I'm trying to put all together (I should pass the initial vector to each sorting method and then each sorting method should return it's own results that I should print). I thought that I should consider the vector as a pointer but certanely I'm doing it wrong... Could you suggest me what am I doing wrong? Thank you and have a nice evening!
Quick sort (separately):
#include<iostream>
#include<fstream>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int n, v[10000];
bool corect = true;
int generareVector(){
srand((int) time(0));
int lowest = 1;
int highest = 10000;
int range = (highest - lowest) + 1;
int n = lowest+int(range*rand()/(RAND_MAX + 1.0));
ofstream sortari;
sortari.open("C:\\Documents and Settings\\S&D\\Desktop\\sortari.txt");
sortari<<n<<endl;
for(int i = 0; i < n; i++){
int index = lowest+int(range*rand()/(RAND_MAX + 1.0));
sortari<<index<<endl;
}
sortari.close();
return 0;
}
int citireVector(){
ifstream reading;
reading.open("C:\\Documents and Settings\\S&D\\Desktop\\sortari.txt");
reading>>n;
for(int i = 0; i<n; i++){
reading>>v[i];
}
reading.close();
return 0;
}
int sortare_rapida(int i, int j){
bool schimb = 0;
int aux;
int a = i; int b = j;
if(j <= i) return 0;
else{
while(i<j){
if(v[i] > v[j]){
aux = v[i];
v[i] = v[j];
v[j] = aux ;
schimb = !schimb;
};
if(schimb == 0) i++;
else j--;
};
sortare_rapida(a, i-1);
sortare_rapida(i+1, b);
};
return 0;
};
int verificareCorectitudine(){
int i = 0;
do{
if(v[i] > v[i+1])
corect = false;
i++;
}
while(corect == true && i < n-1 );
return 0;
}
int afisareVectorSortat(int timeQS){
ofstream results;
results.open("C:\\Documents and Settings\\S&D\\Desktop\\sortari.txt");
results<<"Rezultate pentru QuickSort:"<<endl;
for(int i=0; i<n; i++){
results<<v[i]<<endl;
}
if(corect)
results<<"Vector sorted"<<endl;
results<<timeQS<<endl;
results<<"\n";
return 0;
}
int main ()
{
generareVector();
citireVector();
int timeBQS = clock();
sortare_rapida(0, n-1);
int timeAQS = clock();
verificareCorectitudine();
int timeQS = timeAQS - timeBQS;
afisareVectorSortat(timeQS);
return 0;
}
QuickSort and Selection together:
#include<iostream>
#include<fstream>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int n;
bool corect = true;
int generareVector(){
srand((int) time(0));
int lowest = 1;
int highest = 10000;
int range = (highest - lowest) + 1;
int n = lowest+int(range*rand()/(RAND_MAX + 1.0));
ofstream sortari;
sortari.open("C:\\Documents and Settings\\S&D\\Desktop\\sortari.txt");
sortari<<n<<endl;
for(int i = 0; i < n; i++){
int index = lowest+int(range*rand()/(RAND_MAX + 1.0));
sortari<<index<<endl;
}
sortari.close();
return 0;
}
int * citireVector(){
int v[10000];
ifstream reading;
reading.open("C:\\Documents and Settings\\S&D\\Desktop\\sortari.txt");
reading>>n;
for(int i = 0; i<n; i++){
reading>>v[i];
}
reading.close();
return v;
}
int sortare_rapida(int i, int j, int * v){
bool schimb = 0;
int aux;
int a = i; int b = j;
if(j <= i) return 0;
else{
while(i<j){
if(v[i] > v[j]){
aux = v[i];
v[i] = v[j];
v[j] = aux ;
schimb = !schimb;
};
if(schimb == 0) i++;
else j--;
};
sortare_rapida(a, i-1, v);
sortare_rapida(i+1, b, v);
};
return 0;
};
int * selectie( int * v){
int min, aux, poz, i, j;
for(i=0; i<n-1; i++){
min = v[i];
poz = i;
for(j=i+1; j<n; j++){
if(v[j] < min){
min = v[j];
poz = j;
}
}
if(min != v[i]){
aux = v[i];
v[i] = v[poz];
v[poz] = aux;
}
}
return v;
}
int verificareCorectitudine(int * v){
int i = 0;
do{
if(v[i] > v[i+1])
corect = false;
i++;
}
while(corect == true && i < n-1 );
return 0;
}
int afisareVectorSortat(int * v, int * vv, int timeS, int timeQS){
ofstream results;
results.open("C:\\Documents and Settings\\S&D\\Desktop\\sortari.txt");
results<<"Results for QuickSort:"<<endl;
for(int i=0; i<n; i++){
results<<v[i]<<endl;
}
// results<<corect<<endl;
results<<timeQS<<endl;
results<<"\n";
results<<"Rezultate pentru selectie:"<<endl;
for(int i=0; i<n; i++){
results<<vv[i]<<endl;
}
results<<timeS;
results.close();
return 0;
}
int main ()
{
int * vector;
int * vectorS;
generareVector();
vector = citireVector();
int timeBS = clock();
vectorS = selectie(vector);
int timeAS = clock();
//verificareCorectitudine(vectorS);
int timeS = timeAS - timeBS;
int timeBQS = clock();
sortare_rapida(0, n-1, vector);
int timeAQS = clock();
//verificareCorectitudine(vector);
int timeQS = timeAQS -timeBQS;
afisareVectorSortat(vector, vectorS, timeS, timeQS);
return 0;
}