C++ Reorder random numbers

Reply

Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

C++ Reorder random numbers

 
0
  #1
Feb 26th, 2005
I have random numbers such as
1 6 8 5 7 3 9 2 4

in a 1 dimensional array

what for loop do I use to reorder the numbers from 1 through 9?

thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,301
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 225
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ Reorder random numbers

 
0
  #2
Feb 26th, 2005
Why wouldn't you just assign the array members 1-9?
  1. int i, array[9];
  2. for (i = 0; i < 9; ++i)
  3. {
  4. array[i] = i + 1;
  5. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: C++ Reorder random numbers

 
0
  #3
Feb 26th, 2005
You need a bubble sort algo.

You need to compare the first number to the second, and swap accordingly.

or...what you could do, which would be a lot easier

assign a temp array of the same size,
run a loop to find the smallest number
plop the result in the first element of the second array
set the value u just moves in the first array to zero
run the loop 9 time.

Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,537
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Reorder random numbers

 
0
  #4
Feb 26th, 2005
>You need a bubble sort algo.
Please don't suggest bubble sort if other options are available.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,837
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 861
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: C++ Reorder random numbers

 
0
  #5
Feb 27th, 2005
How about a shell-sort, this one has three nested for loops ...
[php]
// shell sort routine of n items in array[n]
void shellsort(int *array, int n)
{
int basectr, curctr, gapctr, gap, temp;

// reduce gap by 1/2 each loop
for(gap = n / 2; gap > 0; gap /= 2)
{
for(basectr = gap; basectr < n; basectr++)
{
for(curctr = basectr - gap; curctr >= 0; curctr -= gap)
{
gapctr = curctr + gap;
if (array[curctr] > array[gapctr])
{
temp = array[curctr];
array[curctr] = array[gapctr];
array[gapctr] = temp;
}
else break;
}
}
}
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: C++ Reorder random numbers

 
0
  #6
Mar 1st, 2005
Originally Posted by Narue
>You need a bubble sort algo.
Please don't suggest bubble sort if other options are available.
Why? Learning a bubble sort is a good practice for learning C with arrays, and the such.

:rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,537
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Reorder random numbers

 
0
  #7
Mar 1st, 2005
>Why?
Because bubble sort is one of the worst sorting algorithms invented, promotes bad habits, and any of the other quadratic sorts are equally good practice. I personally believe that bubble sort should only be used as an example of how not to sort data.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: C++ Reorder random numbers

 
0
  #8
Mar 15th, 2005
Originally Posted by Narue
>Why?
Because bubble sort is one of the worst sorting algorithms invented, promotes bad habits, and any of the other quadratic sorts are equally good practice. I personally believe that bubble sort should only be used as an example of how not to sort data.
That is a good response, and as such, an understandble way of looking at a bad algo for getting the job done.

Sadly though, it is still taught, and without your explination as to why not to suggest this algo, was a little put off(your first post). As a comp sci student who has had years of C++ experience, it seemed like a smart way to start learning sorting. Is the speed of the algo is in question, or just he methodology? Time for sorting with numbers under the curve of the function for the algo (which I believe this appilcation would have been ok for) are minimal. We are talking milliseconds. understandbly when the dataset moves into the range of millions, there are other methods to improve overall performace time. As for the implementaton, I tend to agree with you as it tends messy.



Now that I can understand where you are coming from, I won't suggest it again.

Gnome
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Re: C++ Reorder random numbers

 
0
  #9
Mar 15th, 2005
how can you reorder numbers that are user input using selection sort in a 1 dimension array and output each element
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,537
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Reorder random numbers

 
0
  #10
Mar 15th, 2005
>Sadly though, it is still taught
Indeed.

>Is the speed of the algo is in question
Yes, always. Bubble sort (as taught) does a lot of unnecessary work. It can be improved slightly, but that's almost always left as an exercise.

>understandbly when the dataset moves into the range of millions
Thousands. Bubble sort's growth is such that even sorting 10,000 records could be noticeably slow (less than that if the records are non-trivial). If you have millions of records then a suitable data structure or an nlogn sort would be more appropriate. The quadratic sorts are only useful for small data sets, or as components in implementing a more sophisticated algorithm.

>how can you reorder numbers that are user input using selection sort in a 1 dimension array and output each element
You've asked this same question four times if I recall correctly. I also recall answering it fully, with code examples. What's the problem?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC