示例#1
0
        static void Main(string[] args)
        {
            int[] sortedArray   = new int[] { 1, 2, 3, 4, 5 };
            int[] unsortedArray = new int[] { 14, 7, 3, 12, 9, 11, 6, 2 };
            //SequentialSearch
            Console.WriteLine("Sequential Search");
            Console.WriteLine(Sequential.Search(sortedArray, 5));

            //BinarySearch (Only works on sorted arrays)
            Console.WriteLine(); Console.WriteLine("Binary Search");
            Console.WriteLine(Binary.Search(sortedArray, 5));

            //InsertionSort
            Console.WriteLine(); Console.WriteLine("Insertion Sort");
            Insertion.Sort(unsortedArray);

            foreach (var item in unsortedArray)
            {
                System.Console.WriteLine(item);
            }

            //BubbleSort
            unsortedArray = new int[] { 14, 7, 3, 12, 9, 11, 6, 2 };
            Console.WriteLine(); Console.WriteLine("BubbleSort");
            Bubble.Sort(unsortedArray);
            foreach (var item in unsortedArray)
            {
                System.Console.WriteLine(item);
            }

            //MergeSort
            unsortedArray = new int[] { 14, 7, 3, 12, 9, 11, 6, 2 };
            Console.WriteLine(); Console.WriteLine("MergeSort");
            Merge.Sort(unsortedArray, 0, unsortedArray.Length - 1);
            foreach (var item in unsortedArray)
            {
                System.Console.WriteLine(item);
            }

            //LinkedList-Search
            LinkedList <int> sortedLlist = new LinkedList <int>(sortedArray);

            System.Console.WriteLine(); System.Console.WriteLine("LinkedList Search");
            Console.WriteLine(Llist.Search(sortedLlist, 5));

            //LinkedList-Insert
            System.Console.WriteLine(); System.Console.WriteLine("LinkedList Insert");
            Llist.Insert(sortedLlist, 10);
            foreach (var item in sortedLlist)
            {
                System.Console.WriteLine(item);
            }
        }
示例#2
0
        private static void Sort <T>(T[] array, int lo, int hi) where T : IComparable
        {
            if (hi - lo > CUT_OFF)
            {
                Insertion.Sort(array);                   // cut off for small sub arrays
            }
            if (hi <= lo)
            {
                return;
            }
            int j = Partition(array, lo, hi);

            Sort(array, lo, j - 1);
            Sort(array, j + 1, hi);
        }
示例#3
0
        private static void Sort <T>(T[] array, int lo, int hi) where T : IComparable
        {
            if (hi - lo > CUT_OFF)
            {
                Insertion.Sort(array);                    // cut off for small sub arrays
            }
            if (hi <= lo)
            {
                return;
            }

            // Three way partition

            int i  = lo;
            T   v  = array[lo];
            int gt = hi;
            int lt = lo;

            while (i <= gt)
            {
                int cmp = array[i].CompareTo(v);
                if (cmp == -1)
                {
                    swap(array, i++, lt++);
                }

                else if (cmp == 1)
                {
                    swap(array, i, gt--);
                }

                else
                {
                    ++i;
                }
            }

            Sort(array, lo, lt - 1);
            Sort(array, gt + 1, hi);
        }