home *** CD-ROM | disk | FTP | other *** search
- (* Quick-Sort *)
- (* Originally translated into Pascal by *)
- (* Brian Solan *)
- (* Translated into OSS Personal Pascal by *)
- (* Mike Matthews *)
- (* *)
- (* (anyone for C?) *)
- (* *)
- (* Sorts 500 numbers in less than 5 seconds! *)
- (* Do with it what you please *)
- (* *)
- (* It's as complicated as it is fast, so I *)
- (* won't even try to explain how it works. *)
- (* Except that 'n' is the array to be sorted *)
- (* and 's9' takes really no more than 20x2, *)
- (* for 500 numbers. *)
- (* (but I have RAM to spare...) *)
- (* *)
-
- Program quick_sort(input,output);
-
- type oned=array[1..5000] of integer;
- twod=array[1..5000,1..2] of integer;
-
- var i1,i,j1,j,k:integer;
- p,t,amt,m:integer;
- s:boolean;
- n:oned;
- s9:twod;
- printer:file of text;
- choice:char;
-
- Function random:Long_Integer;
- XBIOS(17);
-
- Procedure switch(var a,b:integer;var s1:boolean);
-
- var t:integer;
-
- begin
- t:=a;
- a:=b;
- b:=t;
- s1:=not s1
- end;
-
- Procedure save1(var q:integer;var s8:twod;a,k1:integer);
-
- begin
- q:=q+1;
- s8[q,1]:=a+1;
- s8[q,2]:=k1
- end;
-
- Procedure restore(s8:twod;var i2,j2,q:integer);
-
- begin
- i2:=s8[q,1];
- j2:=s8[q,2];
- q:=q-1
- end;
-
- Procedure init(var a,b,a1,b1:integer;var es:boolean);
-
- begin
- a:=a1;
- b:=b1;
- es:=false
- end;
-
- Procedure print(n:oned;choice:char);
-
- var i:integer;
-
- begin
- if (choice='P') or (choice='p') then
- begin
- writeln(printer);
- writeln(printer,'Done!');
- for i:=1 to amt do
- begin
- write(printer,n[i],' ');
- if i mod 15=0 then writeln(printer)
- end;
- writeln(printer)
- end
- else
- begin
- writeln(output);
- writeln(output,'Done!');
- for i:=1 to amt do
- begin
- write(output,n[i],' ');
- if i mod 15=0 then writeln(output)
- end;
- writeln(output)
- end
- end;
-
- Procedure sort;
-
- begin
- repeat
- if n[i]>n[j] then switch(n[i],n[j],s);
- if s then i:=i+1
- else j:=j-1;
- until i=j;
- if not(i+1>=j1) then save1(p,s9,i,j1);
- j1:=i-1;
- if i1<j1 then
- begin
- init(i,j,i1,j1,s);
- sort
- end;
- if p<>0 then
- begin
- restore(s9,i1,j1,p);
- init(i,j,i1,j1,s);
- sort
- end
- end;
-
- begin
- rewrite(printer,'PRN:');
- i1:=1;
- p:=0;
- write('Enter number to sort ? ');
- readln(amt);
- write('[P]rinter or [S]creen ? ');
- readln(choice);
- j1:=amt;
- for m:=1 to amt do
- begin
- n[m]:=trunc((random)/16000)+1;
- if (choice='P') or (choice='p') then
- begin
- write(printer,n[m],' ');
- if m mod 15=0 then writeln(printer)
- end
- else
- begin
- write(output,n[m],' ');
- if m mod 15=0 then writeln(output)
- end
- end;
- init(i,j,i1,j1,s);
- sort;
- print(n,choice)
- end.
-