Public Group

# array sorting problem

This topic is 5400 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

////////////////edit///////////////////////////////// well have 2 arrays the each have five elements un sorted array holds 42,68,35,1,70 completely sorted array holds 1,35,42,68,70 the array with 3 elemements sorted holds 35,42,68,1,70 so why is there not 3 elements sorted??? shouldn't it be 1,35,42,70,68 or something like that???? ////////////////edit///////////////////////////////// i have this code that prints out two arrays one sorted and another unsorted. i need to have the output in neatly right-justified columns. i can get the left one right justified but why not the right handed one??


#include <iostream>
#include <iomanip>
using namespace std;

void bubbleSort(int numbers[], int array_size);
int main()
{
int max = 20;

int * array1 = new int[max];
int * array2 = new int[max];

for (int x = 0; x < max; x++)
{
array1[x] = (rand()%100) + 1;
array2[x] = array1[x];
}

bubbleSort(array2, max);

for (x = 0; x < max; x++)
{
cout.width(2);
cout.fill(' ');
cout <<setiosflags(ios::right)<< array1[x] << ' ' << array2[x] << endl;
}

delete array1;
delete array2;

return 0;
}

void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;

for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j - 1] > numbers[j])
{
temp = numbers[j - 1];
numbers[j - 1] = numbers[j];
numbers[j] = temp;
}
}
}
}


[Edited by - ostamo1 on October 6, 2004 11:58:16 AM]

##### Share on other sites
You need to specify the width of the output twice. cout.width(2) only affects the next thing outputted. So if you change your code to this, it will format both numbers correctly.

#include <iostream>#include <iomanip>using namespace std;void bubbleSort(int numbers[], int array_size);int main(){	int max = 20;		int * array1 = new int[max];	int * array2 = new int[max];					for (int x = 0; x < max; x++) 	{		array1[x] = (rand()%100) + 1;		array2[x] = array1[x];	}		bubbleSort(array2, max);		for (x = 0; x < max; x++) 	{		cout.fill(' ');		cout << setw(2) << setiosflags(ios::right) << array1[x] << ' ' << setw(2) << array2[x] << endl;	}		delete array1;	delete array2;		return 0;}void bubbleSort(int numbers[], int array_size){	int i, j, temp;		for (i = (array_size - 1); i >= 0; i--)	{		for (j = 1; j <= i; j++)		{			if (numbers[j - 1] > numbers[j])			{				temp = numbers[j - 1];				numbers[j - 1] = numbers[j];				numbers[j] = temp;			}		}	}}

cout << setw(2);
is equivalent to
cout.width(2);

##### Share on other sites
how do i get the users input on how many numbers they'd like to sort. iwill ask them, and then i will sort however many they tell me.
i know i need to use pointers

don't give me the exact answer just point me in the right direction please

and thanks for your help rating++

##### Share on other sites
You don't need pointers for input, just use cin!

int number;
cin >> number;
// number is inputted

##### Share on other sites
i know how to get the input.
i want the user to pick a number between one and 20

then if they choose 5 i only want 5 elements in the array sorted
and print out the unsorted array and the array with only 5 things sorted side by side in columns

sorry i did not explain more clearly

##### Share on other sites
Since you don't want the exact answer, you already know how to input a number, and the solution seems to be pretty simple from the way I understand it, here's my hint:

It looks rather convenient that the bubbleSort function has a parameter for the array size.

##### Share on other sites
well have 2 arrays the each have five elements

un sorted array holds
42,68,35,1,70

completely sorted array holds
1,35,42,68,70

the array with 3 elemements sorted holds
35,42,68,1,70

so why is there not 3 elements sorted???
shouldn't it be
1,35,42,70,68 or something like that????

##### Share on other sites
can anyone help me?

##### Share on other sites
Quote:
 Original post by ostamo1the array with 3 elemements sorted holds35,42,68,1,70so why is there not 3 elements sorted???

The three boldface elements are sorted, what's the problem?

##### Share on other sites
Quote:
 Original post by ostamo1well have 2 arrays the each have five elementsun sorted array holds42,68,35,1,70completely sorted array holds1,35,42,68,70the array with 3 elemements sorted holds35,42,68,1,70so why is there not 3 elements sorted???shouldn't it be1,35,42,70,68 or something like that????

To me, sorting the first three elements is accomplished by taking the first three elements (ignoring the rest) and sorting them. In that case, those are the correct answers, because 1 is not one of the first three elements, and is not a part of the bubble sort.

It seems like you want the array sorted completely, then every element past the first 3 to be in a random order?

Using a bubblesort, there's no way to sort an array to retrieve the first three elements only, because the entire array is changed during the sort. That's just the way it works. If you came up with a different sorting method, you could maintain each element's order, and move the three smallest elements to the front.

But before I spend time explaining how that can be done...
This sounds a little bit like a homework problem, since you asked specifically for hints and no answers. If it is in fact an assigned problem, what was the exact wording it asked?
Since you seem to be interpreting "sorting the first three elements" differently than some of us.

If it is not a problem written by someone else, I apologize. I'm afraid I don't fully understand what you're trying to do, unless my above guess is correct.

• 18
• 18
• 11
• 21
• 16