public int[] Sort(int[] array)
        {
            int[] array1 = new int[array.Length];
            for (int i = 0; i < array1.Length; i++)
            {
                array1[i] = array[i];
            }
            var watch = System.Diagnostics.Stopwatch.StartNew();

            // the code that you want to measure comes here
            for (int i = 0; i < array1.Length - 1; i++)
            {
                int j = i + 1;
                while (j > 0)
                {
                    if (array1[j - 1] > array1[j])
                    {
                        Swaping.swap(ref array1[j - 1], ref array1[j]);
                    }
                    j--;
                }
            }
            watch.Stop();
            this.time = watch.ElapsedTicks;
            return(array1);
        }
Example #2
0
        public int Partition(int[] array, int lowIndex, int highIndex)
        {
            int pivot = array[highIndex];
            int i     = lowIndex - 1;

            for (int j = lowIndex; j < highIndex; j++)
            {
                if (array[j] <= pivot)
                {
                    i++;
                    Swaping.swap(ref array[i], ref array[j]);
                }
            }
            Swaping.swap(ref array[i + 1], ref array[highIndex]);
            return(i + 1);
        }
Example #3
0
        public int[] Sort(int[] array)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            int[] array1 = array;
            for (int i = 0; i < array.Length; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[i] > array[j])
                    {
                        Swaping.swap(ref array[i], ref array[j]);
                    }
                }
            }

            time = watch.ElapsedTicks;
            watch.Stop();
            return(array1);
        }
Example #4
0
        public int[] Sort(int[] arr)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            int[] unsortedArray = new int[arr.Length];
            for (int i = 0; i < unsortedArray.Length; i++)
            {
                unsortedArray[i] = arr[i];
            }
            Sorting(unsortedArray);
            for (int i = unsortedArray.Length - 1; i >= 0; i--)
            {
                Swaping.swap(ref unsortedArray[0], ref unsortedArray[i]);
                heapSize--;
                Heapify(unsortedArray, 0);
            }
            time = watch.ElapsedTicks;
            watch.Stop();
            return(unsortedArray);
        }
Example #5
0
        private void Heapify(int[] arr, int index)
        {
            int left    = 2 * index;
            int right   = 2 * index + 1;
            int largest = index;

            if (left <= heapSize && arr[left] > arr[index])
            {
                largest = left;
            }

            if (right <= heapSize && arr[right] > arr[largest])
            {
                largest = right;
            }

            if (largest != index)
            {
                Swaping.swap(ref arr[index], ref arr[largest]);
                Heapify(arr, largest);
            }
        }