예제 #1
0
        static void Main(string[] args)
        {
            int[] arr = { 6, 9, 4, 7, 3, 9, 6, 7, 2, 5, 4, 6, 4, 8, 5, 0 };

            HeapSort sort = new HeapSort();

            sort.Sort(arr);
            Console.WriteLine("sorted");
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            int[] arr = { 16, 4, 10, 14, 7, 9, 3, 2, 8, 1 };
            // int[] arr = { 16, 4, 10, 14, 7, 9, 3, 2, -2, 3, 5, -1, 5, 6, 6, 3, -2, 8, 1 };

            int[]  bigArr  = new int[bigArraySize];
            int[]  bigArr2 = new int[bigArraySize];
            int[]  bigArr3 = new int[bigArraySize];
            Random myRand  = new Random();

            for (int i = 0; i < bigArraySize - 1; i++)
            {
                bigArr[i] = myRand.Next();
            }

            Array.Copy(bigArr, bigArr2, bigArr.Length);
            Array.Copy(bigArr, bigArr3, bigArr.Length);

            File.WriteAllLines("bigArr unsorted.txt", bigArr.Select(pr => pr.ToString()));
            File.WriteAllLines("bigArr2 unsorted.txt", bigArr2.Select(pr => pr.ToString()));
            File.WriteAllLines("bigArr3 unsorted.txt", bigArr3.Select(pr => pr.ToString()));

            Stopwatch stopwatch = new Stopwatch();

            HeapSort hs = new HeapSort();

            stopwatch.Start();
            hs.PerformHeapSort(bigArr);
            stopwatch.Stop();

            Console.WriteLine("heapsort first sort " + stopwatch.ElapsedMilliseconds + " ms ");
            File.WriteAllLines("heapsort bigArr sorted 1.txt", bigArr.Select(pr => pr.ToString()));

            int irRand = myRand.Next();

            bigArr[bigArraySize - 1] = irRand;

            stopwatch.Reset();

            stopwatch.Start();
            hs.PerformHeapSort(bigArr);
            stopwatch.Stop();

            Console.WriteLine("heapsort final sort " + stopwatch.ElapsedMilliseconds + " ms ");
            File.WriteAllLines("heapsort bigArr sorted 2.txt", bigArr.Select(pr => pr.ToString()));

            stopwatch.Reset();

            stopwatch.Start();
            insertionSort(bigArr2);
            stopwatch.Stop();

            Console.WriteLine("insertion sort first sort " + stopwatch.ElapsedMilliseconds + " ms ");

            File.WriteAllLines("insertion first sort.txt", bigArr2.Select(pr => pr.ToString()));

            bigArr2[bigArraySize - 1] = irRand;

            stopwatch.Reset();

            stopwatch.Start();
            insertionSort(bigArr2);
            stopwatch.Stop();

            Console.WriteLine("insertion final sort " + stopwatch.ElapsedMilliseconds + " ms ");
            File.WriteAllLines("final insertion sort.txt", bigArr2.Select(pr => pr.ToString()));

            stopwatch.Reset();

            SortedSet <int> sortedSet = new SortedSet <int>();

            stopwatch.Start();
            foreach (var item in bigArr3)
            {
                sortedSet.Add(item);
            }
            stopwatch.Stop();

            Console.WriteLine("sorted list first sort " + stopwatch.ElapsedMilliseconds + " ms ");
            File.WriteAllLines("sorted list first.txt", sortedSet.Select(pr => pr.ToString()));

            stopwatch.Reset();

            stopwatch.Start();
            sortedSet.Add(irRand);
            stopwatch.Stop();

            Console.WriteLine("sorted list add 1 item " + stopwatch.ElapsedMilliseconds + " ms ");
            File.WriteAllLines("sorted_list_2.txt", sortedSet.Select(pr => pr.ToString()));

            hs.MaxHeapify(arr, 1);
            hs.DisplayArray(arr);
            hs.PerformHeapSort(arr);
            Console.ReadLine();
        }