Пример #1
0
 static void Main(string[] args)
 {
     int[] arr = { 64, 25, 12, 22, 11 };
     SelectionSort.Sort(arr);
     Console.WriteLine("Selection sorted array");
     PrintArray(arr);
 }
Пример #2
0
        static void Main(string[] args)
        {
            int arraySize = 10000000;

            RandomArray arr = new RandomArray(arraySize);

            arr.FillArrayRandom();

            if (arraySize < 10001)
            {
                Console.WriteLine("Size of the shuffled array :" + arraySize);
                Console.WriteLine("Bubble Sort Took(miliseconds):" + BubbleSort.Sort(arr.Clone()));
                Console.WriteLine("Selection Sort Took(miliseconds):" + SelectionSort.Sort(arr.Clone()));
                Console.WriteLine("Insertion Sort Took(miliseconds):" + InsertionSort.Sort(arr.Clone()));
            }


            Console.WriteLine("Shell Sort Took(miliseconds):" + ShellSort.Sort(arr.Clone()));
            Console.WriteLine("Merge Sort Took(miliseconds):" + MergeSort.Sort(arr.Clone()));
            if (arraySize < 100000)
            {
                Console.WriteLine("Quick Sort Took(miliseconds):" + QuickSort.Sort(arr.Clone()));
            }
            Console.WriteLine("Counting Sort Took(miliseconds):" + CountingSort.Sort(arr.Clone()));
            Console.WriteLine("Radix Sort Took(miliseconds):" + RadixSort.Sort(arr.Clone()));
        }
Пример #3
0
        static void Main(string[] args)
        {
            List <int> testList = new List <int> {
                2, 8, 0, -2, 9, 12, 5, 1
            };

            Console.WriteLine("Linq:");
            Console.WriteLine(string.Join(", ", testList.OrderBy(x => x)));

            Console.WriteLine("Selection Sort:");
            Console.WriteLine(string.Join(", ", SelectionSort.Sort(testList)));

            Console.WriteLine("Bubble Sort:");
            Console.WriteLine(string.Join(", ", BubbleSort.Sort(testList)));

            Console.WriteLine("Insertion Sort:");
            Console.WriteLine(string.Join(", ", InsertionSort.Sort(testList)));

            Console.WriteLine("Merge Sort:");
            Console.WriteLine(string.Join(", ", MergeSort.Sort(testList)));



            List <string> testList2 = new List <string>()
            {
                "test", "alakazam", "B", "pudding"
            };

            Console.WriteLine("Bubble Sort (GENERIC):");
            Console.WriteLine(string.Join(", ", BubbleSort.GenericSort(testList2)));

            Console.ReadLine();
        }
Пример #4
0
        static void Main(string[] args)
        {
            // -------------------- Bubble Sort --------------------
            var bubbleNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var bubbleSorter  = new BubbleSort();

            bubbleSorter.Sort(bubbleNumbers);
            Console.WriteLine($"[{string.Join(", ", bubbleNumbers)}]");

            // -------------------- Selection Sort --------------------
            var selectionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var selectionSorter  = new SelectionSort();

            selectionSorter.Sort(selectionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", selectionNumbers));

            // -------------------- Insertion Sort --------------------
            var insertionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var insertionSorter  = new InsertionSort();

            insertionSorter.Sort(insertionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", insertionNumbers));

            // -------------------- Merge Sort --------------------
            int[] mergeNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   mergeSorter  = new MergeSort();

            mergeSorter.Sort(mergeNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", mergeNumbers));

            // -------------------- Quick Sort --------------------
            int[] quickNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   quickSorter  = new QuickSort();

            quickSorter.Sort(quickNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", quickNumbers));

            // -------------------- Counting Sort --------------------
            int[] countingNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   countingSorter  = new CountingSort();

            countingSorter.Sort(countingNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", countingNumbers));

            // -------------------- Bucket Sort --------------------
            int[] bucketNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   bucketSorter  = new BucketSort();

            bucketSorter.Sort(bucketNumbers, 3);
            Console.WriteLine("[{0}]", string.Join(", ", bucketNumbers));
        }
Пример #5
0
        static void SelectionTest()
        {
            int[] integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            SelectionSort.Sort(integerValues);
            Console.WriteLine(string.Join(" | ", integerValues));

            float[] floatValues = { -11.2f, 12.56f, -42.59f, 0.0f, 1.1f, 90.9f, 68.68f, 6.1f, -9.8f };
            SelectionSort.Sort(floatValues);
            Console.WriteLine(string.Join(" | ", floatValues));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            SelectionSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
Пример #6
0
        static void Main(string[] args)
        {
            // Pasirinkti, kas kiek dides musu duomenu kiekis
            Console.WriteLine("Select increment size");
            var input = Console.ReadLine();
            int size  = int.Parse(input);

            // Irasyti i output faila headerius
            File.AppendAllText(Directory.GetCurrentDirectory() + "\\" + "test_results.csv", "Count,Selection,Heap\n");


            for (int i = 0; i <= 100000; i += size)
            {
                if (i != 0)
                {
                    // Inicializuoti klase, kuri skaiciuos algoritmu vykdymu laikus
                    var t = new Stopwatch();

                    // Sugeneruoti duomenis
                    // GenerateSortedData(i) - best-case scenario (bus sugeneruoti jau surikiuoti duomenys)
                    // GenerateData(i) - bus sugeneruoti bet kokie int skaiciai
                    int[] data = GenerateSortedData(i);

                    // Inicializuoti SelectionSort klase ir rikiuoti duomenis
                    var s = new SelectionSort();
                    t.Start();
                    int[] selectSort = s.Sort(data);
                    t.Stop();
                    var selectTime = t.ElapsedMilliseconds;

                    //Atstatyti timeri
                    t.Reset();

                    //Inicializuoti HeapSort klase ir rikiuoti duomenis
                    var h = new HeapSort();
                    t.Start();
                    int[] heap = h.Sort(data);
                    t.Stop();
                    var heapTime = t.ElapsedMilliseconds;

                    // Isvesti i konsole, kiek procentu uzduoties atlikta
                    Console.WriteLine("Progress: " + (float)i / 100000 * 100 + "%");

                    // Isvesti rezultutatus i output faila
                    File.AppendAllText(Directory.GetCurrentDirectory() + "\\" + "test_results.csv", i + "," + selectTime + "," + heapTime + "\n");
                }
            }
        }
        public static void Main(string[] args)
        {
            // Bubble sort.
            Console.WriteLine("Bubble sort");
            int[]      arr        = new int[] { 5, 4, 3, 2, 1 };
            BubbleSort bubbleSort = new BubbleSort(arr);

            bubbleSort.Sort();
            Console.WriteLine(bubbleSort.ToString());

            //Insertion sort
            Console.WriteLine("Insertion sort");
            InsertionSort insertionSort = new InsertionSort(new int[] { 5, 4, 3, 2, 1 });

            insertionSort.Sort();
            Console.WriteLine(insertionSort.ToString());


            //Selection sort
            Console.WriteLine("Selection sort");
            SelectionSort selectionSort = new SelectionSort(new int[] { 5, 4, 3, 2, 1 });

            selectionSort.Sort();
            Console.WriteLine(selectionSort.ToString());

            // Quicksort
            Console.WriteLine("Quick sort");
            QuickSort quickSort = new QuickSort(new int[] { 5, 4, 3, 2, 1 });

            quickSort.Sort();
            Console.WriteLine(quickSort.ToString());

            // Merge sort
            Console.WriteLine("Merge sort");
            MergeSortImpl mergeSortImpl = new MergeSortImpl(new int[] { 5, 4, 3, 2, 1 });

            mergeSortImpl.Sort();
            Console.WriteLine(mergeSortImpl.ToString());

            // Heap sort
            Console.WriteLine("Heap sort");
            HeapSortImpl heapSortImpl = new HeapSortImpl(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            heapSortImpl.Sort();
            Console.WriteLine(heapSortImpl.ToString());
            Console.Read();
        }
Пример #8
0
        static void Main(string[] args)
        {
            var rnd     = new Random();
            var arrSize = rnd.Next(1, 1000);

            int[] arr = Enumerable.Repeat(0, arrSize).Select(x => rnd.Next(int.MinValue, int.MaxValue)).ToArray();
            var   bs  = (int[])arr.Clone();

            BubbleSort.Sort(bs);
            Assert.IsTrue(IsSorted(bs));

            var ss = (int[])arr.Clone();

            SelectionSort.Sort(ss);
            Assert.IsTrue(IsSorted(ss));

            var @is = (int[])arr.Clone();

            InsertionSort.Sort(@is);
            Assert.IsTrue(IsSorted(@is));

            var ms = (int[])arr.Clone();

            MergeSort.Sort(ms);
            Assert.IsTrue(IsSorted(ms));

            var qs = (int[])arr.Clone();

            QuickSort.Sort(qs);
            Assert.IsTrue(IsSorted(qs));

            var cs = (int[])arr.Clone();

            CountingSort.Sort(cs);
            Assert.IsTrue(IsSorted(cs));

            var rs = (int[])arr.Clone();

            RadixSort.Sort(rs);
            Assert.IsTrue(IsSorted(rs));

            var hs = (int[])arr.Clone();

            HeapSort.Sort(hs);
            Assert.IsTrue(IsSorted(hs));
        }
        static void Main(string[] args)
        {
            // Arrays to sort
            int[]    integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            string[] stringValues  = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };

            // Apply methods here
            SelectionSort.Sort(integerValues);
            SelectionSort.Sort(stringValues);
            InsertionSort.Sort(integerValues);
            BubbleSort.Sort(integerValues);
            QuickSort.Sort(integerValues);

            // Read output here
            Console.WriteLine(string.Join(" | ", integerValues));
            Console.WriteLine(string.Join(" | ", stringValues));
            Console.ReadLine();
        }