how to bubble sort ??

Hi Everybody,

I Want to create the function that
reads unsorted informtions from one text file
and sort them based on ID
and then put the sorted informations in that first opened file
but I dont know ho to do this !!

for example :this information are in the file
ID names // this line is not in the file
:::::::::::::::::::::::::::::::::::::::::::::::::::

555 * rahmat
642 * david
214 * john
321 * criss

:::::::::::::::::::::::::::::::::::::::::::::::::::
I want this result from function !!
and I want clean Previous information form the file
and I want put this sorted informations in file


214 * john
321 * criss
555 * rahmat
642 * david

:::::::::::::::::::::::::::::::::::::::::::::::::::
any one can help .....????
thank you.
Hi @Rahmat

1. I want this result from function !!
You can use bubble sort as you mentioned, check this out:http://en.wikipedia.org/wiki/Bubble_sort

2. I want clean Previous information form the file
You can read all information in the file, then close the file handle and use the same name to open an ofstream handle(the idea is to open in "w" mode, instead of "a" or "w+"mode), this will clear the existing file and get you ready to write.

3. I want put this sorted informations in file
Use a loop to run through all of the IDs, and write them to the file created on Step2.

Hope this would be helpful.
Actually, you can just use an fstream, that supports both read and write operations.

(the idea is to open in "w" mode, instead of "a" or "w+"mode),

This sounds like you are getting confused with fopen().
closed account (j3Rz8vqX)
A basic idea to help you jump start.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void bubble_sort(/*your array or vector of strings - passed as reference*/)
{
   //for_loop - based on vector size; ensure there are two comparable strings left!
      //compare string[x] and string[x+1];
         //If less; do nothing;
         //If greater; copy first string to temp, copy second string to first, then copy the temp string to second string.
   //repeat loop till there is only one string left
}
int main()
{
   //initialize variables and file

   //use a mean, to read data from file; I would suggest getline(cin,str_temp) embedded in a while-loop till eof.

   bubble_sort(/*your array or vector of strings*/)

   //write data back to file; ensure to overwrite with "w"
   return 0;
}


You may want to work on one step(comment) at a time.
thank all of you so much !!!
now i can start my project !!

just i want to know something before i start that

please tell me :

1. what is "w" mode ?
2. what is "a" mode ?
3. what is "w+" mode ?

I am not a professional programmer , so please explain them for me .
Thank you again .
The "w", "a", etc is for the C file I/O functions (fopen(), to be exact).

Since you are using C++, you don't have to care. Use an ifstream.

[edit]
BTW, on Bubble Sort
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/
Last edited on
Topic archived. No new replies allowed.