fork(6) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. static List<int> RandomList(int size) {
  7. List<int> ret = new List<int>(size);
  8. Random rand = new Random(1);
  9. for (int i = 0; i < size; i++) {
  10. ret.Add(rand.Next(size));
  11. }
  12. return ret;
  13. }
  14.  
  15. static int MyPartition(List<int> list, int left, int right)
  16. {
  17. int start = left;
  18. int pivot = list[start];
  19. left++;
  20. right--;
  21.  
  22. while(true)
  23. {
  24. while(left <= right && list[left] <= pivot)
  25. left++;
  26.  
  27. while(left <= right && list[right] > pivot)
  28. right--;
  29.  
  30. if(left > right)
  31. {
  32. list[start] = list[left - 1];
  33. list[left - 1] = pivot;
  34.  
  35. return left;
  36. }
  37.  
  38.  
  39. int temp = list[left];
  40. list[left] = list[right];
  41. list[right] = temp;
  42.  
  43. }
  44. }
  45.  
  46. static void MyQuickSort(List<int> list, int left, int right)
  47. {
  48. if(list == null || list.Count <= 1)
  49. return;
  50.  
  51. if(left < right)
  52. {
  53. int pivotIdx = MyPartition(list, left, right);
  54. //Console.WriteLine("MQS " + left + " " + right);
  55. //DumpList(list);
  56. MyQuickSort(list, left, pivotIdx - 1);
  57. MyQuickSort(list, pivotIdx, right);
  58. }
  59. }
  60.  
  61. static void DumpList(List<int> list) {
  62. list.ForEach(delegate(int val)
  63. {
  64. Console.Write(val);
  65. Console.Write(", ");
  66. });
  67. Console.WriteLine();
  68. }
  69.  
  70. public static void Main()
  71. {
  72. List<int> list = RandomList(100);
  73. DumpList(list);
  74. MyQuickSort(list, 0, list.Count);
  75. DumpList(list);
  76. }
  77. }
Success #stdin #stdout 0.03s 33888KB
stdin
Standard input is empty
stdout
36, 20, 95, 25, 90, 36, 75, 60, 83, 35, 85, 54, 64, 76, 67, 98, 65, 86, 65, 51, 10, 83, 79, 50, 35, 95, 30, 98, 92, 33, 58, 16, 25, 0, 38, 36, 67, 6, 49, 97, 97, 7, 13, 50, 36, 8, 20, 96, 57, 4, 50, 80, 12, 6, 75, 20, 95, 95, 87, 53, 68, 68, 10, 86, 38, 78, 40, 14, 40, 58, 78, 69, 28, 60, 1, 29, 70, 73, 74, 15, 0, 35, 11, 38, 64, 90, 5, 39, 61, 60, 95, 53, 65, 90, 19, 28, 78, 53, 49, 6, 
0, 0, 1, 4, 5, 6, 6, 6, 7, 8, 10, 10, 11, 12, 13, 14, 15, 16, 19, 20, 20, 20, 25, 25, 28, 28, 29, 30, 33, 35, 35, 35, 36, 36, 36, 36, 38, 38, 38, 39, 40, 40, 49, 49, 50, 50, 50, 51, 53, 53, 53, 54, 57, 58, 58, 60, 60, 60, 61, 64, 64, 65, 65, 65, 67, 67, 68, 68, 69, 70, 73, 74, 75, 75, 76, 78, 78, 78, 79, 80, 83, 83, 85, 86, 86, 87, 90, 90, 90, 92, 95, 95, 95, 95, 95, 96, 97, 97, 98, 98,