home *** CD-ROM | disk | FTP | other *** search
- program qsort(input,output);
- const max = 2000; {max array size}
- type standardarray = array[0..max] of integer;
- var numbers: standardarray; {numeric array}
- last: integer;
- procedure swap( var a,b: integer );
- var t: integer;
- begin
- t := a;
- a := b;
- b := t
- end;
- procedure getarray( var top : integer ); {fill array from input}
- var index, maxnum: integer;
- temp: integer;
- begin {getarray}
- index := 0;
- randomize;
- write(' Number of integers: ');
- read(maxnum);
- if maxnum > max then maxnum := max;
- while index <= maxnum do
- begin
- temp := random( index+1 );
- numbers[index] := temp;
- index := succ(index);
- end;
- writeln;
- writeln( index-1:4, ' VALUES ENTERED' );
- top := index - 2
- end; {getarray}
- procedure printarray( top: integer ); {output array}
- var index: integer;
- begin {printarray}
- for index := 0 to top do
- begin
- if index/4 = trunc(index/4) then writeln;
- write( numbers[index]:8 );
- end
- end; {printarray}
- procedure bsort( start, top: integer; var arry: standardarray );
- {bubble sort procedure. sorts array from start to top inclusive}
- var index: integer;
- switched: boolean;
- begin {bsort}
- repeat
- switched := false;
- for index := start to top-1 do
- begin
- if arry[index] > arry[index+1] then
- begin
- swap( arry[index], arry[index+1] );
- switched := true;
- end
- end;
- until switched = false;
- end; {bsort}
- procedure findmedian( start, top: integer; var arry: standardarray );
- {procedure to find a good median value in array and place it}
- var middle: integer;
- sorted: standardarray;
- begin {findmedian}
- middle := (start + top) div 2;
- sorted[1] := arry[start];
- sorted[2] := arry[top];
- sorted[3] := arry[middle];
- bsort( 1, 3, sorted );
- if sorted[2] = arry[middle] then
- swap( arry[start], arry[middle] )
- else if sorted[2] = arry[top] then
- swap( arry[start], arry[top] );
- end; {findmedian}
- procedure sortsection( start, top: integer; var arry: standardarray );
- {procedure to sort a section of the main array, and }
- {then divide it into two partitions to be sorted }
- var swapup: boolean;
- s,e,m: integer;
- begin {sortsection}
- if top - start < 6 then {sort small sections with bsort}
- bsort( start, top, arry )
- else
- begin
- findmedian( start, top, arry );
- swapup := true;
- {start scanning from array top}
- s := start; {lower comparison limit}
- e := top; {upper comparison limit}
- m := start; {location of comparison value}
- while e > s do
- begin
- if swapup = true then
- {scan downward from partition top}
- {and exchange if smaller than median}
- begin
- while( arry[e] >= arry[m] ) and (e > m) do
- e := e - 1;
- if e > m then
- begin
- swap( arry[e], arry[m] );
- m := e;
- end;
- swapup := false;
- end
- else
- {scan upward from a partition start}
- {and exchange if larger than median}
- begin
- while( arry[s] <= arry[m] ) and (s < m) do
- s := s + 1;
- if s < m then
- begin
- swap( arry[s], arry[m] );
- m := s;
- end;
- swapup := true;
- end
- end;
- sortsection( start, m-1, arry ); {sort lower half of partition}
- sortsection( m+1, top, arry ); {sort upper half of partition}
- end
- end; {sortsection}
- begin {qsort - main program}
- getarray(last);
- sortsection( 0, last, numbers );
- printarray(last);
- end. {qsort}
-
-