Merge sort

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 20
Reputation: skeet123 is an unknown quantity at this point 
Solved Threads: 0
skeet123 skeet123 is offline Offline
Newbie Poster

Merge sort

 
0
  #1
Nov 23rd, 2004
Can someone point me in the right direction on implementing a merge sort.
Here is what the main program will look like. THanks.

  1. int main(void)
  2. {
  3. int ar[100];
  4.  
  5. int i, v, len;
  6.  
  7. for (i=0; i<100; i++) {
  8. cout << "enter a number (-1 to quit): ";
  9. cin >> v;
  10.  
  11. if (v < 0) break;
  12.  
  13. ar[i] = v;
  14. }
  15.  
  16. len = i;
  17.  
  18. cout << "main: before sort:\n";
  19. for (i=0; i<len; i++) {
  20. cout << "main: ar[" << i << "] = " << ar[i] << endl;
  21. }
  22.  
  23. mergesort ms(ar, len);
  24.  
  25. cout << "main: after sort:\n";
  26. for (i=0; i<len; i++) {
  27. cout << "main: ar[" << i << "] = " << ar[i] << endl;
  28. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: Merge sort

 
0
  #2
Nov 24th, 2004
Regarding the merge sort u shud first specify do u want 2-way merge sort , 3-way merge or n-way.well merge sort algo is as follows:

Function Mergesort (field, lower, upper)
if lower = upper then do nothing (field is sorted)
else
middle = (upper + lower)/2
Mergesort (field, lower, middle)
Mergesort (field, middle+1, upper)
Merge (field, lower, middle, upper)
return field


Function Merge (field, lower, middle, upper)
Let ileft = lower, iright = middle+1, ihelp = lower
While ileft ≤ middle AND iright ≤ upper do
if field[ileft] ≤ field[iright] then
ifield[ihelp] = field[left]
increment ihelp, increment iright
While ileft ≤ middle do
ifield[ihelp] = field[ileft]
increment ihelp, increment ileft
While iright ≤ upper do
ifield[ihelp] = field[iright]
increment ihelp, increment iright
For ihelp from lower to upper do
field[ihelp] = ifield[ihelp]
return field
Thus in plain english do the following steps.

First divide the array into two approximately equal large sub-arrays.

Sort each of these sub-arrays separately.

Merge the resolting sorted sub-arrays into the resulting subarray
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 24
Reputation: yb1pls is an unknown quantity at this point 
Solved Threads: 1
yb1pls yb1pls is offline Offline
Newbie Poster

Re: Merge sort

 
0
  #3
Dec 9th, 2004
#include <iostream.h>
int mergesort;
int main(void)
{
int ar[100];

int i, v, len;

for (i=0; i<100; i++) {
cout << "enter a number (-1 to quit): ";
cin >> v;

if (v < 0) break;

ar[i] = v;
}

len = i;

cout << "main: before sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}

mergesort;

cout << "main: after sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,069
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Merge sort

 
0
  #4
Dec 9th, 2004
Originally Posted by yb1pls
#include <iostream.h>
int mergesort;
int main(void)
{
int ar[100];

int i, v, len;

for (i=0; i<100; i++) {
cout << "enter a number (-1 to quit): ";
cin >> v;

if (v < 0) break;

ar[i] = v;
}

len = i;

cout << "main: before sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}

mergesort;

cout << "main: after sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}
}
It is not April 1 yet!!!
Even if you had this magical global integer called mergesort, you still might have to tell it what to sort. Dream on!!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC