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.

2
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by laudaisoi

If it's a file of simple words, why not just `in>>array[k];` rather than trying to read (poorly) one character at a time.

If it's a file of simple words, why not just `in>>array[k];` rather than trying to read (poorly) one character at a time.

I wrote QuickSort function with input is two-dimensional array A[][] . And I think import data from text file into array[][] with

``````#
for(k=0;k<n;++k){
while(array[k][l]!=NULL){

in>>array[k][l];

l++;

}

}``````

Before sorting it, just read it in, then print it out.

Until that works, don't waste effort trying to debug a sort function with garbage data.

Before sorting it, just read it in, then print it out.

Until that works, don't waste effort trying to debug a sort function with garbage data.

Thnx so much , i try and it works.
But I have another question . In input file , i have some int value .
Like that
3
4
5
2
I want to compare them in same Quicksort function. Any body help ?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.