Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace quick_sort
  6. {
  7. public static class Extension
  8. {
  9.  
  10. public static IEnumerable<T> QuickSort<T>(this IEnumerable<T> array) where T : IComparable<T>
  11. {
  12. List<T> list = array.ToList();
  13.  
  14. int left = 0;
  15. int right = list.Count - 1;
  16.  
  17. if (right <= 0)
  18. {
  19. return list;
  20. }
  21.  
  22. T pivot = list[right / 2];
  23.  
  24. while (left <= right)
  25. {
  26. while (list[left].CompareTo(pivot) < 0)
  27. {
  28. ++left;
  29. }
  30.  
  31. while (list[right].CompareTo(pivot) > 0)
  32. {
  33. --right;
  34. }
  35.  
  36. if (left >= right)
  37. {
  38. break;
  39. }
  40.  
  41. T swap = list[left];
  42. list[left] = list[right];
  43. list[right] = swap;
  44. }
  45.  
  46. if (left < right)
  47. {
  48. return list.Take(right).QuickSort().Concat(list.Skip(right).QuickSort());
  49. }
  50.  
  51. return list;
  52. }
  53.  
  54. }
  55.  
  56. internal class Program
  57. {
  58. private static void Main(string[] args)
  59. {
  60. Console.WriteLine("Enter array with whitespace as delimeter (i.e. 1 2 3):");
  61.  
  62. IEnumerable<double> input = Console.ReadLine().Split().Select(double.Parse);
  63. IEnumerable<double> output = input.QuickSort();
  64.  
  65. Console.WriteLine(string.Join(" ", output.Select(x => x.ToString())));
  66.  
  67. Console.ReadKey();
  68. }
  69.  
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement