Advertisement
Iv555

Untitled

Feb 15th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6.  
  7. namespace QuickSort
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<int> input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)
  14. .ToList();
  15. input = Quicksort(input);
  16. Console.WriteLine(string.Join(" ", input));
  17. }
  18.  
  19. public static List<T> Quicksort<T>(List<T> list) where T : IComparable
  20. {
  21.  
  22. if (list.Count <= 1)
  23. {
  24. return list;
  25. }
  26. T pivot = list[0];
  27. List<T> leftArray = new List<T>();
  28. List<T> righArray = new List<T>();
  29. for (int i = 0; i < list.Count; i++)
  30. {
  31. if (list[i].CompareTo(pivot) < 0)
  32. {
  33. leftArray.Add(list[i]);
  34. }
  35. else if (list[i].CompareTo(pivot) > 0)
  36. {
  37. righArray.Add(list[i]);
  38. }
  39. }
  40.  
  41. leftArray = Quicksort(leftArray);
  42. righArray = Quicksort(righArray);
  43. leftArray.Add(pivot);
  44. leftArray.AddRange(righArray);
  45. return leftArray;
  46. }
  47. }
  48. }
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement