Ejemplo n.º 1
0
 public void One()
 {
     int[] lst = new int[] {5};
     QuickSort<int> qs = new QuickSort<int>();
     qs.quicksort(lst, null);
     Assert.AreEqual(1, lst.Length);
 }
Ejemplo n.º 2
0
 public void Empty()
 {
     int[] lst = new int[0];
     QuickSort<int> qs = new QuickSort<int>();
     qs.quicksort(lst, null);
     Assert.AreEqual(0, lst.Length);
 }
Ejemplo n.º 3
0
 public void Rev()
 {
     int[] lst = new int[] { 2, 1, 3 };
     QuickSort<int>.TCmp cmpFwd = delegate(int a, int b) { return ((a < b) ? 1 : ((a > b) ? -1 : 0)); };
     QuickSort<int> qs = new QuickSort<int>();
     qs.quicksort(lst, cmpFwd);
     Assert.AreEqual(3, lst[0]);
     Assert.AreEqual(2, lst[1]);
     Assert.AreEqual(1, lst[2]);
 }
Ejemplo n.º 4
0
 public void Random()
 {
     int[] lst = new int[100];
     for (int i = 0; i < lst.Length; i++)
         lst[i] = (i + 25) % 100;
     QuickSort<int>.TCmp cmpFwd = delegate(int a, int b) { return ((a < b) ? -1 : ((a > b) ? 1 : 0)); };
     QuickSort<int> qs = new QuickSort<int>();
     qs.quicksort(lst, cmpFwd);
     for (int i = 0; i < lst.Length; i++)
         Assert.AreEqual(i, lst[i]);
 }
