Re: Sorting
- From: "Army1987" <please.ask@xxxxxx>
- Date: Sat, 28 Apr 2007 14:56:38 +0200
"Malcolm McLean" <regniztar@xxxxxxxxxxxxxx> ha scritto nel messaggio
news:eLmdnR7M5KYws67bnZ2dnUVZ8vWdnZ2d@xxxxxxxxx
"Army1987" <please.ask@xxxxxx> wrote in message
news:f0v338$32j$1@xxxxxxxxxxxxxxx
The algorithm isn't conceptually the simplest. But there is no memory
"Malcolm McLean" <regniztar@xxxxxxxxxxxxxx> ha scritto nel messaggio
news:77OdnWahTNmt5a_bRVnyvwA@xxxxxxxxx
There are lots of ways of sorting.
The easiest, probably, is to go through the array. Compare the number
you are on with the number above it. If they are in the wrong order,
swap them. Then go back to the start of the array and do it again, until
you make a clean sweep without swapping. Set a flag when you swap, and
clear it each time you restart.
This algorithm is called bubblesort.
What the hell? "Easiest"? I would have never thought of anything
similar, and when I was taught about it, I wandered why did anybody
bother to think and implement such a horrid algorithm. The answer
"it's a didactic toy" is the one which convices me best. It is even
less efficient than selection sort or insertion sort, which are
more human-comprehensible. (Which do you use to physically sort a deck of
playing cards? I don't believe you use bubblesort.)
management to speak of, so it is generally easier to implement.
Easier than what? What memory management is below?
size_t find_max(int array[], size_t length);
void selection_sort(int array[], size_t length)
{
size_t m;
for( ; length > 0; length--, array++) {
m = find_max(array, length);
if (m != 0) {
int tmp = array[0];
array[0] = array[m];
array[m] = tmp;
}
}
return;
}
size_t find_max(int array[], size_t length)
{
size_t i, result = 0;
for (i=1; i<length; i++)
if (array[i] > array[result])
result = i;
return result;
}
.
- Follow-Ups:
- Re: Sorting
- From: Malcolm McLean
- Re: Sorting
- References:
- Sorting
- From: Umesh
- Re: Sorting
- From: Malcolm McLean
- Re: Sorting
- From: Army1987
- Re: Sorting
- From: Malcolm McLean
- Sorting
- Prev by Date: random password generator
- Next by Date: Re: C a Dead Language
- Previous by thread: Re: Sorting
- Next by thread: Re: Sorting
- Index(es):
Relevant Pages
|
Loading