473,651 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Merge Sort

computerfox
276 Contributor
Hello everyone,

I'm having some trouble with the code for merge sort. I get what is it but the code doesn't seem to work. Here is my code:


Expand|Select|Wrap|Line Numbers
  1. ////.h
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. string merge(string,string);
  9. string mergeSort(string);
  10. string rest(int,int,string);
  11.  
  12. string rest(int from,int to,string phrase){
  13.     string temp;
  14.  
  15.     for(int i=from;i<=to;i++){
  16.         temp[i-1]=phrase[i];
  17.     }
  18.     return temp;
  19. }
  20. string merge(string left,string right){
  21.     string result;
  22. * * while (left.length() > 0 || right.length() > 0){
  23. * * * * if (left.length() > 0 && right.length() > 0){
  24.             if (left[0] <= right[0]){
  25. * * * * * * * * result+=left[0];
  26. * * * * * * * * left= rest(1,left.length(),left);
  27.             }
  28.             if(left[0]>right[0])
  29.             * * {
  30.                 result+=right[0];
  31.                 right= rest(1,right.length(),right);
  32.             }
  33.                 if (left.length() > 0){
  34.                     result+= left[0];
  35.                     left= rest(1,left.length(),left);
  36.                 }
  37.                 if (right.length() > 0){
  38.  
  39.                 * * result+=right[0];
  40.                     right= rest(1,right.length(),right);
  41.                 }
  42.             }
  43.         }
  44.     return result;    
  45. }
  46. string mergeSort(string a){
  47.     if(a.length()<=1)
  48.         return a;
  49.  
  50.     string left,right,result;
  51.     int middle=(a.length())/2;
  52.  
  53.     for(int i=0;i<a.length();i++){
  54.         if(i<middle)
  55.         * left+=a[i];
  56.         else*
  57.             if(i>=middle)
  58.             right+=a[i];
  59.  
  60.     }
  61.     left = mergeSort(left);
  62. * * right = mergeSort(right);
  63. * * result = merge(left, right);
  64. * * return result+left;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. ////.cpp
  71.  
  72. #include "mergeSort.h"
  73. using namespace std;
  74.  
  75. int main () {
  76. * * string a,b;
  77.     int again=1;
  78.     while(again==1){
  79.  
  80.     * * cout<<"Enter string to sort: ";
  81.     * * cin>>a;        
  82.         cout<<"Before the sort: "<<a<<endl;
  83.         cout<<"After sort: ";
  84.         cout<<mergeSort(a)<<endl;
  85.     * * cout<<"1-Again* 2-Close: ";
  86.         cin>>again;
  87.     }
  88.  
  89. * * return 0;
  90. }
  91.  
Any ideas?
Oct 28 '11 #1
1 2910
computerfox
276 Contributor
Easy fix-

Expand|Select|Wrap|Line Numbers
  1.  
  2. ////.h
  3.  
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. char *mergeSort(char letters[], char temp[], int array_size);
  9. char *mSort(char letters[], char temp[], int left, int right);
  10. char *merge(char letters[], char temp[], int left, int mid, int right);
  11.  
  12. char *mergeSort(char letters[], char temp[], int array_size)
  13. {
  14.     return mSort( letters, temp, 0, array_size - 1);
  15. }
  16.  
  17.  
  18. char *mSort(char letters[], char temp[], int left, int right)
  19. {
  20.     int mid;
  21.  
  22.     if (right > left)
  23.     {
  24.         mid = (right + left) / 2;
  25.         mSort( letters, temp, left, mid);
  26.         mSort( letters, temp, (mid+1), right);
  27.  
  28.         merge( letters, temp, left, (mid+1), right);
  29.     }
  30.  
  31.     return letters;
  32. }
  33.  
  34. char *merge(char letters[], char temp[], int left, int mid, int right)
  35. {
  36.     int i, leftEnd, elements, tmpPos;
  37.  
  38.     leftEnd = (mid - 1);
  39.     tmpPos = left;
  40.     elements = (right - left + 1);
  41.  
  42.     while ((left <= leftEnd) && (mid <= right))
  43.     {
  44.         if ( letters[left] <=  letters[mid])
  45.         {
  46.             temp[tmpPos] =  letters[left];
  47.             tmpPos += 1;
  48.             left += 1;
  49.         }
  50.         else
  51.         {
  52.             temp[tmpPos] =  letters[mid];
  53.             tmpPos += 1;
  54.             mid += 1;
  55.         }
  56.     }
  57.  
  58.     while (left <= leftEnd)
  59.     {
  60.         temp[tmpPos] =  letters[left];
  61.         left += 1;
  62.         tmpPos += 1;
  63.     }
  64.     while (mid <= right)
  65.     {
  66.         temp[tmpPos] =  letters[mid];
  67.         mid += 1;
  68.         tmpPos += 1;
  69.     }
  70.  
  71.     for (i=0; i < elements; i++)
  72.     {
  73.         letters[right] = temp[right];
  74.         right -= 1;
  75.     }
  76.  
  77.     return temp;
  78. }
  79.  
  80.  
  81. ////.cpp
  82.  
  83.  
  84. #include "mergeSort.h"
  85.  
  86. int main()
  87. {
  88.     int max,again=1;
  89.  
  90.     while(again==1){
  91.     cout<<"Word size: ";
  92.     cin>>max;
  93.  
  94.     char arrayOne[max];
  95.     char arrayTwo[max];
  96.         string result;
  97.  
  98.     cout<<"Enter word to sort: ";
  99.  
  100.     //Enter character array
  101.     for(int i=0;i<max;i++){
  102.         cout<<"Letter"<<i+1<<"/"<<max<<": ";
  103.         cin>>arrayOne[i];
  104.     }
  105.  
  106.     //Before the sort    
  107.     cout<<"Before the sort: ";
  108.     for(int i=0;i<max;i++)
  109.         cout<<arrayOne[i]<<" ";
  110.     cout<<endl;
  111.  
  112.     //Sort
  113.     result=mergeSort(arrayOne, arrayTwo, max);
  114.  
  115.     //After the sort
  116.  
  117.     cout<<"After the sort: ";
  118.     for(int i=0;i<max;i++)
  119.         cout<<result[i]<<" ";
  120.     cout<<endl;
  121.  
  122.         cout<<"1-Again  2-Close: ";
  123.         cin>>again;
  124.     }
  125.  
  126.     cout<<endl;
  127.     return 0;
  128. }
  129.  
Merge sort can not be done with strings, you need a list of either characters or arrays. Despite strings being character arrays, it treats it differently.
Oct 28 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4598
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many comparisons. It seems to me like I should just use a single counter in the merge function's 'if' statement, but this can't be right because an array of 50 takes about 100 comparisons this way. If anyone has any suggestions I would greatly...
1
12834
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
5
3540
by: uppe | last post by:
Hey everyone, I've just finished my implementation of the merge-sort algorithm in C, and I thought I could ask for some feedback. (One can always improve, they say) Right now, the code sorts integers in an ascending order, which probably isn't very useful on its own. I know the code works for a reasonable and arbitrary size of input, but
9
5608
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
7
14247
by: Zeba | last post by:
Hi, I have to write program in C# to merge sort a linked list (doubly linked). Is it true that the best way to do it is copy it into an array, sort it and then convert back ? I'm new to C#, but I tried to develop a merge sort program myself, but have got stuck with a Null reference exception for the pos variable in merge function. Also I wrote a function to get the actual nodes from teh index values before calling merge function. Is...
2
3544
nabh4u
by: nabh4u | last post by:
hi friends, i have a program where i have to sort a double linked list using merge sort. i am having problem with changing the pointers of the list, like the previous and the next pointers of a node. i am unable to understand how can i change those. i tried something but that is not working. please help me as soon as possible as i have a deadline. here is a sample : int merge(low,mid,high) { int i,j,k; i=low; j=mid+1; list...
2
2882
by: mqueene7 | last post by:
below is my code for my merge sort but I can't get it to sort properly. I am trying generate random numbers based on input from the user and then sort those random numbers. Can you tell me what I am doing wrong? void merge(int,int,int); void merge_sort(int low, int high) { int mid, temp; if (low==high) return; if (low+1==high)
5
4481
by: Sarahger9 | last post by:
I am trying to generate a random vector of integers and sort them using merge sort. I am having trouble with the code. It is long, but I would appreciate if someone could take a look at it and see if the can help me, I've been working on it for days and am completely stuck. When I run the program, it just stops at the point where I call the mergesort. I believe the problem may be in the merge split, because when I cout the values for low,...
0
1480
by: enigma08 | last post by:
I need to merge sort two linked lists, each has a header and the elements are all ints. I've tried adapting some generic code, but have run into a problem - errors that are similar to this one: split(AlgSet.LLAlgSet) in AlgSet.LLAlgSet cannot be applied to (Node<java.lang.Integer>) I see how that is happening as I'm giving it an element next instead of an actual list. Can someone help me out? I don't think it'll take much to fix it,...
7
3307
by: curiously enough | last post by:
What's the difference between merge sort and direct merge sort?
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.