Ejemplo n.º 5
0
        public void TestQuickSortMiddleElementAsPivot()
        {
            int expected = 21;

            QuickSort.QuickSort sort  = new QuickSort.QuickSort();
            int[] arrayToBeSorted     = QuickSort.QuickSortHelper.ReadFiles(@"C:\Users\Nilufar\Source\Repos\QuickSort\10.txt");
            int   numberOfComparisons = 0;

            sort.QuickSortAlgorithmWithMedianElementAsPivot(arrayToBeSorted, ref numberOfComparisons, 0, 9);
            Assert.AreEqual(expected, numberOfComparisons);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            int[]     array     = { 72, 12, 6, 33, 81, 97, 37, 59, 52, 1, 20 };
            int       length    = array.Length;
            QuickSort quickSort = new QuickSort(array);

            Console.WriteLine("QuickSort");
            quickSort.PrintInitial(array);
            quickSort.Sort(0, length - 1);
            quickSort.PrintFinal(array);

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public static void Main()
        {
            QuickSort q_Sort = new QuickSort();

            int[] array = { 4, 3, 1, 4, 6, 7, 5, 4, 32, 5, 26, 187, 8 };
            q_Sort.array = array;
            q_Sort.len   = q_Sort.array.Length;
            q_Sort.quickSort();

            for (int j = 0; j < q_Sort.len; j++)
            {
                Console.WriteLine(q_Sort.array[j]);
            }
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Console.WriteLine("Generating numbers...");
            int[]     nums = RandomNumberGenerator.GenerateNumbers(1000000);
            QuickSort qs   = new QuickSort();

            Console.WriteLine("Sorting numbers...");

            Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

            qs.Sort(ref nums, 0, nums.Length - 1);
            watch.Stop();

            Console.WriteLine("Time taken to sort in ms: " + watch.ElapsedMilliseconds);
        }
Ejemplo n.º 9
0
        public static void Main(string[] args)
        {
            var count = 100000;
            var array = Enumerable.Range(1, count).Shuffle().ToArray();

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            QuickSort.Start(array, QuickSortMode.Parallel);

            stopWatch.Stop();
            Console.WriteLine("Check: " + array.IsAscSort());
            Console.WriteLine($"Spended {stopWatch.ElapsedMilliseconds}ms when sorting {count} items");
            // 1087ms for 100000
        }
Ejemplo n.º 10
0
        public static void Main(string[] args)
        {
            int[] input = ReadFromFile();
            //int[] input = new int[] { 3, 8, 4, 7, 6, 5, 2, 1, 9, 0 };

            QuickSort quickSort = new QuickSort();

            quickSort.Sort(ref input, 0, input.Length - 1);

            //for (int i = 0; i < input.Length; i++)
            //{
            //    Console.WriteLine("{0}", input[i]);
            //}

            Console.WriteLine("Number of comparisions = {0}", quickSort.comparisions);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            int[] arr1 = new int[] { 1000, 2000, 1500, 500, 100, 300 };
            int[] arr2 = new int[] { 20, 38, 10, 29, 33, 50, 15 };

            Console.Write("arr1: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr1, item => $"{item} ")));
            QuickSort.Sort(arr1);
            Console.Write("sorted arr1: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr1, item => $"{item} ")));

            Console.Write("arr2: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr2, item => $"{item} ")));
            QuickSort.Sort(arr2);
            Console.Write("sorted arr2: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr2, item => $"{item} ")));
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            var numbers = HelperFunctions.GenerateIntegers(100);

            //var numbers = HelperFunctions.GenerateIntegersOrdered(100);

            Console.WriteLine("unsorted: " + String.Join(",", numbers));
            //QuickSort.Sort(numbers, 0, numbers.Length-1);
            QuickSort.Sort2(numbers, 0, numbers.Length - 1);

            Console.WriteLine();

            Console.WriteLine("sorted: " + String.Join(",", numbers));
            Console.WriteLine("*********************************************************");

#if RELEASE
            BenchmarkRunner.Run <QuickSortBenchmark>();
#endif
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            //int count_inversions;
            string     line;
            List <int> list_int  = new List <int>();
            string     path_file = "C:\\Users\\hmarkley\\Documents\\QuickSort.txt";

            System.IO.StreamReader file = new System.IO.StreamReader(path_file);
            while ((line = file.ReadLine()) != null)
            {
                list_int.Add(Int32.Parse(line));
            }

            int[] array_int = list_int.ToArray();
            file.Close();
            Tuple <long, int[]> sorted_tuple = QuickSort.doQuickSort(array_int);

            Console.ReadLine();
        }
Ejemplo n.º 14
0
        static void Test()
        {
            QuickSort quickSort = new QuickSort();

            int[] numbers = new int[] { 1, 2, 19, -4, -1, 0, 81, -102 };
            Console.WriteLine("Unsorted array: ");
            foreach (var number in numbers)
            {
                Console.Write($"{number}\t");
            }

            var sortNumbers = quickSort.algorithmQuickSort(numbers, 0, numbers.Length - 1);

            Console.WriteLine("\nSorted array: ");
            foreach (var sortNum in sortNumbers)
            {
                Console.Write($"{sortNum}\t");
            }
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            int[]     _10integers   = QuickSortHelper.ReadFiles(@"C:\Users\Nilufar\Source\Repos\QuickSort\10000.txt");
            int[]     _100integers  = QuickSortHelper.ReadFiles(@"C:\Users\Nilufar\Source\Repos\QuickSort\10000.txt");
            int[]     _1000integers = QuickSortHelper.ReadFiles(@"C:\Users\Nilufar\Source\Repos\QuickSort\10000.txt");
            QuickSort sort          = new QuickSort();
            //fix running totals
            int numberOfComparisons1 = 0;
            int numberOfComparisons2 = 0;
            int numberOfComparisons3 = 0;

            sort.QuickSortAlgorithmWithMedianElementAsPivot(_10integers, ref numberOfComparisons3, 0, 9999);
            sort.QuickSortAlgorithmWithFirstElementAsPivot(_1000integers, ref numberOfComparisons1, 0, 9999);
            sort.QuickSortAlgorithmWithLastElementAsPivot(_100integers, ref numberOfComparisons2, 0, 9999);
            Console.WriteLine(numberOfComparisons1);
            Console.WriteLine(numberOfComparisons2);
            Console.WriteLine(numberOfComparisons3);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            int[] list = { 2, 5, 6, 3, 1, 9, 8 };

            foreach (var item in list)
            {
                Console.Write($" {item}");
            }

            QuickSort quicksort = new QuickSort();

            list = quicksort.Sort(list);

            foreach (var item in list)
            {
                Console.Write($" {item}");
            }

            Console.ReadLine();
        }
        public void Sort_PivotInTheMiddle_OrderedArray()
        {
            var numbers = HelperFunctions.GenerateIntegersOrdered(100);

            QuickSort.Sort2(numbers, 0, numbers.Length - 1);
        }
        public void Sort_LastItemAsPivot_RandomizedArray()
        {
            var numbers = HelperFunctions.GenerateIntegers(100);

            QuickSort.Sort(numbers, 0, numbers.Length - 1);
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            int    LEFT   = 0;
            int    RIGHT  = 99;
            string answer = "n";
            bool   display;
            bool   retry = true;

            while (retry)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("Quick Sort. Verloka Vadim, 2017");
                Console.WriteLine(new string('-', 50));
                //input
                int n = 0;
                Console.Write("Size of array, N=");
                int.TryParse(Console.ReadLine(), out n);
                Console.Write("Display array?[y/n] ");
                display = Console.ReadLine() == "y" ? true : false;

                //sorting
                Console.Write("Start sort? [y/n] ");
                answer = Console.ReadLine();
                if (answer == "y")
                {
                    long Avrg = 0;
                    for (int i = 0; i < 10; i++)
                    {
                        //array
                        if (display)
                        {
                            Console.Write($"\nArray - {i + 1}: ");
                        }
                        Random random = new Random((int)DateTime.Now.Ticks);
                        int[]  arr    = new int[n];
                        for (int j = 0; j < n; j++)
                        {
                            arr[j] = random.Next(LEFT, RIGHT);
                            if (display)
                            {
                                Console.Write($"{arr[j]} ");
                            }
                        }
                        if (display)
                        {
                            Console.Write("\n");
                        }

                        //sort
                        Stopwatch stopWatch = new Stopwatch();
                        stopWatch.Start();
                        /*Start*/
                        QuickSort.Sort(ref arr, 0, n - 1);
                        /*STOP*/
                        stopWatch.Stop();
                        long ts = stopWatch.ElapsedMilliseconds;
                        Console.WriteLine($"{i}. Algorithm runtime [Milliseconds] - {ts}");
                        Avrg += ts;

                        if (display)
                        {
                            Console.WriteLine("Sorted array:");
                            foreach (var item in arr)
                            {
                                Console.Write($"{item} ");
                            }
                            Console.Write('\n');
                        }
                    }
                    Console.WriteLine($"Algorithm runtime, 9 iteration - {Avrg}");
                    Console.WriteLine($"Algorithm runtime, Avrg - {Avrg / 9}");

                    Console.Write("Retry?[y/n] ");
                    retry = Console.ReadLine() == "y" ? true : false;
                }
                else
                {
                    return;
                }
            }
        }