842 M C 20.00 15 2 250 10.00 UC RosebudTheatre 90 One Mike Stand
990 E C 20.00 19 2 1250 10.00 TX TexasHall 75 Maverick Speaker Series554 C N 16.45 30 1 35 0.00 UC PaloDuraLounge 60 Global Grounds Coffee Hour
318 E C 18.00 3 4 350 0.00 NH 100 75 Engineering Speaker Series
842 M C 20.00 16 4 250 10.00 UC RosebudTheatre 90 One Mike Stand
4 V N 9.00 26 4 2000 0.00 LIB LibraryMall 240 The Big Event
868 M N 10.00 2 4 600 0.00 SAC 108 360 3 Art Glass Sale

ok, Lets say I have this txt file above. each line is stored in an array of events. each of these events has a code at the begining of the event (for the first event it would be 842, second would be 990 and so on). I want to use recursive merge sort algorithm to sort each of the events by this code and put them all in the right order. how do i implement this as i looked around and could only find sorting an array of numbers which i understand however this will be sorting something from each list. each of the members in the event have a set_name function as well as get_name function.

Recommended Answers

All 2 Replies

Pick whatever sort you want to use to sort integers (you have chosen merge sort). Presumably you have this data stored in a struct/class and you have an array of this type. Your ==, < and > comparison operators for this struct/class will be based on this first element. You'll likely need a deep copy function for any swaps. Whether you go the class/operator route or just use plain old functions is up to you, but write the comparison functions and the deep copy functions first, then write (possibly) a swap function. Then implement your sort using them. Add the appropriate const qualifiers to the functions below:

struct a
{
    int b; // order based on this
    int c;
};

bool LessThan (a d, a e)
{
    return (d.b < e.b);
}

bool GreaterThan (a d, a e)
{
    // similar to above
}

bool Equals (a d, a e)
{
    // similar to above
}

void DeepCopy (a d, a e)
{
    // deep copy code.  Copy e into d
}

void Swap (a d, a e)
{
    a temp;
    // swap function.  You'll do a deep copy of d or e into temp as part of the swap
}

void Sort (a d[], int numItems)
{
    // whatever sort function you choose.  Use the functions above to help you.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.