below is my code - i can get the numbers to generate but they will not sort. i need to use the merge sort method for sorting. i also need to make this array size whatever the user wants - but it will have to go up to 1000000.
can someone point out why my numbers are not sorting? I've been testing with small values such as 4 or 5. -
#include <iostream>
-
#include <cstdlib>
-
#include <cmath>
-
#include <time.h>
-
using namespace std;
-
-
int a[10], aSize;
-
void merge(int,int,int);
-
void merge_sort(int low, int high);
-
-
void merge_sort(int low, int high)
-
{ int mid, temp;
-
if (low==high) //this works - checking if there's one element
-
return;
-
if (low+1==high) // checking if there are only 2 elements
-
{ if (low>high) //compare the two elements
-
{
-
temp=high; //swap if low is greater than high
-
high=low;
-
low=temp;
-
}
-
}
-
else
-
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 size, p, q, i; //, r;
-
int c[10]; //array c - the temporary array for sorting
-
size = high - low + 1; //size of array c
-
c[size];
-
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])
-
{ c[i]=a[p]; //copy smaller value to local array c
-
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 i,elem;
-
a[aSize];
-
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>>aSize;
-
for (i=0; i<aSize; 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(0,aSize);
-
cout<<endl<<"So, the sorted list (using MERGE SORT) will be : "<<endl;
-
for(i=0;i<aSize;i++)
-
{ cout<<a[i]<<" ";
-
}
-
cout<<endl<<endl<<"Time Elapsed is: "<<timeElapsed<<endl<<endl<<endl<<endl;
-
system("PAUSE");
-
return 0;
-
}
-
4 1564 -
void merge(int low,int mid,int high)
-
{ int size, p, q, i; //, r;
-
int c[10]; //array c - the temporary array for sorting
-
size = high - low + 1; //size of array c
-
c[size];
-
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])
-
{ c[i]=a[p]; // <--- HERE"S YOUR PROBLEM
-
-
You never initialized i before using it to index into the c array.
You never initialized i before using it to index into the c array.
i is initialized in the first line
Correction: i is declared (so it exists), but you never give it a value. Giving a starting value to a variable is called initializing it.
i is initialized in the first line
Declared, not initialized.
Until you give it a value, i contains junk.
Your program will crash because c[i] tries to access memory outside of your array.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: its me |
last post by:
Let's say I have a class of people...
Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class
And I declare an array of this class...
|
by: Elijah Bailey |
last post by:
I want to sort a set of records using STL's sort() function,
but dont see an easy way to do it.
I have a
char *data;
which has size mn bytes where m is size of the record and
n is the...
|
by: Grahamo |
last post by:
Hi
I'm working with deployed code and am looking for ways to optimise it
and clean up memory leaks etc. I'm working with a template
ContainerOfPointers, which is exactly that. It has methods...
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort...
|
by: Bob Dankert |
last post by:
Is there any way to maintain the sort order of a sort on a 2D array? For
example:
I have the array:
1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b
and sort it by the second column, and I...
|
by: DC Gringo |
last post by:
I have a datagrid that won't sort. The event handler is firing and return
label text, just not the sort.
Here's my Sub Page_Load and Sub DataGrid1_SortCommand:
--------------------
Private...
|
by: IGotYourDotNet |
last post by:
I'm trying to sort my grid and its working halfway, I can only sort the grid
once and it puts everything in ASC order (1234555666) and i can't sort it
again to display as DESC order (6665554321),
...
|
by: TM |
last post by:
I was able to find the code to list the name and email from the contacts in
Outlook 2003, but the sort property is not working.
Here is my sample code. For now I am just adding an item to a...
|
by: Neil Chambers |
last post by:
Hi All,
I'm looking to see if it's feasible to use the SortObjectCommand
included in the Microsoft.Powershell.Commands assembly.
I have a Dictionary
Dictionary<string, intd = new...
|
by: Vbbeginner07 |
last post by:
its about Sorting a list view
but its not working.
please help.......
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
'Determine whether the column is the same...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |