예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("输入数字");
            string numbers = Console.ReadLine();

            string[] number = numbers.Trim().Split(' ');
            int[]    data   = new int[number.Length];
            for (int i = 0; i < number.Length; i++)
            {
                data[i] = Convert.ToInt32(number[i]);
            }
            Console.WriteLine("选择排序法:");
            int[] selectsort = SelectSort.Select(data);
            Write(selectsort);

            Console.WriteLine("插入排序法:");
            int[] isnertsort = InsertSort.Insert(data);
            Write(isnertsort);

            Console.WriteLine("冒泡排序法:");
            int[] bubblesort = BubbleSort.Bubble(data);
            Write(bubblesort);

            Console.WriteLine("希尔排序法:");
            int[] shellsort = ShellSort.Shell(data);
            Write(shellsort);

            Console.WriteLine("堆排序法:");
            int[] halfsort = HeapSort.Heap(data);
            Write(halfsort);

            Console.ReadKey();
        }
예제 #2
0
        public void SortWithInsertSort(int[] a, int lo, int hi)
        {
            if (hi <= lo + m)
            {
                InsertSort IS = new InsertSort();
                IS.Sort(a, lo, hi);
                return;
            }

            int j = Partition(a, lo, hi);

            Sort(a, lo, j - 1);
            Sort(a, j + 1, hi);
        }
예제 #3
0
        static void Main(string[] args)
        {
            int[] arr1 = new int[20];
            Helper.CrearArray(ref arr1);;
            Console.WriteLine("--------before sort---------");
            Helper.ShowArray(arr1);
            BubbleSort bubbleSort = new BubbleSort();

            bubbleSort.BubbleSorts(arr1);
            Console.WriteLine("--------- after bubble sort-----------");
            Helper.ShowArray(arr1);
            SelectionSorts selectionSorts = new SelectionSorts();

            selectionSorts.SelectionSort(arr1);
            Console.WriteLine("--------- after bubble sort-----------");
            Helper.ShowArray(arr1);
            InsertSort insertSort = new InsertSort();

            insertSort.InsertionSort(arr1);
            Console.WriteLine("--------- after insert sort-----------");
            Helper.ShowArray(arr1);
        }
예제 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("/*-----------------------*/");

            Console.WriteLine("Chose one:");
            Console.WriteLine("1  - bubble sort");
            Console.WriteLine("2  - insert sort");
            Console.WriteLine("3  - select sort");
            Console.WriteLine("4  - merge sort");
            Console.WriteLine("5  -  sort");
            var opt = Console.ReadLine();

            switch (opt)
            {
            case "1":
                BubbleSort _bs = new BubbleSort();

                int[] arr1 = ArrayGenerator.array1D();
                int[] arr2 = ArrayGenerator.array1D();

                //bs.bubbleSort_rec(arrrec, arrrec.Length)
                Console.WriteLine(@"BUBBLE SORT");
                Console.WriteLine(@"Sorted array by for loop");
                PrintArray.printArray(arr1);
                PrintArray.printArray(_bs.bubbleSort(arr1));
                Console.WriteLine(@"/*-----------------------*/");
                Console.WriteLine(@"Sorted array by recursive method");
                PrintArray.printArray(arr2);
                PrintArray.printArray(_bs.bubbleSort_rec(arr2, arr2.Length));
                Console.WriteLine(@"/*-----------------------*/");
                break;

            case "2":
                InsertSort _is = new InsertSort();

                Console.WriteLine(@"INSERT SORT");
                int[] arr3 = ArrayGenerator.array1D();
                int[] arr4 = ArrayGenerator.array1D();

                Console.WriteLine(@"Sorted array by for\while loop");
                PrintArray.printArray(arr3);
                PrintArray.printArray(_is.insertSort(arr3));
                Console.WriteLine(@"/*-----------------------*/");
                Console.WriteLine(@"Sorted array by recursive method");
                PrintArray.printArray(arr4);
                PrintArray.printArray(_is.insertSort_rec(arr4, arr4.Length));
                Console.WriteLine(@"/*-----------------------*/");
                break;

            case "3":
                SelectionSort _ss = new SelectionSort();
                Console.WriteLine(@"SELECTION SORT");
                int[] arr5 = ArrayGenerator.array1D();

                Console.WriteLine(@"Sorted array by for loop");
                PrintArray.printArray(arr5);
                PrintArray.printArray(_ss.selectionSort(arr5));
                Console.WriteLine(@"/*-----------------------*/");
                break;

            case "4":
                MergeSort _ms = new MergeSort();
                Console.WriteLine(@"SELECTION SORT");
                int[] arr6 = ArrayGenerator.array1D();

                Console.WriteLine(@"Sorted array by for loop");
                PrintArray.printArray(arr6);
                PrintArray.printArray(_ms.MergeSort_Recursive(arr6, 0, arr6.Length - 1));
                Console.WriteLine(@"/*-----------------------*/");
                break;
            }

            Console.ReadKey();
        }