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)
-
{
-
temp=high;
-
high=low;
-
low=temp;
-
}
-
if(low<high)
-
{
-
mid=(low+high)/2;
-
merge_sort(low,mid);
-
merge_sort(mid+1,high);
-
merge(low,mid,high);
-
}
-
}
-
void merge(int low,int mid,int high)
-
{
-
int p, q, i, c[10],r;
-
p=low; //index for a[i]...a[mid]
-
q=mid+1; //index for a[mid + 1]...a[j]
-
while((p<=mid)&&(q<=high))
-
{
-
if(a[p]<=a[q]) //copy smaller value to local array c
-
{
-
c[i]=a[p];
-
p++;
-
}
-
else
-
{
-
c[i]=a[q];
-
q++;
-
}
-
i++;
-
}
-
while (a[p]<= mid)//copy the rest of the longer array
-
{
-
c[i] = a[p];
-
}
-
while (a[q]<=high)//copy the rest of the larger array
-
{
-
c[i]=a[q];
-
}
-
for (i=low; i<=high; i++)//copy back from temp array to main array
-
{
-
a[i]=c[i];
-
}
-
}
-
-
int main()
-
{
-
long timeElapsed;
-
timeElapsed = clock();
-
int num,i,elem, percent;
-
a[num];
-
cout<<"*********************************************************************"<<endl;
-
cout<<" MERGE SORT PROGRAM"<<endl;
-
cout<<"**********************************************************************"<<endl<<endl<<endl;
-
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort[THEN PRESS ENTER]:"<<endl;
-
cin>>num;
-
cout<<"Please Enter THE PERCENTAGE you want to sort[THEN PRESS ENTER]:"<<endl;
-
cin>>percent;
-
for (i=1; i<=num; i++)
-
{
-
elem =((rand()*9)+1);
-
cout<<endl<<"element "<<i<<"is "<<elem<<endl;
-
a[i]= elem;
-
cout<<"When i is "<<i<<" ai is "<<a[i];
-
}
-
merge_sort(1,num);
-
cout<<endl<<"So, the sorted list (using MERGE SORT) will be : "<<endl;
-
for(i=1;i<=num;i++)
-
{
-
cout<<a[i]<<" ";
-
}
-
cout<<endl<<"Time Elapsed is: "<<timeElapsed<<endl<<endl<<endl<<endl<<endl;
-
system("PAUSE");
-
return 0;
-
}
2 2866
Hi,
There are couple of issues in the code....
1)First u start referencing the array from 1 and reach the max size which is wrong(For example if size of array is 5 then in the loop u shuld start from 0 and reference upto 4)
2)U are not initializing local variables before usage.
I have corrected some part of the code and posted it here.... -
void merge(int,int,int);
-
void merge_sort(int low, int high)
-
{
-
int mid, temp;
-
if (low==high)
-
return;
-
if (low+1==high)
-
{
-
temp=high;
-
high=low;
-
low=temp;
-
}
-
if(low<high)
-
{
-
mid=(low+high)/2;
-
merge_sort(low,mid);
-
merge_sort(mid+1,high);
-
merge(low,mid,high);
-
}
-
}
-
void merge(int low,int mid,int high)
-
{
-
int p, q, i, c[10],r;
-
i=0;
-
r=0;
-
p=low; //index for a[i]...a[mid]
-
q=mid+1; //index for a[mid + 1]...a[j]
-
while((p<=mid)&&(q<=high))
-
{
-
if(a[p]<=a[q]) //copy smaller value to local array c
-
{
-
c[i]=a[p];
-
p++;
-
}
-
else
-
{
-
c[i]=a[q];
-
q++;
-
}
-
i++;
-
}
-
while (a[p]<= mid)//copy the rest of the longer array
-
{
-
c[i] = a[p];
-
}
-
while (a[q]<=high)//copy the rest of the larger array
-
{
-
c[i]=a[q];
-
}
-
for (i=low; i<=high; i++)//copy back from temp array to main array
-
{
-
a[i]=c[i];
-
}
-
}
-
-
int main()
-
{
-
long timeElapsed;
-
timeElapsed = clock();
-
int num,i,elem, percent;
-
//a[num];
-
cout<<"************************************************** *******************"<<endl;
-
cout<<" MERGE SORT PROGRAM"<<endl;
-
cout<<"************************************************** ********************"<<endl<<endl<<endl;
-
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort[THEN PRESS ENTER]:"<<endl;
-
cin>>num;
-
cout<<"Please Enter THE PERCENTAGE you want to sort[THEN PRESS ENTER]:"<<endl;
-
cin>>percent;
-
//for (i=1; i<=num; i++)
-
for (i=0; i<num; i++)
-
{
-
elem =((rand()*9)+1);
-
cout<<endl<<"element "<<i<<"is "<<elem<<endl;
-
a[i]= elem;
-
cout<<"When i is "<<i<<" ai is "<<a[i];
-
}
-
//merge_sort(1,num);
-
merge_sort(0,num);
-
cout<<endl<<"So, the sorted list (using MERGE SORT) will be : "<<endl;
-
//for(i=1;i<=num;i++)
-
for(i=0;i<num;i++)
-
{
-
cout<<a[i]<<" ";
-
}
-
cout<<endl<<"Time Elapsed is: "<<timeElapsed<<endl<<endl<<endl<<endl<<endl;
-
system("PAUSE");
-
return 0;
-
}
-
-
Try this....i havent checked the logic of ur code still...
Raghuram
<Sometimes posting with a quote and code causes the post to disappear. The quote has been removed so that the response is visible.
MODERATOR>
I tried this and it looks good, but my sort isn't sorting correctly. Can you you look at the logic? Thank you!!
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...
|
by: Tom Keane |
last post by:
Okay, so the deal is, I am doing a mail merge document in order to print
invoices. The field I get the amount of money is "invAmount". What I would
like to do is be able to manipulate this number...
|
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: rkk |
last post by:
Hi,
I have written a generic mergesort program which is as below:
---------------------------------------------------------
mergesort.h
-----------------------
void
MergeSort(void...
|
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#,...
|
by: Sam Durai |
last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes
even 0 rows) to a big table (of rows around 4 billion). I used the PK
of the big table as merge key but merge does a table scan...
|
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...
|
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:
...
|
by: Tem |
last post by:
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |