summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/amunits/examples/sortdemo.pas
blob: 66816576dcde85f854fc9035e24a60378a70968c (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
PROGRAM SortDemo;

{ Graphical demonstration of sorting algorithms (W. N~ker, 02/96) }
{ based on "Sortieren" of Purity #48 }

{
    Translated to PCQ from Kick(Maxon) Pascal.
    Updated the source to 2.0+.
    Now uses GadTools for menus.
    Added CloseWindowSafely.
    Cleaned up the menuhandling.
    Added LockWinSize and RestoreWin, now the
    window will be locked on showtime.

    The German text was translated to English
    by Andreas Neumann, thanks Andreas.
    Jun 03 1998.

    Translated to FPC Pascal.
    Removed CloseWindowSafely, have do add
    that procedure to Intuition.
    Fixed a bug, when you halt the show the
    window stayed locked.
    Aug 23 1998.

    Added MessageBox for report.
    31 Jul 2000.

    Removed opening of graphics.library.
    21 Mar 2001.

    Reworked to use systemvartags.
    28 Nov 2002.

    nils.sjoholm@mailbox.swipnet.se

    One last remark, the heapsort can't be stoped
    so you have to wait until it's finished.
}

uses Exec, Intuition, Graphics, Utility, GadTools, msgbox,systemvartags;


CONST
      vers : string = '$VER: SortDemo 1.3 ' + {$I %DATE%} + ' ' + {$I %TIME%}#0;

      nmax=2000;

      MinWinX = 80;
      MinWiny = 80;

      w         : pWindow  = Nil;
      s         : pScreen  = Nil;
      MenuStrip : pMenu    = Nil;
      vi        : Pointer  = Nil;


      modenames : Array[0..7] of string[10] = (
                                'Heapsort',
                                'Shellsort',
                                'Pick out',
                                'Insert',
                                'Shakersort',
                                'Bubblesort',
                                'Quicksort',
                                'Mergesort');

      { The easiest way to use gadtoolsmenus in FPC is
        to have them as const types. No need to cast
        strings to PChar. That we have to use recordmembers
        name is a pain.
      }

      nm : array[0..21] of tNewMenu = (
      (nm_Type: NM_TITLE; nm_Label: 'Demo';nm_CommKey: NIL; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'Start';nm_CommKey: 'S'; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'Stop';nm_CommKey: 'H'; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),

      { this will be a barlabel, have to set this one later }
      (nm_Type: NM_ITEM;  nm_Label: NIL; nm_CommKey: NIL; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),

      (nm_Type: NM_ITEM;  nm_Label: 'Quit';  nm_CommKey: 'Q'; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_TITLE; nm_Label: 'Algorithm'; nm_CommKey: NIL; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'HeapSort'; nm_CommKey: '1'; nm_Flags:
       CHECKIT+CHECKED+MENUTOGGLE; nm_MutualExclude: 254; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'ShellSort'; nm_CommKey: '2'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 253; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'Pick out'; nm_CommKey: '3'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 251; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'Insert'; nm_CommKey: '4'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 247; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'ShakerSort'; nm_CommKey: '5'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 239; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'BubbleSort'; nm_CommKey: '6'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 223; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'QuickSort'; nm_CommKey: '7'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 191; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'MergeSort'; nm_CommKey: '8'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 127; nm_UserData: NIL),
      (nm_Type: NM_TITLE; nm_Label: 'Preferences'; nm_CommKey: NIL; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'Data'; nm_CommKey: NIL; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_SUB;   nm_Label: 'Random'; nm_CommKey: 'R'; nm_Flags:
       CHECKIT+CHECKED+MENUTOGGLE; nm_MutualExclude: 2; nm_UserData: NIL),
      (nm_Type: NM_SUB;   nm_Label: 'Malicious'; nm_CommKey: 'M'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 1; nm_UserData: NIL),
      (nm_Type: NM_ITEM;  nm_Label: 'Diagram'; nm_CommKey: NIL; nm_Flags: 0;
       nm_MutualExclude: 0; nm_UserData: NIL),
      (nm_Type: NM_SUB;   nm_Label: 'Needles'; nm_CommKey: 'N'; nm_Flags:
       CHECKIT+CHECKED+MENUTOGGLE; nm_MutualExclude: 2; nm_UserData: NIL),
      (nm_Type: NM_SUB;   nm_Label: 'Dots'; nm_CommKey: 'D'; nm_Flags:
       CHECKIT+MENUTOGGLE; nm_MutualExclude: 1; nm_UserData: NIL),
      (nm_Type: NM_END;   nm_Label: NIL; nm_CommKey: NIL; nm_Flags:
       0;nm_MutualExclude:0;nm_UserData:NIL));


VAR sort: ARRAY[1..nmax] OF Real;
    sort2: ARRAY[1..nmax] OF Real;  { for dumb Mergesort %-( }
    num,range,modus : Integer;
    rndom,needles   : Boolean;
    Rast            : pRastPort;
    QuitStopDie     : Boolean;
    Msg             : pMessage;
    wintitle        : string[80];
    scrtitle        : string[80];

Procedure CleanUp(s : string; err : Integer);
begin
    if assigned(MenuStrip) then begin
       ClearMenuStrip(w);
       FreeMenus(MenuStrip);
    end;
    if assigned(vi) then FreeVisualInfo(vi);
    if assigned(w) then CloseWindow(w);
    if s <> '' then MessageBox('SortDemo Report',s,'OK');
    Halt(err);
end;

Procedure RestoreWin;
var
   dummy : Boolean;
begin
   dummy := WindowLimits(w,MinWinX,MinWinY,-1,-1);
end;

Procedure LockWinSize(x,y,x2,y2 : Integer);
var
   dummy : Boolean;
begin
   dummy := WindowLimits(w,x,y,x2,y2);
end;

FUNCTION cancel: Boolean;
{ checked while sorting }
VAR m,i,s: Integer;
    result : boolean;
    IM : pIntuiMessage;
BEGIN
  result := False;
  IM := pIntuiMessage(GetMsg(w^.UserPort));
  IF IM<>Nil THEN BEGIN
    IF IM^.IClass=IDCMP_CLOSEWINDOW THEN
      result := True;   { Close-Gadget }
    IF IM^.IClass=IDCMP_MENUPICK THEN BEGIN
      m := IM^.Code AND $1F;
      i := (IM^.Code SHR 5) AND $3F;
      s := (IM^.Code SHR 11) AND $1F;
      IF (m=0) AND (i=1) THEN  result := True;  { Menu item "Stop" }
    END;
    ReplyMsg(pMessage(Msg));
  END;
  cancel := result;
END;


PROCEDURE showstack(size: Integer);
{ little diagram showing the depth of Quicksort's recursion :-) }
BEGIN
  SetAPen(Rast,2); IF size>0 THEN RectFill(Rast,0,0,3,size-1);
  SetAPen(Rast,0); RectFill(Rast,0,size,3,size);
END;


PROCEDURE setpixel(i: Integer);
BEGIN
  SetAPen(Rast,1);
  IF needles THEN BEGIN
    Move(Rast,i,range); Draw(Rast,i,Round((1-sort[i])*range));
  END ELSE
    IF WritePixel(Rast,i,Round((1-sort[i])*range))=0 THEN;
END;

PROCEDURE clearpixel(i: Integer);
BEGIN
  SetAPen(Rast,0);
  IF needles THEN BEGIN
    Move(Rast,i,range); Draw(Rast,i,Round((1-sort[i])*range));
  END ELSE
    IF WritePixel(Rast,i,Round((1-sort[i])*range))=0 THEN;
END;

procedure Exchange(var first,second : real);
var
  temp : real;
begin
  temp := first;
  first := second;
  second := temp;
end;

PROCEDURE swapit(i,j: integer);
BEGIN
  clearpixel(i); clearpixel(j);
  Exchange(sort[i],sort[j]);
  setpixel(i); setpixel(j);
END;

FUNCTION descending(i,j: Integer): Boolean;
BEGIN
  descending := sort[i]>sort[j];
END;

Function IntToStr (I : Longint) : String;

     Var S : String;

     begin
      Str (I,S);
      IntToStr:=S;
     end;


PROCEDURE settitles(time: Longint);
VAR
  s : string[80];
BEGIN
  s := modenames[modus];
  IF time=0 THEN
    wintitle := s + ' running ...'
  ELSE IF time < 0 then
    wintitle := '<- ' + IntToStr(num) + ' Data ->'
  ELSE
    wintitle := IntToStr(time) + ' Seconds';
  scrtitle := strpas(@vers[6]) + ' - ' + s;
  wintitle := wintitle + #0;
  scrtitle := scrtitle + #0;
  SetWindowTitles(w,@wintitle[1],@scrtitle[1]);
END;

PROCEDURE refresh;
{ react on new size of window/init data }
VAR i: Integer;
BEGIN
  num := w^.GZZWidth; IF num>nmax THEN num := nmax;
  range := w^.GZZHeight;
  settitles(-1);
  SetRast(Rast,0);    { clear screen }
  FOR i := 1 TO num DO BEGIN
    IF rndom THEN sort[i] := Random  { produces 0..1 }
      ELSE sort[i] := (num-i)/num;
    setpixel(i);
  END;
END;

{ *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* }
{ *#*#*#*#*#*#*#*#*#*#*# The sorting algorithms! #*#*#*#*#*#*#*#*#*#*#*#* }
{ *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* }

PROCEDURE bubblesort;
{ like the head of a beer, reaaal slow and easy-going }
VAR i,j,max: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  max := num;
  REPEAT
    j := 1;
    FOR i := 1 TO max-1 DO
      IF descending(i,i+1) THEN BEGIN
        swapit(i,i+1); j := i;
      END;
    max := j;
  UNTIL (max=1) OR cancel;
  RestoreWin;
END;

PROCEDURE shakersort;
{ interesting variant, but bubblesort still remains hopelessness }
{ (because it only compares and swaps immediate adjacent units)  }
VAR i,j,min,max: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  min := 1;
  max := num;
  REPEAT
    j := min;
    FOR i := min TO max-1 DO
      IF descending(i,i+1) THEN BEGIN
        swapit(i,i+1); j := i;
      END;
    max := j;
    j := max;
    FOR i := max DOWNTO min+1 DO
      IF descending(i-1,i) THEN BEGIN
        swapit(i,i-1); j := i;
      END;
    min := j;
  UNTIL (max=min) OR cancel;
  RestoreWin;
END;

PROCEDURE e_sort;
{ Insert: a pretty human strategy }
VAR i,j: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  FOR i := 2 TO num DO BEGIN
    j := i;
    WHILE j>1 DO
      IF descending(j-1,j) THEN BEGIN
        swapit(j-1,j); Dec(j);
      END ELSE
        j := 1;
    IF cancel THEN begin
        RestoreWin;
        Exit;
    end;
  END;
  RestoreWin;
END;

PROCEDURE a_sort;
{ Pick out: Preparation is one half of a life }
{ Take a look at the ridiculous low percentage of successful comparisions:  }
{ Although there are only n swaps, there are n^2/2 comparisions!            }
{ Both is a record, one in a good sense, the other one in a bad sense.      }

VAR i,j,minpos: Integer;
    min: Real;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  FOR i := 1 TO num-1 DO BEGIN
    minpos := i; min := sort[i];
    FOR j := i+1 TO num DO
      IF descending(minpos,j) THEN
        minpos := j;
    IF minpos<>i THEN swapit(i,minpos);
    IF cancel THEN begin
        RestoreWin;
        Exit;
    end;
  END;
  RestoreWin;
END;

PROCEDURE shellsort;
{ brilliant extension of E-Sort, stunning improvement of efficience }
VAR i,j,gap: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  gap := num DIV 2;
  REPEAT
    FOR i := 1+gap TO num DO BEGIN
      j := i;
      WHILE j>gap DO
        IF descending(j-gap,j) THEN BEGIN
          swapit(j,j-gap); j := j-gap;
        END ELSE
          j := 1;
      IF cancel THEN begin
          RestoreWin;
          Exit;
      end;
    END;
    gap := gap DIV 2;
  UNTIL gap=0;
  RestoreWin;
END;

PROCEDURE seepaway(i,max: Integer);
{ belongs to heapsort }
VAR j: Integer;
BEGIN
  j := 2*i;
  WHILE j<=max DO BEGIN
    IF j<max THEN IF descending(j+1,j) THEN
      Inc(j);
    IF descending(j,i) THEN BEGIN
      swapit(j,i);
      i := j; j := 2*i;
    END ELSE
      j := max+1; { cancels }
  END;
END;

PROCEDURE heapsort;
{ this genius rules over the chaos: it's the best algorithm, I know about    }
{ The only disadvantage compared with shellsort: it's not easy to understand }
{ and impossible to know it by heart. }
VAR i,j: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  i := num DIV 2 + 1;
  j := num;
  WHILE i>1 DO BEGIN
    Dec(i); seepaway(i,j);
  END;
  WHILE j>1 DO BEGIN
    swapit(i,j);
    Dec(j); seepaway(i,j);
  END;
  RestoreWin;
END;

PROCEDURE quicksort;
{ "divide and rule": a classic, but recursive  >>-( }
{ In this demonstration it is faster than heapsort, but does considerable }
{ more unsuccessful comparisions. }
VAR stack: ARRAY[1..100] OF RECORD li,re: Integer; END;
    sp,l,r,m,i,j: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  sp := 1; stack[1].li := 1; stack[1].re := num;
  REPEAT
    l := stack[sp].li; r := stack[sp].re; Dec(sp);
    showstack(sp);
    m := (l+r) DIV 2;
    i := l; j := r;
    REPEAT
      WHILE descending(m,i) DO Inc(i);
      WHILE descending(j,m) DO Dec(j);
      IF j>i THEN swapit(i,j);
      IF m=i THEN m := j ELSE IF m=j THEN m := i; { ahem ... }
      { This "Following" of the reference data is only required because  }
      { I stubborn call the comparision function, and this one only gets }
      { indices on the values which have to be compared. }
    UNTIL i>=j;
    IF i>l THEN BEGIN
      Inc(sp); stack[sp].li := l; stack[sp].re := i; END;
    IF i+1<r THEN BEGIN
      Inc(sp); stack[sp].li := i+1; stack[sp].re := r; END;
  UNTIL (sp=0) OR cancel;
  RestoreWin;
END;

PROCEDURE mergesort;
{ *the* algorithm for lists with pointers on it, for arrays rather }
{ inacceptable. The non.recursive implementation came out pretty more }
{ complicated than the one for quicksort, as quicksort first does }
{ something and then recurses; with mergesort it is the other way round. }
VAR stack: ARRAY[1..100] OF RECORD li,re,mi: Integer; END;
    sp,l,r,i,j,k,m: Integer;
BEGIN
  LockWinSize(w^.Width,w^.Height,w^.Width,w^.Height);
  sp := 1; stack[1].li := 1; stack[1].re := num; stack[1].mi := 0;
  REPEAT
    l := stack[sp].li; r := stack[sp].re; m := stack[sp].mi; Dec(sp);
    showstack(sp);
    IF m>0 THEN BEGIN { put two halfs together }
      { Unfortunately it is only possible in an efficient way by using }
      { extra memory; mergesort really is something for lists with }
      { pointers originally ... }
      FOR i := m DOWNTO l do sort2[i] := sort[i];  i := l;
      FOR j := m+1 TO r DO sort2[r+m+1-j] := sort[j];  j := r;
      FOR k := l TO r DO BEGIN
        clearpixel(k);
        IF sort2[i]<sort2[j] THEN BEGIN
          sort[k] := sort2[i]; Inc(i);
        END ELSE BEGIN
          sort[k] := sort2[j]; Dec(j);
        END;
        setpixel(k);
      END;
    END ELSE IF l<r THEN BEGIN
      { create two halfs and the order to put them together }
      m := (l+r) DIV 2;
      Inc(sp); stack[sp].li := l; stack[sp].mi := m; stack[sp].re := r;
      Inc(sp); stack[sp].li := m+1; stack[sp].mi := 0; stack[sp].re := r;
      Inc(sp); stack[sp].li := l; stack[sp].mi := 0; stack[sp].re := m;
    END;
  UNTIL (sp=0) OR cancel;
  RestoreWin;
END;


Procedure OpenEverything;
begin

    s := LockPubScreen(nil);
    if s = nil then CleanUp('Could not lock pubscreen',10);

    vi := GetVisualInfoA(s, NIL);
    if vi = nil then CleanUp('No visual info',10);

    w := OpenWindowTags(NIL, [
                WA_IDCMP,         IDCMP_CLOSEWINDOW or IDCMP_MENUPICK or
IDCMP_NEWSIZE,
                WA_Left,          0,
                WA_Top,           s^.BarHeight+1,
                WA_Width,         224,
                WA_Height,        s^.Height-(s^.BarHeight-1),
                WA_MinWidth,      MinWinX,
                WA_MinHeight,     MinWinY,
                WA_MaxWidth,      -1,
                WA_MaxHeight,     -1,
                WA_DepthGadget,   ltrue,
                WA_DragBar,       ltrue,
                WA_CloseGadget,   ltrue,
                WA_SizeGadget,    ltrue,
                WA_Activate,      ltrue,
                WA_SizeBRight,    ltrue,
                WA_GimmeZeroZero, ltrue,
                WA_PubScreen,     s,
                TAG_END]);

    IF w=NIL THEN CleanUp('Could not open window',20);

    Rast := w^.RPort;

    { Here we set the barlabel }
    nm[3].nm_Label := PChar(NM_BARLABEL);

    if pExecBase(_ExecBase)^.LibNode.Lib_Version >= 39 then begin
        MenuStrip := CreateMenus(@nm,[
                     GTMN_FrontPen, 1, TAG_END]);
    end else MenuStrip := CreateMenusA(@nm,NIL);

    if MenuStrip = nil then CleanUp('Could not open Menus',10);
    if LayoutMenusA(MenuStrip,vi,NIL)=false then
        CleanUp('Could not layout Menus',10);

    if SetMenuStrip(w, MenuStrip) = false then
        CleanUp('Could not set the Menus',10);

end;

PROCEDURE ProcessIDCMP;
VAR
    IMessage    : tIntuiMessage;
    IPtr    : pIntuiMessage;

    Procedure ProcessMenu;
    var
    MenuNumber  : Integer;
    ItemNumber  : Integer;
    SubItemNumber   : Integer;
    t0,t1,l         : Longint;

    begin
    if IMessage.Code = MENUNULL then
        Exit;

    MenuNumber := MenuNum(IMessage.Code);
    ItemNumber := ItemNum(IMessage.Code);
    SubItemNumber := SubNum(IMessage.Code);

    case MenuNumber of
      0 : begin
          case ItemNumber of
             0 : begin
                   refresh;
                   settitles(0);
                   CurrentTime(t0,l);
                   CASE modus OF
                     0: heapsort;
                     1: shellsort;
                     2: a_sort;
                     3: e_sort;
                     4: shakersort;
                     5: bubblesort;
                     6: quicksort;
                     7: mergesort;
                   END;
                   CurrentTime(t1,l);
                   settitles(t1-t0);
                 end;
             3 : QuitStopDie := True;
          end;
          end;
      1 : begin
          case ItemNumber of
              0..7 : modus := ItemNumber;
          end;
          settitles(-1);
          end;
      2 : begin
          case ItemNumber of
             0 : begin
                 case SubItemNumber of
                    0 : if not rndom then rndom := true;
                    1 : if rndom then rndom := false;
                 end;
                 end;
             1 : begin
                 case SubItemNumber of
                    0 : if not needles then needles := true;
                    1 : if needles then needles := false;
                 end;
                 end;
          end;
          end;
    end;
    end;

begin
    IPtr := pIntuiMessage(Msg);
    IMessage := IPtr^;
    ReplyMsg(Msg);

    case IMessage.IClass of
      IDCMP_MENUPICK    : ProcessMenu;
      IDCMP_NEWSIZE     : refresh;
      IDCMP_CLOSEWINDOW : QuitStopDie := True;
    end;
end;



begin
   OpenEverything;
   QuitStopDie := False;
   modus := 0;
   needles := true;
   rndom := true;
   refresh;
   repeat
   Msg := WaitPort(w^.UserPort);
   Msg := GetMsg(w^.UserPort);
       ProcessIDCMP;
   until QuitStopDie;
   CleanUp('',0);
end.