Why wouldn't you just assign the array members 1-9?
int i, array[9];
for (i = 0; i < 9; ++i)
{
array[i] = i + 1;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>You need a bubble sort algo.
Please don't suggest bubble sort if other options are available.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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]
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401