1 / 11

Merge Sort Algorithm

Merge Sort Algorithm. Merge sort has two phases. First it divides the data into smaller and smaller lists until they are size 2 or 1 Second as it returns it merges all the lists using a merge algorithm Consider the following data set. 78 45 34 20 18 15 96 10

sauda
Download Presentation

Merge Sort Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Merge Sort Algorithm • Merge sort has two phases. • First it divides the data into smaller and smaller lists until they are size 2 or 1 • Second as it returns it merges all the lists using a merge algorithm • Consider the following data set. • 78 45 34 20 18 15 96 10 • The first two sub lists for merging will be • 45 78 • 20 34 • After merge • 20 34 45 78 • The next two sub-lists are • 15 18 • 10 96 • After merge • 10 15 18 96 • Final merge yields • 10 15 18 20 34 45 78 96 Computer Science I - Martin Hardwick

  2. vector <int> merge( vector <int>list1, vector <int>list2) // Merge two sorted lists { vector <int> result; int i1 = 0; int i2 = 0; while (i1 < list1.size() || i2 < list2.size()) { if (i1 < list1.size() && i2 < list2.size()) { if (list1[i1] < list2[i2]) result.push_back (list1[i1++]); else result.push_back (list2[i2++]); } else { while (i1 < list1.size()) result.push_back (list1[i1++]); while (i2 < list2.size()) result.push_back (list2[i2++]); } } return result; } This algorithm picks the smallest item from each list. When it reaches the end of one list it then fills in the remaining items from the other list. Consider List1: 18 23 45 78 List2: 19 21 80 90 List 1 will be consumed first Result: 18 19 21 23 45 78 I1 = 4 I2 = 2 We then add 80 and 90 from list 2 Result:18 19 21 23 45 78 80 90 I1 = 4 I2 = 4 Merge Computer Science I - Martin Hardwick

  3. Data management issues • The given algorithm is not very efficient. • It adds too many items to too many vectors using push_back. • The system may run out of space and have to garbage collect • A more efficient approach is to define the space needed at the beginning of the program • Either • Create a vector with a specific size vector <int> result (10000); • Use ordinary arrays with enough size int result [1000]; • Another issue for the largest test (128,000) items is running out of memory in your program. To fix this: • Goto Project/Properties • Select Linker/System • Set the stack sizes to 1,000,000,000 Computer Science I - Martin Hardwick

  4. The memory is divided into three areas Static area (not shown) This area is of fixed size It stores the program code and any static (global) variables Stack area This area stores the stack frames of all the currently executing functions It needs to be big enough for all of the local data for all the functions Heap area This area stores all the data whose size cannot be predicted Memory management Call stack Frame Call stack Frame Call stack Frame Call stack Frame Call stack Frame top top gap gap Computer Science I - Martin Hardwick

  5. Memory management • If the heap grows too large then it will collide with the stack and the system will run out of memory • The heap contains random items of random size some of which are no longer used • A garbage collector can go and find these unused locations (pass 1) • Compress all the space by squeezing out the gaps (pass 2) • Leaving new space at the top of the heap • However, running the garbage collector is very expensive • We can help by not wasting memory • By telling the system when something is going to grow big vector <int> result (10000) • So that the system does not waste space by first creating a small vector, then a middle size copy, then a large, then a very large, then an enormous copy. Computer Science I - Martin Hardwick

  6. A vector object is divided into two components The header containing fixed size information Current number of elements Pointer to data elements Stored on the stack Data elements The data items in sequence stored in the heap More on this CS 2 How to make and use pointers How to get your own data on the heap. Memory management Vector <int> V1 Header elements Vector <int> V2 Header elements Data elements for v1 top gap gap Computer Science I - Martin Hardwick

  7. vector <int> merge( vector <int>list1, vector <int>list2) // Merge two sorted lists { vector <int> result (list1.size() + list2.size()); int i1 = 0; int i2 = 0; int resi = 0; while (i1 < list1.size() || i2 < list2.size()) { if (i1 < list1.size() && i2 < list2.size()) { if (list1[i1] < list2[i2]) result[resi++] = list1[i1++]; else result[resi++] = list2[i2++]; } else { while (i1 < list1.size()) result[resi++] = list1[i1++]; while (i2 < list2.size()) result[resi++] = list2[i2++]; } } return result; } This algorithm sets a size for the new list. Therefore, it does not need to use push_back Still slow compared to array solution however. Arrays always use a big block of contiguous memory. Paging is not such an issue. (Paging occurs when the OS has to get data for your program from the disk) Merge made more efficient Computer Science I - Martin Hardwick

  8. bool operator >(acct a, acct b) { return a.get_num() > b.get_num(); } ostream& operator<< (ostream &s, acct a) { os << “Name:“ << a.get_name(); os << “ Balance:“ << a.get_bal(); return os; } acct operator + (acct a, acct b) { return acct (a.get_num(), a.get_name(), a.get_bal() + b.get_bal()); } Remember the bank account example. We can enrich this example by using operator overloading The code on the left defines The meaning of > for bank accounts A special version of << for bank accounts A plus function for bank accounts that returns the value of a with b’s balance added Operator Overloading Computer Science I - Martin Hardwick

  9. class acct { // bank account data private: int num; // account number string name; // owner of account double balance; // balance in account public: acct (); acct (int anum, string aname, double abal); double get_bal (); string get_name(); int get_num (); void put_num (int num); void put_name (string name); void put_bal (double bal); bool is_bankrupt(); bool operator<(acct b); bool operator>(acct b); }; There is a sort function in <algorithm> that can be used to sort a vector of any type of data. All we have to do is define the “>” and “<“ operators for this data Operator overloading and sort Computer Science I - Martin Hardwick

  10. bool acct::operator< (acct b) { return get_name() < b.get_name(); } bool acct::operator> (acct b) { return get_name() > b.get_name(); } In the implementation we give a meaning to the > and < operators. In this case we are defining them using the alphabetic order of the names. These are object functions so get_name() returns the name of this object. Implementation Computer Science I - Martin Hardwick

  11. else if (command == "sort") { // LIST COMMAND for (loc=0; loc < my_bank.size () - 1; loc++) { if (my_bank.get (loc) > my_bank.get (loc + 1)) cout << "Bank is out of order at location " << loc << endl; } vector <acct> temp = my_bank.get_all(); sort (temp.begin(), temp.end()); my_bank.put_all (temp); cout << “Sorted List of accounts:" << endl; for (loc=0; loc < my_bank.size (); loc++) { account = my_bank.get (loc); cout << "Account: " << account.get_num() << “\tOwner: " << account.get_name() << “\tBalance: " << account.get_bal() << endl; } } You must include <algorithm> at the top of your program. This code tests for unsorted data. Then it does a sort Then it prints the new data After the sort the accounts will be sorted by name! Not by number. Do not try using the insert functionality in the bank account example after doing this sort A better solution will put the sort into a member function on the bank Usage Computer Science I - Martin Hardwick

More Related