zornitza_gencheva

QuickSortAlgorithm

Aug 12th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace QuickSortAlgorithm
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //Task 14: Write a program that sorts an array of strings using
  14. //the quick sort algorithm (find it in Wikipedia).
  15.  
  16. Console.WriteLine("Please enter number of elements: ");
  17. int numElements = int.Parse(Console.ReadLine());
  18.  
  19. string[] elements = new string[numElements];
  20.  
  21. // Input elements
  22. for (int i = 0; i < numElements; i++)
  23. {
  24. Console.WriteLine("Please enter element {0}: ", i);
  25. elements[i] = Console.ReadLine();
  26. }
  27.  
  28. // Quick Sort
  29. bool[] elementIsPivot = new bool[numElements]; // Here we will put markers for pivots; 'true' means we have pivot in the same place of elements[]
  30. while (true) // Condition TBD
  31. {
  32. int startIndex = 0;
  33. int endIndex = numElements - 1;
  34.  
  35. int index = 0;
  36. bool subIntervalFound = false;
  37.  
  38. while (index < numElements) // Until reach the end of elements[]
  39. {
  40. // search for false in pivotMarks[]
  41. while (elementIsPivot[index])
  42. {
  43. index++;
  44. if (index == numElements)
  45. {
  46. break;
  47. }
  48. }
  49.  
  50. // we have found the start of current subinterval
  51. startIndex = index;
  52. if (startIndex == numElements)
  53. {
  54. // We have finished the current pass
  55. break;
  56. }
  57.  
  58. // search for true ahead - to determine the end index of current subinterval
  59. while (!elementIsPivot[index])
  60. {
  61. subIntervalFound = true;
  62. index++;
  63. if (index == numElements)
  64. {
  65. break;
  66. }
  67. }
  68.  
  69. // we have found the end of current subinterval
  70. endIndex = index - 1;
  71.  
  72. // We have found the next subinterval, process it!
  73. int subLength = endIndex - startIndex + 1;
  74. string[] subInterval = new string[subLength];
  75. int pivotIndex = (endIndex + startIndex + 1) / 2;
  76.  
  77. int newSubIndex = 0;
  78.  
  79. // First, copy all elements smaller than pivot in the beginning of subinterval
  80. for (int subIndex = 0; subIndex < subLength; subIndex++)
  81. {
  82. //if (elements[startIndex + subIndex] < elements[pivotIndex])
  83. if (elements[startIndex + subIndex].CompareTo(elements[pivotIndex]) < 0)
  84. {
  85. subInterval[newSubIndex] = elements[startIndex + subIndex];
  86. newSubIndex++;
  87. }
  88. }
  89.  
  90. // Mark the new (permanent) place of the current pivot! (Put Circle!) and copy the pivot itself
  91. subInterval[newSubIndex] = elements[pivotIndex];
  92. elementIsPivot[newSubIndex + startIndex] = true;
  93.  
  94. newSubIndex++;
  95.  
  96. // Then, copy all elements greater than or equal to the pivot, after it
  97. for (int subIndex = 0; subIndex < subLength; subIndex++)
  98. {
  99. //if (elements[startIndex + subIndex] >= elements[pivotIndex] && (startIndex + subIndex != pivotIndex))
  100. if (elements[startIndex + subIndex].CompareTo(elements[pivotIndex]) >= 0 && (startIndex + subIndex != pivotIndex))
  101. {
  102. subInterval[newSubIndex] = elements[startIndex + subIndex];
  103. newSubIndex++;
  104. }
  105. }
  106.  
  107. // Copy the processed elements back to the original elements[] array
  108. for (int subIndex = 0; subIndex < subLength; subIndex++)
  109. {
  110. elements[subIndex + startIndex] = subInterval[subIndex];
  111. }
  112. }
  113.  
  114. if (!subIntervalFound)
  115. {
  116. break;
  117. }
  118. }
  119.  
  120. // Output elements
  121. for (int i = 0; i < numElements; i++)
  122. {
  123. Console.WriteLine("element {0}: {1}", i, elements[i]);
  124. }
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment