fork download
  1. program SortingTwoWay;
  2. {
  3. Задача:
  4. - по курсу #1 : Алгоритмы Сортировка
  5. Решение:
  6. - автор Дмитрий Кузнецов
  7.   - цена $108.00
  8. }
  9. const
  10. n = 100;
  11.  
  12. type
  13. item=word;
  14. index=byte;
  15. mas=array[1..n] of item;
  16.  
  17. var
  18. a :mas;
  19. i,j :index;
  20. f :file of mas;
  21.  
  22. procedure binsort(left,right:index);
  23. var
  24. i,j,m,l,r :index;
  25. x :item;
  26. begin
  27. for i:=left+1 to right do
  28. begin
  29. l:=left;
  30. r:=i;
  31. x:=a[i];
  32. while l<r do
  33. begin
  34. m:=(l+r) div 2;
  35. if a[m]<=x then
  36. l:=m+1
  37. else
  38. r:=m
  39. end;
  40. for j:=i downto r+1 do
  41. a[j]:=a[j-1];
  42. a[r]:=x
  43. end
  44.  
  45. end;
  46.  
  47.  
  48. procedure qsort(l,r:index);
  49. var
  50. i,j :index;
  51. w,x :item;
  52. begin
  53. i:=l;
  54. j:=r;
  55. x:=a[(l+r) div 2];
  56. repeat
  57. while a[i]<x do inc(i);
  58. while a[j]>x do dec(j);
  59. if i<=j then
  60. begin
  61. w:=a[i];
  62. a[i]:=a[j];
  63. a[j]:=w;
  64. inc(i);
  65. dec(j)
  66. end;
  67. until i>j;
  68.  
  69. if l<j then
  70. if j-l>10 then
  71. qsort(l,j)
  72. else
  73. binsort(l,j);
  74.  
  75. if i<r then
  76. if r-i>10 then
  77. qsort(i,r)
  78. else
  79. binsort(i,r);
  80.  
  81. end;
  82.  
  83. begin
  84. randomize;
  85. for i:=1 to n do
  86. a[i]:=random(1000);
  87.  
  88. for i:=0 to 9 do
  89. begin
  90. for j:=1 to 10 do
  91. write(a[i*10+j]:5);
  92. writeln
  93. end;
  94.  
  95. {----------------------------------------------}
  96. qsort(1,n);
  97. {----------------------------------------------}
  98.  
  99. writeln;
  100.  
  101. for i:=0 to 9 do
  102. begin
  103. for j:=1 to 10 do
  104. write(a[i*10+j]:5);
  105. writeln
  106. end;
  107. readln
  108. end.
  109.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
  879   30  280  938  817  986  241  787  195  929
   95  794  450  368  723  252  169  202  660  903
   16  710  583  890  357  462  938   63    6  232
   59  532  821  200  597  880  869  367  591  295
  760  479  570  524  702  605  460  842  492  826
  397  943  985   83  901   93  449  817  516  660
  130  601   64   54  945  588  551  320  413  376
  554  766  952  285  424  762  235  155  333  602
  874  694   39  582  139   47  451   89  356    9
  257  580  214  766  674  845  530  285   62  888

    6    9   16   30   39   47   54   59   62   63
   64   83   89   93   95  130  139  155  169  195
  200  202  214  232  235  241  252  257  280  285
  285  295  320  333  356  357  367  368  376  397
  413  424  449  450  451  460  462  479  492  516
  524  530  532  551  554  570  580  582  583  588
  591  597  601  602  605  660  660  674  694  702
  710  723  760  762  766  766  787  794  817  817
  821  826  842  845  869  874  879  880  888  890
  901  903  929  938  938  943  945  952  985  986