954,170 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sorting Array Help

Ok I am new to C++ and I am having a hard time trying to figure out how to Sort the array I have created. I dont know if I am on the wrong track or if I am on the right track. Someone PLEASE help!! I have to sort some integers that are given by the user using a sort method like bubble sort. Please HELP!!!

Thanks for your help in advance. Below is what I have so far.

#include
#include


using namespace std;


int main()

{
int number1;
cout << "Please enter number1: ";
cin >> number1;

int number2;
cout << "Please enter number2: ";
cin >> number2;

int number3;
cout << "Please enter number3: ";
cin >> number3;

int number4;
cout << "Please enter number4: ";
cin >> number4;

cout << number1 << endl;
cout << number2 << endl;
cout << number3 << endl;
cout << number4 << endl;

system( "PAUSE");

return 0;
}

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Ok I am new to C++ and I am having a hard time trying to figure out how to Sort the array I have created. I dont know if I am on the wrong track or if I am on the right track. Someone PLEASE help!! I have to sort some integers that are given by the user using a sort method like bubble sort. Please HELP!!!

Thanks for your help in advance. Below is what I have so far.

#include #include

using namespace std;

int main()

{ int number1; cout << "Please enter number1: "; cin >> number1;

int number2; cout << "Please enter number2: "; cin >> number2; int number3; cout << "Please enter number3: "; cin >> number3; int number4; cout << "Please enter number4: "; cin >> number4; cout << number1 << endl; cout << number2 << endl; cout << number3 << endl; cout << number4 << endl;

system( "PAUSE");

return 0; }

Life will be easier if you create an array rather than hard-code number1, number2, number3, number4. You'll want to create a boolean flag variable to keep track of whether there have been any swaps in a Bubble Sort pass. Within an outer do-while loop, you'll want a for-loop that compares neighboring array values and swaps them if necessary.

Repeat the do-while loop until you go through the entire for-loop without making any swaps. At that time all numbers will be in order and you'll be done, so exit the do-while loop. So your do-while condition will be checking that boolean flag variable to see if there have been any swaps. If there have, repeat the do-while loop. If not, don't.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Thanks for the advice. I had a feeling I was making this harder then it should be. I have a habit of that :). The problem I have with what you suggested is that I dont know how to make an array that will allow user input. Do you know of any good links or tutorials that I can look at that will help me?

Thanks Again

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Thanks for the advice. I had a feeling I was making this harder then it should be. I have a habit of that :). The problem I have with what you suggested is that I dont know how to make an array that will allow user input. Do you know of any good links or tutorials that I can look at that will help me?

Thanks Again

Not much to it. Declaring the array and reading numbers into the array are separate problems. Assuming you haven't learned dynamic arrays yet, you can make an array bigger than it possibly needs to be:

const int MAX_SIZE = 1000;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 1000): ";
cin >> numElements;

for (int i = 0; i < numElements; i++)
{
    cout << "Enter number " << i << " : ";
    cin >> numbers[i];
}

// numbers array is filled with data.  Now sort it.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Thanks So much. Now I just need the sort function. I appreciate all your help thank you sooooo much!!!

you can make an array bigger than it possibly needs to be:

const int MAX_SIZE = 1000;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 1000): ";
cin >> numElements;

for (int i = 0; i < numElements; i++)
{
    cout << "Enter number " << i << " : ";
    cin >> numbers[i];
}

// numbers array is filled with data.  Now sort it.


[/QUOTE]

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Ok so if I was going to sort the code you posted would this code be correct?

for(top=i; i>1; i--)
{
i = 1;
for(i=0;iconst int MAX_SIZE = 1000;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 1000): ";
cin >> numElements;

for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}

// numbers array is filled with data. Now sort it.

[/QUOTE]

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Not quite. Your outer loop is going down and the inner loop is going down, both loops should go the same way. Your inner loop would be a good outer loop to control which element of the array do you start with on any given sequence of the inner loop if n is 1 less than the number of elements in the loop.

You also have a typo, it's a[n - 1] instead of a{n - 1], and most of the implementations I've seen compar a[x] to a[x + 1] rather than a[x - 1].

The sequence to swap two elements would be:
temp = a[x + 1]; //store one element in a third variable of the same type
a[x + 1] = a[x]; //assign the second element to the first element
a[x] = temp; //assign the value of the third variable to the second element

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

AHHH I guess Im an Idiot or something. I am just frustrated now and I know how the code is but I dont know how to make it work with my program I have this code. Can you give me some guide on how to make it work with the code you posted?

