Sunday, February 2, 2014

Bubble Sort on Text File in C++

I have tried to implement very simple bubble sort algorithm on files its because many of us beginners struggle with filing in the beginning and sorting in a same file without temporary files is a problem I have used the integers Id here to help you guys solve the problem you can use strings and characters as well string's individual characters can be accessed by the [ ] operator so putting in 0 will give you the first element. So you can find many more things to do here is the code you all have been wanting so long.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;
/******************CREATED by ALI AZAM RANA************************
*************aliazamrana@outlook.com******************************
-----------------------------------------------------------------*/



int main()
{
    fstream file;//use anyfile name
    file.open("Text1.txt", fstream::in | fstream::out);
   
//Contents of File I supposed
/*Id        Name            Number
78        Ali                0300-6629282
661        Xy                965555516516
82        adsa            263536153151
1        dasfa            656516531
*/
 string temp;
    int counter = 0;
    while (!file.eof())// loop to count number of lines in file
    {
        getline(file, temp);
        counter++;    
    }
    file.close();//closing file
    file.open("Text1.txt", fstream::in | fstream::out);//reopening
    counter = counter - 1; //in my file firstline was of headings so I subtracted 1 from it

    for (int i = 1; i < counter; i++)//outer loop for bubble sort
    {
        int id1, id2;//two id variable of int type to store ids sorting via them
        string detail1;//to hold the rest of the record details for ids
        string detail2;
        getline(file, temp);//remember that line of headings on top
        for (int x = 0; x < counter - i; x++)
        {
            
            streampos temPos1, temPos2;// two streamposition objects to hold the file pointer address
            temPos1 = file.tellg(); getting the streamposition first
            file >> id1;//getting id
            getline(file, detail1);//getting details of records
            temPos2 = file.tellg();now checking the file pointer position and storing it
            file >> id2;
            getline(file, detail2);
            
            if (id1 < id2)//swap is happening here 
            {
                int x = id1;
                string y = detail1;
                detail1 = detail2;
                detail2 = y;
                id1 = id2;
                id2 = x;
                file.seekg(temPos1, ios_base::beg);// moving pointer back to the start of the record to be overwritter
                file << id1 << detail1;//swapped ids and details written
                file << endl;                file << id2 << detail2;
            }
            file.seekg(temPos2, ios_base::beg);moving file pointer to the proper place of the last record
            
        }
        file.seekg(0, ios_base::beg);//back to start of the file
    }
    

    cout << endl;
    system("pause");
    return 0;
}

No comments:

Post a Comment