Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 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 recursiveSum
  8. {
  9. public class Program
  10. {
  11. public static void Main(string[] args)
  12. {
  13. var arr = new[] { 1, 10, 5, 2 };
  14. Console.WriteLine(string.Join(", ", QuickSort(arr)));
  15. Console.ReadLine();
  16. }
  17.  
  18. private static IEnumerable<int> QuickSort(IEnumerable<int> list)
  19. {
  20. if (list.Count() <= 1) return list;
  21. var pivot = list.First();
  22. //Sub-array of all the elements less than the pivot
  23. var less = list.Skip(1).Where(i => i <= pivot);
  24. //Sub-array of all the elements greater than the pivot
  25. var greater = list.Skip(1).Where(i => i > pivot);
  26. // quick sort less + pivot + quick sort greater
  27. return QuickSort(less).Union(new List<int> { pivot }).Union(QuickSort(greater));
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement