In place sort for IList?

前端 未结 3 757
有刺的猬
有刺的猬 2020-12-20 01:07

Does .NET have an in place sort function for IList? I need to sort the collection itself, not create a new collection.

EDIT: The reason I need to do the sort in plac

3条回答
  •  猫巷女王i
    2020-12-20 01:48

    How about an in-place Quicksort / InsertionSort / ShellSort implementation:

    public static class InPlaceQuickSort {
        private static void Swap(IList set, int left, int right) {
            T temp = set[left];
            set[left] = set[right];
            set[right] = temp;
        }
    
        private static Int32 Partition(IList set, int lBound, int rBound)
            where T : IComparable {
            T pivot = set[rBound];
            int left = lBound - 1;
            int right = rBound;
    
            while (true) {
    
                while (set[++left].CompareTo(pivot) < 0) ;
                while (set[--right].CompareTo(pivot) > 0) if (left == right) break;
    
                if (left >= right) break;
                Swap(set, left, right);
            }
    
            Swap(set, left, rBound);
            return left;
        }
    
        private static IList QuickSort(IList set, int lBound, int rBound)
            where T : IComparable {
            if (lBound >= rBound) return set;
    
            Int32 pivot = Partition(set, lBound, rBound);
            QuickSort(set, lBound, pivot - 1);
            QuickSort(set, pivot + 1, rBound);
    
            return set;
        }
    
        public static IList InsertionSort(this IList set)
            where T : IComparable {
            for (Int32 index = 1; index < set.Count; index++) {
                for (Int32 insertion = index; insertion > 0 && set[insertion - 1].CompareTo(set[insertion]) > 0; insertion--) {
                    Swap(set, insertion - 1, insertion);
                }
            }
    
            return set;
        }
    
        public static IList ShellSort(this IList set)
            where T : IComparable {
            Int32 shell = 1;
    
            while (shell < (set.Count / 3)) shell = shell * 3 + 1;
    
            while (shell >= 1) {
                for (Int32 index = shell; index < set.Count; index++) {
                    for (Int32 insertion = index; insertion >= shell && set[insertion - shell].CompareTo(set[insertion]) > 0; insertion -= shell) {
                        Swap(set, insertion - shell, insertion);
                    }
                }
    
                shell = shell / 3;
            }
    
            return set;
        }
    
        public static IList QuickSort(this IList set)
            where T : IComparable {
            return QuickSort(set, 0, set.Count - 1);
        }
    }
    

    And, here's how you use it:

    public static void Main() {
        List numbers = new List { 1, 3, 2, 4, 2 };
    
        foreach (Int32 number in numbers.QuickSort())
            Console.WriteLine(number);
    
        Console.ReadLine();
    }
    

提交回复
热议问题