Hello everybody !
I'm a novice , and already started learn C++ . I try sorting string alphabetically from a text file . This is my code
#include <iostream>
#include <fstream>
using namespace std;
// Quick Sort function
void quickSort(char A[][15], int lower, int upper){
if (lower>=upper) return ; // If first >= lastest -> not array ;
int i=lower ; int j=upper;
char *x;
x=A[(lower+upper)/2]; // middle point is pivot
while(i<=j){
while ((strcmp(A[i],x) < 0)) // Compare A[i] and pivot
i++;
while ((strcmp(A[j],x) > 0))
j--;
if(i<=j){ // Swap A[i] , A[j]
char tmp[20];
strcpy(tmp,A[i]); //
strcpy(A[i],A[j]);
strcpy(A[j],A[i]);
i++;j--;
}
}
if(j>lower){ // Call recursion
quickSort(A,lower,j);
}
if(i<upper){
quickSort(A,i,upper);
}
}
main(){
int k=0,n=0;char array[100][15];
ifstream in("quicksort.in"); // Call input file
ofstream out("quicksort.out"); // Call output file
in>>n;int l=0; // The first number is height of array
for(k=0;k<n;++k){ // insert data into array
while(array[k][l]!=NULL){ // insert each character into two-dimensional array
in>>array[k][l];
l++;
}
}
in.close();
quickSort(array,0,n-1); // Call quicksort
for(k=0;k<n;++k){ // Export to output
out<<array[k]<<"\n";
}
out.close();
system( "pause" );
}
Form of input file like
3
nga
born
hell
But output don't like I image . Anybody help me ? Thank so much.