algoritmus

vajk fekete halaloszto at yahoo.co.uk
Fri Dec 1 10:25:52 CET 2006


Most reszletezzem:
 - kenyszeres pointeraritmetika hasznalat, de pl a reference inicializalasanal megis inkabb a tombindex szintakszist hasznalja.
 - a *left++=*right semmivel nem fordul hatekonyabbra mint a *left=*right ; left++; csak nehezebb elolvasni.
 - ha a fenti modon kenyszeresen tomorit, akkor a while-t is irhatta volna igy: while (reference_key<(right--)->key); csak akkor meg lehetetlenebb elolvasni
 - egy az algoritmus megerteset szolgalo oktatasi celo source-ban nem az erthetosegre kellene torekedni?

Amugy en is irtam olyan aritmetikai algoritmusokat, hogy az ilyen ekv. atalakitasok utan harom sor krikszkraksz volt az egesz de mukodott. Viszont 2 honap mulva orakba tellett megertenem hogy mit csinal. Pedig en irtam!

vajk

----- Original Message ----
From: Palasik Sandor <palasik at mail.datanet.hu>
To: elektro at tesla.hu
Sent: Friday, 1 December, 2006 9:55:27 AM
Subject: Re: algoritmus

> 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

-----------------------------------------
          elektro[-flame|-etc]






Send instant messages to your online friends http://uk.messenger.yahoo.com 


More information about the Elektro mailing list