Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want algorithm to sort records.

Status
Not open for further replies.

KingOfJava

Programmer
Feb 21, 2003
7
US
I want to sort records in a flatfile Could any one please help me in writing (program for it) Algoritham.

Thanx in advance,

Regards.
 
Why not use one of the predefined sorts?
What are the records - colon delinated data?
Sorted on what type of critiria?

Sounds like you should RTFM on Alrogithm.h and the STL.

Merge-sort (a.k.a. quick sort) would be the best option. The idea being that one element is already sorted, so you devide the list recursivly into halves, and then reassemble it in order. O(n lg n)

Although if you have clockcycles to spare, you may want to start out with a bubble sort or other quadratic sort.

What KIND of sort are looking for?
 
What ever the sort, Let me do it on EMp rec

EMPNO:ENAME:JOB:MGR_NO:HIRE_DATE:SAL:COMM:DEPTNO

Is the records in the file with no of rec's I wanna sort with Job and Deptno as a key.

I am looking for alog.

Thanx in adv.

Regards,

KingOfJava.
 
First write a class to mirror the object.... (a struct might do)... include a constructor that takes that string.


Code:
Rec(string s)
{
  //parse string
}


then push into an array as you read them.


Code:
vector<rec> v;
string a;
While(cin >> a)
{
    v.push_back(rec(a));
}


Next, over load a lessthan() funtion that takes two recs, and compares the two, you can even overload the < operator, with just a call to this function.
Then use it to call what ever sort you need.

This is plain vanilla coding... look at cplusplus.com and other internet resources for information.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top