Author |
Message |
poll262
|
Posted: Sun Dec 16, 2012 3:23 pm Post subject: Sorting Algorithm Help |
|
|
I am just starting to learn C++ and have written a few simple programs. I am now trying to make a program that asks the user for ten numbers, stores them in an array and then sorts the array. I am using the bubble sort method but, for some reason the sorting algorithm I wrote does not work. The numbers will be out of order and sometimes numbers that the user did not input will be in the array. Here is what I have done so far. Could someone tell me what I am doing wrong?
c++: |
#include <iostream>
using namespace std;
int main()
{
int number[10];
int temp;
for (int i=0; i<=9; i++)
{
cout << "Enter #" << i + 1 << ": ";
cin >> number[i];
}
for (int j=0; j<=9; j++)
{
for (int k=0; k<=8; k++)
{
if (number[k] > number[k+1])
temp = number[k + 1];
number[k+1] = number[k];
number [k] = temp;
}
}
cout << "Sorted Numbers" << endl;
for (int l=0; l<=9; l++)
{
cout << number[l] << endl;
}
return 0;
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Panphobia
![](http://compsci.ca/v3/uploads/user_avatars/62033050050c6b61548706.png)
|
Posted: Sun Dec 16, 2012 3:54 pm Post subject: Re: Sorting Algorithm Help |
|
|
Here is some pseudocode that works perfectley code: | bottom = numItems - 1
sw = true
while sw = true
sw = false
for j = 0 to bottom
if A[j] > A[j+1] then
swap(A[j], A[j+1])
sw = true
next j
bottom = bottom - 1
end while |
|
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Sun Dec 16, 2012 3:58 pm Post subject: RE:Sorting Algorithm Help |
|
|
Could you explain how this works? I don't fully understand. |
|
|
|
|
![](images/spacer.gif) |
Panphobia
![](http://compsci.ca/v3/uploads/user_avatars/62033050050c6b61548706.png)
|
Posted: Sun Dec 16, 2012 4:00 pm Post subject: RE:Sorting Algorithm Help |
|
|
Ok, basically it checks two different parts of the array side by side and then checks which is bigger and it switches them if the first is bigger than the second, this way the biggest values bubble up the array while the smallest go to the bottom |
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Sun Dec 16, 2012 4:06 pm Post subject: RE:Sorting Algorithm Help |
|
|
Thanks for the help. I was able to fix the issue by replacing the section where I swapped the two numbers with the swap function you used in your example. I'm not sure why this fixed it though. |
|
|
|
|
![](images/spacer.gif) |
Panphobia
![](http://compsci.ca/v3/uploads/user_avatars/62033050050c6b61548706.png)
|
Posted: Sun Dec 16, 2012 4:06 pm Post subject: RE:Sorting Algorithm Help |
|
|
if you are having trouble with the bubble sort, try the selection sort first, i would think that is the most simple, if you want me to explain it, this is what it does, it scans the whole array for the smallest or biggest value then puts it at the beginning of the array then the loop increments by 1 and it starts at the second point and checks for the smallest in the new parameters of the array and keeps doing this until the end |
|
|
|
|
![](images/spacer.gif) |
Panphobia
![](http://compsci.ca/v3/uploads/user_avatars/62033050050c6b61548706.png)
|
Posted: Sun Dec 16, 2012 4:34 pm Post subject: Re: Sorting Algorithm Help |
|
|
I can see that the bubble sort in java and c++ are written a little bit differentley but they do the same thing here is some code that does the job code: | #include <iostream>
using namespace std;
int n[10];
void bubble(){
int bottom = 9;
while(bottom > 0){
for (int j=0; j < bottom; j++)
{
if (n[j+1] > n[j]) // ascending order simply changes to <
{
int temp = n[j]; // swap elements
n[j] = n[j+1];
n[j+1] = temp;
}
}
bottom--;
}
}
int main(){
for(int i = 0; i < 10;++i) cin >> n[i];
bubble();
for(int i = 0; i < 10;++i) cout << n[i] << endl;
return 0;
} |
|
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Sun Dec 16, 2012 4:52 pm Post subject: RE:Sorting Algorithm Help |
|
|
Thanks! This helped me out a lot. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Panphobia
![](http://compsci.ca/v3/uploads/user_avatars/62033050050c6b61548706.png)
|
Posted: Sun Dec 16, 2012 4:55 pm Post subject: RE:Sorting Algorithm Help |
|
|
May I ask, is this for a programming assignment, because last unit in my grade 12 programming class it was searching/sorting ![Razz Razz](http://compsci.ca/v3/images/smiles/icon_razz.gif) |
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Sun Dec 16, 2012 5:04 pm Post subject: RE:Sorting Algorithm Help |
|
|
No I'm just teaching myself C++ in my spare time. I haven't started that course yet. I am taking it next semester. |
|
|
|
|
![](images/spacer.gif) |
|