Ejemplo n.º 1
0
        /// <summary>
        /// Sort function, uses the Bubble sort algoritm
        /// </summary>
        /// <param name="arr"> Array with type InitAr. </param>
        public static void Sort(InitArr arr)
        {
            int[] array = new int[arr.Arr().Length];
            Array.Copy(arr.Arr(), array, arr.Arr().Length);
            var watch = Stopwatch.StartNew();

            int size = array.Length;

            for (int i = 0; i < array.Length; i++)
            {
                for (int j = 0; j < size - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        array[j + 1] = array[j + 1] + array[j];
                        array[j]     = array[j + 1] - array[j];
                        array[j + 1] = array[j + 1] - array[j];
                    }
                }
                size--;
            }

            watch.Stop();
            elapsedTime = watch.ElapsedTicks * (1000000.0 / Stopwatch.Frequency);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sort function, uses the heap sort algoritm
        /// </summary>
        /// <param name="arr"> Array with type InitAr. </param>
        /// <returns>Returns sorted array</returns>
        public static int[] Sort(InitArr arr)
        {
            int[] array = new int[arr.Arr().Length];
            Array.Copy(arr.Arr(), array, arr.Arr().Length);
            var watch = Stopwatch.StartNew();

            for (int i = array.Length / 2 - 1; i >= 0; i--)
            {
                Heap(array, array.Length, i);
            }

            int temp;

            for (int i = array.Length - 1; i >= 0; i--)
            {
                temp     = array[0];
                array[0] = array[i];
                array[i] = temp;

                Heap(array, i, 0);
            }

            watch.Stop();
            elapsedTime = watch.ElapsedTicks * (1000000.0 / Stopwatch.Frequency);

            return(array);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sort function via Merge sort.
        /// </summary>
        /// <param name="arr"> Array with type InitAr. </param>
        public static int[] Sort(InitArr arr)
        {
            int[] array = new int[arr.Arr().Length];
            Array.Copy(arr.Arr(), array, arr.Arr().Length);
            var watch = Stopwatch.StartNew();

            mergeSort(array, 0, array.Length - 1);

            watch.Stop();
            elapsedTime = watch.ElapsedTicks * (1000000.0 / Stopwatch.Frequency);

            return(array);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sort function, uses the heap sort algoritm
        /// </summary>
        /// <param name="arr"> Array with type InitAr. </param>
        public static void Sort(InitArr arr)
        {
            int[] array = new int[arr.Arr().Length];
            Array.Copy(arr.Arr(), array, arr.Arr().Length);

            var watch = Stopwatch.StartNew();

            int size = array.Length;

            for (int i = 1; i < array.Length; i++)
            {
                int j = i;
                while (j > 0 && array[j - 1] > array[j])
                {
                    array[j]     = array[j] + array[j - 1];
                    array[j - 1] = array[j] - array[j - 1];
                    array[j]     = array[j] - array[j - 1];
                    j--;
                }
            }
            watch.Stop();
            elapsedTime = watch.ElapsedTicks * (1000000.0 / Stopwatch.Frequency);
        }
Ejemplo n.º 5
0
        ///<sumary>
        /// Genert an array and ask user by which algoritm he/she wants to sort.
        ///</sumary>
        public NewInput()
        {
            Console.WriteLine("Please enter the size of an array");
            this.userInput = Console.ReadLine();
            arr            = new InitArr(Int32.Parse(userInput));
            Console.WriteLine("Select which algorithm you want to perform: \n " +
                              "1. Insertion sort " +
                              "\n 2. Bubble sort " +
                              "\n 3. Quick sort " +
                              "\n 4. Heap sort " +
                              "\n 5. Merge sort" +
                              "\n 6. all");
            do
            {
                this.userInput = Console.ReadLine();

                if (Regex.IsMatch(userInput, "^[1-6]$"))
                {
                    if (Int32.TryParse(this.userInput, out int k))
                    {
                        if (Int32.Parse(this.userInput) == 6)
                        {
                            for (int i = 1; i < 6; i++)
                            {
                                this.sortingNumber.Add(i);
                            }
                        }
                        else
                        {
                            this.sortingNumber.Add(Int32.Parse(this.userInput));
                        }

                        sortingNumberArr = this.sortingNumber.ToArray();
                    }
                    else
                    {
                        Console.WriteLine("error");
                    }
                    break;
                }

                else if (Regex.IsMatch(this.userInput, "^[1-5]-[1-5]$"))
                {
                    Regex    regex      = new Regex("[-]");
                    string[] substrings = regex.Split(this.userInput);
                    foreach (string match in substrings)
                    {
                        if (Int32.TryParse(match, out int k))
                        {
                            //WriteLine(Int32.Parse(match));
                            this.sortingNumber.Add(Int32.Parse(match));
                        }
                        else
                        {
                            Console.WriteLine("error");
                        }
                    }

                    if (sortingNumber[0] >= sortingNumber[1])
                    {
                        sortingNumber[1] = sortingNumber[1] + sortingNumber[0];
                        sortingNumber[0] = sortingNumber[1] - sortingNumber[0];
                        sortingNumber[1] = sortingNumber[1] - sortingNumber[0];
                    }
                    int someValue = sortingNumber[0] + 1;
                    for (int i = 2; i <= sortingNumber[1] - sortingNumber[0]; i++)
                    {
                        this.sortingNumber.Add(someValue);
                        someValue++;
                    }
                    sortingNumberArr = this.sortingNumber.ToArray();
                    break;
                }

                else if (Regex.IsMatch(this.userInput, "^(([1-5][,]){1,100}?)[1-5]$"))
                {
                    Regex    regex      = new Regex("[,]");
                    string[] substrings = regex.Split(userInput);
                    foreach (string match in substrings)
                    {
                        if (Int32.TryParse(match, out int k))
                        {
                            this.sortingNumber.Add(Int32.Parse(match));
                        }
                        else
                        {
                            Console.WriteLine("error");
                        }
                    }
                    sortingNumberArr = this.sortingNumber.ToArray();
                    break;
                }

                else
                {
                    Console.WriteLine("wrong input");
                }
            }while (true);
        }