- Delphiartiklar, tips, forum, länksamling - 

      

START | DELPHI | LÄNKARGÄSTBOK 




 Forum

Grundkurs
  »Introduktion
  »Snabbguide
  »Komponenter
  »Händelser
  »Strängar
  »Räkna med Delphi   »Egna typer
  »Selektion
  »Iteration
  »Menyer
  »Funktioner
  »Arraystrukturer

Tips & Tricks
  »Nya tips
  »Blandat
  »Databaser
  »Filer
  »Forms
  »Grafik
  »Internet
  »Komponenter
  »Matematik
  »Multimedia
  »Objekt/ActiveX
  »Skrivare
  »Strängar
  »System
  »Mest lästa tips

Artiklar
  »Delphi och ADO
  »Bygga en DLL
  »Skapa en enkel rapport
  »Hantera registret
  »Enheter, units
  »Klassen TCanvas
  »Använd LookUp Controls

 Nya tips
 Lägg till tips
 Delphilänkar
 Gästbok

 

Sortera arrayer på olika sätt Kategori: Strangar
Inlagt: 2004-06-10
Läst: 1364
Inlagt av: Staffan Berg
Beskrivning
Exempel på olika sorteringsordningar av en array. 
Kod
unit QSort; 
 
interface 
type CompFunc = function(Item1, Item2 : word) : integer; 
 
procedure QuickSort( 
  var Data; 
//An array. Must be [0..Count-1] and not [1..Count] or anything else! 
  Count, 
//Number of elements in the array 
  Size  : word; 
//Size in bytes of a single element -- e.g. 2 for integers or words, 
4 for longints, 256 for string s and so on  
  Compare : CompFunc); 
//The function that decides which element is "greater" or "less". //Must return an integer that's < 0 if the first element is less, 0 if they're 
//equal and > 0 if the first element is greater. A simple Compare for words can look like this: 
 
function WordCompare(Item1, Item2: word): integer; 
begin 
   WordCompare := MyArray[Item1] - MyArray[Item2] 
end; 
 
 
implementation 
procedure QuickSort; 
 
 procedure Swap(Item1, Item2 : word); 
 var P1, P2 : ^byte; I : word; 
 begin 
   if Item1 <> Item2 then 
   begin 
     I := Size; 
     P1 := @Data; inc(P1, Item1 * Size); 
     P2 := @Data; inc(P2, Item2 * Size); 
     asm 
      mov cx,I   { Size } 
      les di,P1 
      push ds 
      lds si,P2 
     @L: 
      mov ah,es:[di] 
      lodsb 
      mov [si-1],ah 
      stosb 
      loop @L 
      pop ds 
     end 
   end 
 end; 
 
 procedure Sort(Left, Right: integer); 
 var i, j, x, y : integer; 
 begin 
   i := Left; j := Right; x := (Left+Right) div 2; 
   repeat 
    while compare(i, x) < 0 do inc(i); 
    while compare(x, j) < 0 do dec(j); 
    if i <= j then 
    begin 
      swap(i, j); inc(i); dec(j) 
    end 
   until i > j; 
   if Left < j then Sort(Left, j); 
   if i < Right then Sort(i, Right) 
 end; 
 
begin Sort(0, Count) end; 
 
end. //of unit 
 
//A simple testprogram can look like this: 
 
program QS_Test; //Test QuickSort  la C 
uses qsort; 
var v: array[0..9999] of word; 
  i: word; 
 
{$F+} //Must be compiled as FAR calls! 
function cmpr(a, b: word): integer; 
begin cmpr := v[a] - v[b] end; 
 
function cmpr2(a, b: word): integer; 
begin cmpr2 := v[b] - v[a] end; 
{$F-} 
 
begin 
randomize; 
for i := 0 to 9999 do v[i] := random(20000); 
quicksort(v, 10000, 2, cmpr); //in order lo to hi 
quicksort(v, 10000, 2, cmpr2); //we now have a sorted list, sort it in 
                //reverse -- nasty for qsort! 
quicksort(v, 10000, 2, cmpr); //and reverse again 
quicksort(v, 10000, 2, cmpr); //sort a sorted list -- also not very popular 
end. 

 
 
© Copyright 2005 - Staffan Berg
- Alla rättigheter förbehålles -