Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace recursiveSum
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- var arr = new[] { 1, 10, 5, 2 };
- Console.WriteLine(string.Join(", ", QuickSort(arr)));
- Console.ReadLine();
- }
- private static IEnumerable<int> QuickSort(IEnumerable<int> list)
- {
- if (list.Count() <= 1) return list;
- var pivot = list.First();
- //Sub-array of all the elements less than the pivot
- var less = list.Skip(1).Where(i => i <= pivot);
- //Sub-array of all the elements greater than the pivot
- var greater = list.Skip(1).Where(i => i > pivot);
- // quick sort less + pivot + quick sort greater
- return QuickSort(less).Union(new List<int> { pivot }).Union(QuickSort(greater));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement