algoritmus

Sragner Miklós srami at kabelnet.hu
Sat Dec 2 14:17:04 CET 2006


{************************************************}
{                                                }
{ QuickSort Demo                                 }
{ Copyright (c) 1985,90 by Borland International }
{                                                }
{************************************************}

program QSort;
{$R-,S-}
uses Crt;

{ This program demonstrates the quicksort algorithm, which      }
{ provides an extremely efficient method of sorting arrays in   }
{ memory. The program generates a list of 1000 random numbers   }
{ between 0 and 29999, and then sorts them using the QUICKSORT  }
{ procedure. Finally, the sorted list is output on the screen.  }
{ Note that stack and range checks are turned off (through the  }
{ compiler directive above) to optimize execution speed.        }

const
  Max = 1000;

type
  List = array[1..Max] of Integer;

var
  Data: List;
  I: Integer;

{ QUICKSORT sorts elements in the array A with indices between  }
{ LO and HI (both inclusive). Note that the QUICKSORT proce-    }
{ dure provides only an "interface" to the program. The actual  }
{ processing takes place in the SORT procedure, which executes  }
{ itself recursively.                                           }

procedure QuickSort(var A: List; Lo, Hi: Integer);

procedure Sort(l, r: Integer);
var
  i, j, x, y: integer;
begin
  i := l; j := r; x := a[(l+r) DIV 2];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

begin {QuickSort};
  Sort(Lo,Hi);
end;

begin {QSort}
  Write('Now generating 1000 random numbers...');
  Randomize;
  for i := 1 to Max do Data[i] := Random(30000);
  Writeln;
  Write('Now sorting random numbers...');
  QuickSort(Data, 1, Max);
  Writeln;
  for i := 1 to 1000 do Write(Data[i]:8);
end.

----- Original Message -----
From: "Papp Zoltán" <zombi at c2.hu>
To: <elektro at tesla.hu>
Sent: Friday, December 01, 2006 12:10 AM
Subject: Re: algoritmus


> 2006.11.30. 20:53:45 dátumon vajk fekete <halaloszto at yahoo.co.uk> írta:
>
> > 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.
>
> Hát kérem nemhiába Pongor írta. Te nem vizsgázál nála? Amikor elénktolta a
> 3 soros C programot (faltól falig minden sor), hogy mit ír ki, tele
> c=++a+b+++++c és hasonló hülyeségekkel.
> Ráadásul papíron kellett (szintaktikailag is helyes!) tökéletes programot
> írni...
>
> --
> Papp Zoltán
> OneWay Electronics
>
>
>



More information about the Elektro mailing list