{
void bubble_sort(apvector &array)
{
int i, j, flag = 1; // set flag to 1 to begin initial pass
int temp; // holding variable
int arrayLength = array.length( );
for(i = 1; (i <= arrayLength) && flag; i++)
{
flag = 0;
for (j=0; j < (arrayLength -1); j++)
{
if (array[j+1] > array[j]) // ascending order simply changes to <
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return;

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 
for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
  for (j = i; j < arrayLength; j++) //start at i
 {
    if (array[j + 1] > array[j]) // ascending order simply changes to <
   { 
      temp = array[j]; // swap elements
      array[j] = array[j + 1];
      array[j + 1] = temp;
    }
  }
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Ok I have combined that with the code you posted first just to see what it would do and I get a lot of undeclared identifiers. Now to declare them would just simply be the :
int arrayLenght;
int j;

etc... Right? Here is the whole code.

#include
#include


using namespace std;


int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort

for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < arrayLength; j++) //start at i
{
if (array[j+1] > array[j]) // ascending order simply changes to <
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
}
}
}for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < arrayLength; j++) //start at i
{
if (array[j+1] > array[j]) // ascending order simply changes to <
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Ok I have combined that with the code you posted first just to see what it would do and I get a lot of undeclared identifiers. Now to declare them would just simply be the : int arrayLenght; int j; etc... Right? Here is the whole code.

#include #include

using namespace std;

int main() { const int MAX_SIZE = 10; int numbers[MAX_SIZE]; int numElements; cout << "Enter number of elements (2 - 10): "; cin >> numElements; for (int i = 0; i < numElements; i++) { cout << "Enter number " << i << " : "; cin >> numbers[i]; } //Sort

for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1 { for (j = i; j < arrayLength; j++) //start at i { if (array[j+1] > array[j]) // ascending order simply changes to < { temp = array[j]; // swap elements array[j] = array[j+1]; array[j+1] = temp; } } }for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1 { for (j = i; j < arrayLength; j++) //start at i { if (array[j+1] > array[j]) // ascending order simply changes to < { temp = array[j]; // swap elements array[j] = array[j+1]; array[j+1] = temp; } } }

Lerner and I used different variable names in our examples so you can't just combine them. You have to pick variable names and stick with them. You can't declare variable names of numElements and numbers, then use variable names of arrayLength and array and expect the compiler to realize what they refer to, so pick one set of names (say numElements and numbers) and change the other set of names (arrayLength and array) to those names. Or vice versa.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Ok I changed that so now all the elements are the same. But there is still the undeclared i and J's do I just declare those or do I need to change them also?

Here is the code:

#include
#include


using namespace std;


int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort

for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (array[j+1] > numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j+1] > numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Ok I changed that so now all the elements are the same. But there is still the undeclared i and J's do I just declare those or do I need to change them also?

Here is the code:

#include #include

using namespace std;

int main() { const int MAX_SIZE = 10; int numbers[MAX_SIZE]; int numElements; cout << "Enter number of elements (2 - 10): "; cin >> numElements; for (int i = 0; i < numElements; i++) { cout << "Enter number " << i << " : "; cin >> numbers[i]; } //Sort

for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1 { for (j = i; j < numElements; j++) //start at i { if (array[j+1] > numbers[j]) // ascending order simply changes to < { temp = numbers[j]; // swap elements numbers[j] = numbers[j+1]; numbers[j+1] = temp; } } }for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1 { for (j = i; j < numElements; j++) //start at i { if (numbers[j+1] > numbers[j]) // ascending order simply changes to < { temp = numbers[j]; // swap elements numbers[j] = numbers[j+1]; numbers[j+1] = temp; } } }

Code tags make things easier to read:
[code=cplusplus]
// paste code here
[/code]


Every variable you use needs to be declared. The name change had to do with the fact that you had two variables with different names which were referring to the same thing. The compiler had no idea of that. i and j are counters and in your case don't have that problem. Just be careful not to confuse i and j. Just declare them as integers. There is nothing to be renamed. You need to change arrayLength in your comment too because no one reading will know that that means numElements. They'll probably be able to figure it out by the similar name, but you should change it to the real variable name.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

WOW its amazing what happens when someone smart knows :). Ok I got that working but the problem I have now is that the sorted list is not showing on the screen. Here is the code:

#include
#include


using namespace std;


int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
int i;
int j;
int temp;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort

for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j+1] < numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is numElements - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j+1] < numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap numbers
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}

system("Pause");
return 1;
}

NoGood123
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Part of learning how to code is to try to take what you've already learned and try to use it in a new situation.

It looks like you've recopied the sorting code instead of trying to output the values of array, at least you have two copies of the sorting algorithm and nothing that would output the results of the sorting.

