fork download
  1. program ideone;
  2.  
  3. Const smax = 99;
  4. Type simple = array[0..smax] of smallint;
  5. Var data, sort:simple; i, n:smallint;
  6.  
  7. procedure writeArr(arr:simple);
  8. Var i:smallint;
  9. begin
  10. for i:=0 to n-1 do write(arr[i], ' ');
  11. writeln();
  12. end;
  13.  
  14. function cocktailShakerSort(data:simple):simple;
  15. Var i, j, t:smallint; isSwap:boolean;
  16. begin
  17. i:=0;
  18. isSwap:=True;
  19. while isSwap=True do begin
  20. isSwap:=False;
  21. for j:=0 to n-2 do
  22. if data[j]>data[j+1] then begin
  23. t:=data[j];
  24. data[j]:=data[j+1];
  25. data[j+1]:=t;
  26. isSwap:=True;
  27. end;
  28. for j:=n-2 downto 0 do
  29. if data[j]>data[j+1] then begin
  30. t:=data[j];
  31. data[j]:=data[j+1];
  32. data[j+1]:=t;
  33. isSwap:=True;
  34. end;
  35.  
  36. i:=i+1;
  37. writeArr(data);
  38. end;
  39. cocktailShakerSort:=data;
  40. end;
  41.  
  42. begin
  43. read(n);
  44. for i:=0 to n-1 do read(data[i]);
  45. cocktailShakerSort(data)
  46. end.
Success #stdin #stdout 0s 5284KB
stdin
10
9 12 18 3 19 7 7 16 8 2
stdout
2 9 12 3 18 7 7 16 8 19 
2 3 9 7 12 7 8 16 18 19 
2 3 7 7 9 8 12 16 18 19 
2 3 7 7 8 9 12 16 18 19 
2 3 7 7 8 9 12 16 18 19