algoritmus
Palasik Sandor
palasik at mail.datanet.hu
Fri Dec 1 09:55:27 CET 2006
> void quick_sort (element_t *table, int length)
> {
> element_t temp, *left=table, *right=table+length-1;
> keytype reference_key = table[length/2].key; /* for partitioning */
>
> do { /* partitioning */
> while (left->key < reference_key) left++; /* search from left */
> while (reference_key < right->key) right--; /* search from right */
> if (left <= right) {
> temp = *left; *left++ = *right; *right-- = temp; /* swap, then
> ...*/
> } /* ... bypass swapped
elements
> */
> } while (left <= right);
> if (table<right) quick_sort (table,right-table+1);
> if (left<table+length-1) quick_sort(left,table+length-left);
> }
>
>
> Hat ez tokeletes pelda arra hogyan lehet a C sajatossagait kihasznalni
> olvashatatlan,
> karbantarthatatlan kod irasara.
Konkrétan mi vele a gondod? Egy két plusz soremelést leszámítva kb. én is
így írnám.
Más: nekem tetszik még a "fésű rendezés", COMBSORT. A következő oldalon
szépen körbejárják:
http://yagni.com/combsort/
Majdnem olyan egyszerű, mint a buborék és majdnem olyan gyors, mint a
quicksort és nem kell hozzá rekurzió, még szimulálva sem.
Arról az oldalról Basic-ban:
sub combsort(a, aLength)
gap = aLength
repeat
gap = int((gap * 10) / 13)
if gap = 9 or gap = 10 then gap = 11 endif
if gap < 1 then gap = 1 endif
swapped = 0
for i = 1 to aLength - gap
j = i + gap
if a(i) > a(j) then
temp = a(i)
a(i) = a(j)
a(j) = temp
swapped = 1
endif
next i
until (gap = 1 and not swapped)
end sub
Ha a gap nevű változót fix 1-re állítjuk, akkor kapjuk
a buborékrendezést.
Palasik Sándor
More information about the Elektro
mailing list