To output the sorted array elements to the screen just use a loop to access every valid element of the array and write code to output each element of array one at a time each time through the loop from within the body of the loop. It's actually a lot shorter to write the code than it is to tell you how to do it!

EDIT: and learn how to use code tags soon if you're going to continue to post code to the board.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

WOW its amazing what happens when someone smart knows :). Ok I got that working but the problem I have now is that the sorted list is not showing on the screen. Here is the code:

#include #include

using namespace std;

int main() { const int MAX_SIZE = 10; int numbers[MAX_SIZE]; int numElements; int i; int j; int temp; cout << "Enter number of elements (2 - 10): "; cin >> numElements; for (int i = 0; i < numElements; i++) { cout << "Enter number " << i << " : "; cin >> numbers[i]; } //Sort

for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1 { for (j = i; j < numElements; j++) //start at i { if (numbers[j+1] < numbers[j]) // ascending order simply changes to < { temp = numbers[j]; // swap elements numbers[j] = numbers[j+1]; numbers[j+1] = temp; } } }for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is numElements - 1 { for (j = i; j < numElements; j++) //start at i { if (numbers[j+1] < numbers[j]) // ascending order simply changes to < { temp = numbers[j]; // swap numbers numbers[j] = numbers[j+1]; numbers[j+1] = temp; } } }

system("Pause"); return 1; }

You need to format your code so it's readable and use code tags (see how above). The reason it's not displaying is because you aren't telling it to display anywhere. There are no cout statements after everything is sorted. Plus it seems like you are sorting twice with two back to back nested for loops.

#include <stdlib.h>
#include <iostream>


using namespace std;

int main() 
{
    const int MAX_SIZE = 10;
    int numbers[MAX_SIZE];
    int numElements;
    int i;
    int j;
    int temp;
    cout << "Enter number of elements (2 - 10): ";
    cin >> numElements;
    for (int i = 0; i < numElements; i++) 
    {
        cout << "Enter number " << i << " : ";
        cin >> numbers[i];
    }
    //Sort

    for (i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
    {
        for (j = i; j < numElements; j++) //start at i
        {
            if (numbers[j + 1] < numbers[j]) // ascending order simply changes to <
            {
                temp = numbers[j]; // swap elements
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
            }
        }
    }
    for (i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is numElements - 1
    {
        for (j = i; j < numElements; j++) //start at i
        {
            if (numbers[j + 1] < numbers[j]) // ascending order simply changes to <
            {
                temp = numbers[j]; // swap numbers
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
            }
        }
    }

    system("Pause");
    return 1;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
	const int MAX_SIZE = 10;
	int numbers[MAX_SIZE];
	int numElements;
	cout << "Enter number of elements (2 - 10): ";
	cin >> numElements;
	
	for (int i = 0; i < numElements; i++)
	{
		cout << "Enter number " << i << " : ";
		cin >> numbers[i];
	}
//Sort

for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
	for (j = i; j < arrayLength; j++) //start at i
	{

		if (array[j+1] > array[j]) // ascending order simply changes to <
		{
			temp = array[j]; // swap elements
			array[j] = array[j+1];
			array[j+1] = temp;
		}
	}
}

[TEX]
**Please wrap your coding......
[/TEX]


1. See line 14. Your "i" is declared in the for loop, which means that the int i will only be valid in the for loop. After the for loop exit, it will no longer be valid. If you want to use "i" for several times, I suggest you to declare it as a global variable. Read more on the scope of a declared variable.

2. "Arraylength" is just like the "i" you used just now. To use it, you must first declare and initialize it with a value. Or you can replace the arraylength with sizeof(numbers).

3. You declare your array as numbers at line 9 right? But in line 26 suddenly you refer your array as "array" which is not declared or initialized earlier.

4. Some of the variable you used is not declared as well, such as "temp" and "j". Remember to declare any variable you use and it's a good habit to initialize it with a default value.

Try fix all these errors first..... : )

raul15791
Junior Poster
102 posts since Jun 2008
Reputation Points: 37
Solved Threads: 7
 

if u want to just sort an array use this syntax
sort(array_name,array_name+size _of_array);
//in ur case it would be
sort(numbers,numbers+numElements);
// then run this loop for displaying the outout
for(i=0;i
u dont need to iterate ..or use while loops if u are interested in sorting only
and include this header file in the begining#include
usually its not required in my system but as a precaution use it

Arpy Giri
Newbie Poster
14 posts since Jan 2008
Reputation Points: 11
Solved Threads: 3
 

Tremendous Saga of Bubble Sorting!
Thank you,
all members of this Instant/FastFood C++ society!

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You