Advertisement
Ang377ou

sortednumbers

Mar 23rd, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace activity3
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //int[] numbers = { 5, 55,10, 60,40, 100, 25, 30,80, 90 };
  13. int[] numbers = new int[10];
  14. Random r = new Random();
  15. for (int index = 0; index <= numbers.Length - 1; index++)
  16. numbers[index] = r.Next(1, 100);
  17.  
  18. Console.WriteLine("Sorted numbers:");
  19.  
  20. int length =10;
  21. quickSort(numbers, 0, length - 1);
  22.  
  23. for (int i = 0; i < 10; i++)
  24.  
  25. Console.Write(numbers[i] + "\t");
  26. Console.WriteLine();
  27. }
  28.  
  29. static void quickSort(int[] numbers, int left, int right)
  30. {
  31.  
  32. int i = left, j = right;
  33.  
  34. int temp;
  35.  
  36. int pivot = numbers[(left + right) / 2];
  37.  
  38. /* partition */
  39.  
  40. while (i <= j)
  41. {
  42.  
  43. while (numbers[i] < pivot)
  44.  
  45. i++;
  46.  
  47. while (numbers[j] > pivot)
  48.  
  49. j--;
  50.  
  51. if (i <= j)
  52. {
  53.  
  54. temp = numbers[i];
  55.  
  56. numbers[i] = numbers[j];
  57.  
  58. numbers[j] = temp;
  59.  
  60. i++;
  61.  
  62. j--;
  63.  
  64. }
  65.  
  66. };
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. //* recursion */
  74.  
  75. if (left < j)
  76.  
  77. quickSort(numbers, left, j);
  78.  
  79. if (i < right)
  80.  
  81. quickSort(numbers, i, right);
  82.  
  83.  
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement