Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 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. public static IEnumerable<T> QuickSort<T>(this IEnumerable<T> array) where T : IComparable<T>
  10. {
  11. List<T> list = array.ToList();
  12. int left = 0;
  13. int right = list.Count - 1;
  14.  
  15. if (right <= 0) return list;
  16.  
  17. T pivot = list[right / 2];
  18.  
  19. while (left <= right)
  20. {
  21. while (list[left].CompareTo(pivot) < 0) ++left;
  22. while (list[right].CompareTo(pivot) > 0) --right;
  23. if (left >= right) break;
  24.  
  25. T swap = list[left];
  26. list[left] = list[right];
  27. list[right] = swap;
  28. }
  29.  
  30. return left >= right ? list : list.Take(right).QuickSort().Concat(list.Skip(right).QuickSort());
  31. }
  32. }
  33.  
  34. internal class Program
  35. {
  36. private static void Main(string[] args)
  37. {
  38. Console.WriteLine("Enter array with whitespace as delimeter (i.e. 1 2 3):");
  39.  
  40. var input = Console.ReadLine().Split().Select(double.Parse);
  41. var output = input.QuickSort();
  42.  
  43. Console.WriteLine(string.Join(" ", output.Select(x => x.ToString())));
  44.  
  45. Console.ReadKey();
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement