-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSORT.PAS
82 lines (75 loc) · 1.56 KB
/
SORT.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{ @author: Sylvain Maltais ([email protected])
@created: 2021
@website(https://www.gladir.com/linux-0)
@abstract(Target: Turbo Pascal, Free Pascal)
}
Program SORT(Input,Output);
Uses Dos;
Type
StrPointer=^String;
Var
I,_Index:Word;
P:StrPointer;
S:String;
PA:Array[1..12000] of StrPointer;
F:Text;
Procedure QuickSort(Left,Right:Word);
Var
Lower,Upper,Middle:Word;
Pivot,T:String;
Temp:StrPointer;
Begin
Lower:=Left;
Upper:=Right;
Middle:=(Left+Right) shr 1;
Pivot:=PA[Middle]^;
Repeat
While PA[Lower]^ < Pivot do Inc(Lower);
While Pivot < PA[Upper]^ do Dec(Upper);
If(Lower<=Upper)Then Begin
Temp:=PA[Lower];
PA[Lower]:=PA[Upper];
PA[Upper]:=Temp;
Inc(Lower);
Dec(Upper);
End;
Until Lower>Upper;
If Left<Upper Then QuickSort(Left,Upper);
If Lower<Right Then QuickSort(Lower,Right);
End;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('SORT : Cette commande permet de trier un fichier texte ASCII.');
WriteLn;
WriteLn('Syntaxe : SORT fichier');
End
Else
If ParamCount>=1Then Begin
Assign(F,ParamStr(1));
Reset(F);
_Index:=0;
While Not EOF(F) do Begin
ReadLn(F,S);
Inc(_Index);
GetMem(P,Length(S)+1);
P^:=S;
PA[_Index]:=P;
End;
Close(F);
If _Index>1 Then QuickSort(1,_Index);
For I:=1 to _Index do WriteLn(PA[I]^);
End
Else
Begin
_Index:=0;
While Not EOF do Begin
ReadLn(Input,S);
Inc(_Index);
GetMem(P,Length(S)+1);
P^:=S;
PA[_Index]:=P;
End;
If _Index>1 Then QuickSort(1,_Index);
For I:=1 to _Index do WriteLn(PA[I]^);
End;
END.