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: -
////.h
-
-
#include <iostream>
-
#include <string>
-
using namespace std;
-
-
-
string merge(string,string);
-
string mergeSort(string);
-
string rest(int,int,string);
-
-
string rest(int from,int to,string phrase){
-
string temp;
-
-
for(int i=from;i<=to;i++){
-
temp[i-1]=phrase[i];
-
}
-
return temp;
-
}
-
string merge(string left,string right){
-
string result;
-
* * while (left.length() > 0 || right.length() > 0){
-
* * * * if (left.length() > 0 && right.length() > 0){
-
if (left[0] <= right[0]){
-
* * * * * * * * result+=left[0];
-
* * * * * * * * left= rest(1,left.length(),left);
-
}
-
if(left[0]>right[0])
-
* * {
-
result+=right[0];
-
right= rest(1,right.length(),right);
-
}
-
if (left.length() > 0){
-
result+= left[0];
-
left= rest(1,left.length(),left);
-
}
-
if (right.length() > 0){
-
-
* * result+=right[0];
-
right= rest(1,right.length(),right);
-
}
-
}
-
}
-
return result;
-
}
-
string mergeSort(string a){
-
if(a.length()<=1)
-
return a;
-
-
string left,right,result;
-
int middle=(a.length())/2;
-
-
for(int i=0;i<a.length();i++){
-
if(i<middle)
-
* left+=a[i];
-
else*
-
if(i>=middle)
-
right+=a[i];
-
-
}
-
left = mergeSort(left);
-
* * right = mergeSort(right);
-
* * result = merge(left, right);
-
* * return result+left;
-
}
-
-
-
-
-
////.cpp
-
-
#include "mergeSort.h"
-
using namespace std;
-
-
int main () {
-
* * string a,b;
-
int again=1;
-
while(again==1){
-
-
* * cout<<"Enter string to sort: ";
-
* * cin>>a;
-
cout<<"Before the sort: "<<a<<endl;
-
cout<<"After sort: ";
-
cout<<mergeSort(a)<<endl;
-
* * cout<<"1-Again* 2-Close: ";
-
cin>>again;
-
}
-
-
* * return 0;
-
}
-
Any ideas?
1 2910
Easy fix- -
-
////.h
-
-
#include <iostream>
-
#include <string>
-
using namespace std;
-
-
char *mergeSort(char letters[], char temp[], int array_size);
-
char *mSort(char letters[], char temp[], int left, int right);
-
char *merge(char letters[], char temp[], int left, int mid, int right);
-
-
char *mergeSort(char letters[], char temp[], int array_size)
-
{
-
return mSort( letters, temp, 0, array_size - 1);
-
}
-
-
-
char *mSort(char letters[], char temp[], int left, int right)
-
{
-
int mid;
-
-
if (right > left)
-
{
-
mid = (right + left) / 2;
-
mSort( letters, temp, left, mid);
-
mSort( letters, temp, (mid+1), right);
-
-
merge( letters, temp, left, (mid+1), right);
-
}
-
-
return letters;
-
}
-
-
char *merge(char letters[], char temp[], int left, int mid, int right)
-
{
-
int i, leftEnd, elements, tmpPos;
-
-
leftEnd = (mid - 1);
-
tmpPos = left;
-
elements = (right - left + 1);
-
-
while ((left <= leftEnd) && (mid <= right))
-
{
-
if ( letters[left] <= letters[mid])
-
{
-
temp[tmpPos] = letters[left];
-
tmpPos += 1;
-
left += 1;
-
}
-
else
-
{
-
temp[tmpPos] = letters[mid];
-
tmpPos += 1;
-
mid += 1;
-
}
-
}
-
-
while (left <= leftEnd)
-
{
-
temp[tmpPos] = letters[left];
-
left += 1;
-
tmpPos += 1;
-
}
-
while (mid <= right)
-
{
-
temp[tmpPos] = letters[mid];
-
mid += 1;
-
tmpPos += 1;
-
}
-
-
for (i=0; i < elements; i++)
-
{
-
letters[right] = temp[right];
-
right -= 1;
-
}
-
-
return temp;
-
}
-
-
-
////.cpp
-
-
-
#include "mergeSort.h"
-
-
int main()
-
{
-
int max,again=1;
-
-
while(again==1){
-
cout<<"Word size: ";
-
cin>>max;
-
-
char arrayOne[max];
-
char arrayTwo[max];
-
string result;
-
-
cout<<"Enter word to sort: ";
-
-
//Enter character array
-
for(int i=0;i<max;i++){
-
cout<<"Letter"<<i+1<<"/"<<max<<": ";
-
cin>>arrayOne[i];
-
}
-
-
//Before the sort
-
cout<<"Before the sort: ";
-
for(int i=0;i<max;i++)
-
cout<<arrayOne[i]<<" ";
-
cout<<endl;
-
-
//Sort
-
result=mergeSort(arrayOne, arrayTwo, max);
-
-
//After the sort
-
-
cout<<"After the sort: ";
-
for(int i=0;i<max;i++)
-
cout<<result[i]<<" ";
-
cout<<endl;
-
-
cout<<"1-Again 2-Close: ";
-
cin>>again;
-
}
-
-
cout<<endl;
-
return 0;
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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
|
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));
|
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...
| |
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...
|
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)
|
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,...
|
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,...
|
by: curiously enough |
last post by:
What's the difference between merge sort and direct merge sort?
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |