Różne typy sortowań

0

Witam!
Zaczynam dopiero swoją "karierę" z C# i jestem w trakcie robienia projektu mianowicie chodzi o pomiar czasu dla poszczególnych typów sortowań i trochę się pogubiłem z wyświetlaniem wyników. I nie mam pomysłu w jaki sposób zapisać np wyniki do tablicy i później na samym końcu wyświetlić je jakoś ładnie sformatowane jak wszystkie pomiary przejdą. Aktualnie mam to zrobione testowo dla CocktailSort i niestety nie działa tak jak powinno. Do tego algorytm wyświetla mi iteracje np w taki sposób:
50000 CZAS
50000 CZAS
50000 CZAS

A powinien:
50000 CZAS
60000 CZAS
70000 CZAS

Bardzo proszę o pomoc.
Pozdrawiam.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Sortowanie
{
    class Program
    {
       static void InsertionSort(int[] t)
        {
            for (uint i = 1; i < t.Length; i++)
            {
                uint j = i; // elementy 0 .. i-1 są już posortowane
                int Buf = t[j]; // bierzemy i-ty (j-ty) element
                while ((j > 0) && (t[j - 1] > Buf))
                { // przesuwamy elementy
                    t[j] = t[j - 1];
                    j--;
                }
                t[j] = Buf; // i wpisujemy na docelowe miejsce
            }
        } /* InsertionSort() */

        //----------------------------------------------------

        static void SelectionSort(int[] t)
        {
            uint k;
            for (uint i = 0; i < (t.Length - 1); i++)
            {
                int Buf = t[i]; // bierzemy i-ty element
                k = i; // i jego indeks
                for (uint j = i + 1; j < t.Length; j++)
                    if (t[j] < Buf) // szukamy najmniejszego z prawej
                    {
                        k = j;
                        Buf = t[j];
                    }
                t[k] = t[i]; // zamieniamy i-ty z k-tym
                t[i] = Buf;
            }
        } /* SelectionSort() */

        //----------------------------------------------------

        static void HeapSort(int[] t)
        {
            uint left = ((uint)t.Length / 2),
            right = (uint)t.Length - 1;
            while (left > 0) // budujemy kopiec idąc od połowy tablicy
            {
                left--;
                Heapify(t, left, right);
            }
            while (right > 0) // rozbieramy kopiec
            {
                int buf = t[left];
                t[left] = t[right];
                t[right] = buf; // największy element
                right--; // kopiec jest mniejszy
                Heapify(t, left, right); // ale trzeba go naprawić
            }
        } /* HeapSort() */

        static void Heapify(int[] t, uint left, uint right)
        { // procedura budowania/naprawiania kopca
            uint i = left,
            j = 2 * i + 1;
            int buf = t[i]; // ojciec
            while (j <= right) // przesiewamy do dna stogu
            {
                if (j < right) // wybieramy większego syna
                    if (t[j] < t[j + 1]) j++;
                if (buf >= t[j]) break;
                t[i] = t[j];
                i = j;
                j = 2 * i + 1; // przechodzimy do dzieci syna
            }
            t[i] = buf;
        } /* Heapify() */

        //----------------------------------------------------

        static void CocktailSort(int[] t)
        {
            int Left = 1, Right = t.Length - 1, k = t.Length - 1;
            do
            {
                for (int j = Right; j >= Left; j--) // przesiewanie od dołu
                    if (t[j - 1] > t[j])
                    {
                        int Buf = t[j - 1]; t[j - 1] = t[j]; t[j] = Buf;
                        k = j; // zamiana elementów i zapamiętanie indeksu
                    }
                Left = k + 1; // zacieśnienie lewej granicy
                for (int j = Left; j <= Right; j++) // przesiewanie od góry
                    if (t[j - 1] > t[j])
                    {
                        int Buf = t[j - 1]; t[j - 1] = t[j]; t[j] = Buf;
                        k = j; // zamiana elementów i zapamiętanie indeksu
                    }
                Right = k - 1; // zacieśnienie prawej granicy
            }
            while (Left <= Right);
        } /* CocktailSort() */

        //----------------------------------------------------
        
        static void MethodAscending(int[] Array)
        {
                for (int k = 0; k < Array.Length; k++)
                {
                    Array[k] = k + 1;
                }
        }

        //----------------------------------------------------

        static void MethodDecresing(int[] Array)
        {
                for(int i = 0; i < Array.Length; i++)
                {
                    Array[i] = Array.Length-i;
                }
        }

        //----------------------------------------------------

        static void MethodConstant(int[] Array)
        {
                int Value = 1;
                for (int k = 0; k < Array.Length; k++)
                {
                    Array[k] = Value;
                }
        }

        //----------------------------------------------------

        static void VGenerator(int[] Array)
        {
            int j = Array.Length - 1;
            int k = Array.Length - 1;
            int i;
            for (i = 0; i < Array.Length / 2; i++)
            {
                Array[i] = k--;
                Array[j--] = k--;
            }
        }

        static void MethodV(int[] Array)
        {
                VGenerator(Array);
        }

        //----------------------------------------------------

        static void MethodRandom(int[] Array)
        {
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < Array.Length; i++)
            {
                Array[i] = rnd.Next(Array.Length);
            }
        }

        //----------------------------------------------------

        static void CocktailSortTime(int[] Array)
        {
            const int NIter = 10; // liczba powtórzeń testu

                double ElapsedSeconds;
                long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
                for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
                {
                    long StartingTime = Stopwatch.GetTimestamp();
                    CocktailSort(Array);
                    long EndingTime = Stopwatch.GetTimestamp();
                    IterationElapsedTime = EndingTime - StartingTime;
                    ElapsedTime += IterationElapsedTime;
                    //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
                    if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
                    if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
                }
                ElapsedTime -= (MinTime + MaxTime);
                ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
             Console.Write(Array.Length + "\t\t{0}", ElapsedSeconds.ToString("F4"));
        }

        static void Main(string[] args)
        {
            int Size = 50000;
           //Console.WriteLine("n\t\tt[s]");
            for (int i = 0; i < 16; i++, Size += 10000)
            {
                Console.Write("n");
                //Deklaracja Tablic 
                int[] ArrayAscending = new int[Size];
                int[] ArrayDecresing = new int[Size];
                int[] ArrayConstant = new int[Size];
                int[] ArrayV = new int[Size];
                int[] ArrayRandom = new int[Size];
                int[] ArrayRandomCopy = new int[Size];

                //Zapełnianie Tablic CoctailSort
                MethodAscending(ArrayAscending);
                MethodDecresing(ArrayDecresing);
                MethodConstant(ArrayConstant);
                MethodV(ArrayV);
                MethodRandom(ArrayRandom);
                ArrayRandomCopy = (int[])ArrayRandom.Clone(); //Klonowanie tablicy Random (Później działamy na kopii) 

                //Sortowanie Tablic CoctailSort
                CocktailSortTime(ArrayAscending);
                CocktailSortTime(ArrayDecresing);
                CocktailSortTime(ArrayConstant);
                CocktailSortTime(ArrayV);
                CocktailSortTime(ArrayRandomCopy);

                //----------------------------------------------------

                //Zapełnianie Tablic InsertionSort
                MethodAscending(ArrayAscending);
                MethodDecresing(ArrayDecresing);
                MethodConstant(ArrayConstant);
                MethodV(ArrayV);
                ArrayRandomCopy = (int[])ArrayRandom.Clone();

                //Sortowanie Tablic InsertionSort
                InsertionSort(ArrayAscending);
                InsertionSort(ArrayDecresing);
                InsertionSort(ArrayConstant);
                InsertionSort(ArrayV);
                InsertionSort(ArrayRandomCopy);

                //----------------------------------------------------

                //Zapełnianie Tablic HeapSort
                MethodAscending(ArrayAscending);
                MethodDecresing(ArrayDecresing);
                MethodConstant(ArrayConstant);
                MethodV(ArrayV);
                ArrayRandomCopy = (int[])ArrayRandom.Clone();

                //Sortowanie Tablic HeapSort
                HeapSort(ArrayAscending);
                HeapSort(ArrayDecresing);
                HeapSort(ArrayConstant);
                HeapSort(ArrayV);
                HeapSort(ArrayRandomCopy);

                //----------------------------------------------------

                //Zapełnianie Tablic SelectionSort
                MethodAscending(ArrayAscending);
                MethodDecresing(ArrayDecresing);
                MethodConstant(ArrayConstant);
                MethodV(ArrayV);
                ArrayRandomCopy = (int[])ArrayRandom.Clone();

                //Sortowanie Tablic SelectionSort
                SelectionSort(ArrayAscending);
                SelectionSort(ArrayDecresing);
                SelectionSort(ArrayConstant);
                SelectionSort(ArrayV);
                SelectionSort(ArrayRandomCopy);
            }
        }
    }
}
    

2

Nazwy zmiennych w C# pisze się małą literą.

1 użytkowników online, w tym zalogowanych: 0, gości: 1