Example #1
0
        public static void Sort(int[] array)
        {
            if (array.Length == 0)
            {
                return;
            }

            var lastIndex  = array.Length;
            var swapExists = false;

            while (lastIndex != 1)
            {
                for (var j = 0; j < lastIndex - 1; ++j)
                {
                    if (array[j] > array[j + 1])
                    {
                        SortHelpers.Swap(array, j + 1, j);
                        swapExists = true;
                    }
                }

                if (!swapExists)
                {
                    break;
                }
                --lastIndex;
            }
        }
Example #2
0
        /// <summary>
        /// Performs a down-heap or sink-down operation for a max-heap.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="heapSize">The heap size.</param>
        /// <param name="i">The index to start at when sinking down.</param>
        /// <param name="progressCallback">The progress callback.</param>
        private void Sink(int[] values, int heapSize, int i, Action <SortProgress> progressCallback)
        {
            int largest = i;

            int left  = (2 * i) + 1;
            int right = (2 * i) + 2;

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

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

            if (largest != i)
            {
                progressCallback?.Invoke(new SortProgress(new[] { largest }, values));
                SortHelpers.Swap(values, i, largest);
                progressCallback?.Invoke(new SortProgress(new[] { i }, values));

                this.Sink(values, heapSize, largest, progressCallback);
            }
        }
 /// <summary>
 /// Bubble sorts input values and optionally report the progress.
 /// </summary>
 /// <param name="values">The values to sort.</param>
 /// <param name="progressCallback">The optional progress callback.</param>
 public void Sort(int[] values, Action <SortProgress> progressCallback = null)
 {
     for (var write = 0; write < values.Length; write++)
     {
         for (var sort = 0; sort < values.Length - 1; sort++)
         {
             if (values[sort] > values[sort + 1])
             {
                 SortHelpers.Swap(values, sort + 1, sort);
                 progressCallback?.Invoke(new SortProgress(new[] { sort + 1 }, values));
             }
         }
     }
 }
Example #4
0
        /// <summary>
        /// Heap sorts input values and optionally report the progress.
        /// </summary>
        /// <param name="values">The values to sort.</param>
        /// <param name="progressCallback">The optional progress callback.</param>
        public void Sort(int[] values, Action <SortProgress> progressCallback = null)
        {
            var heapSize = values.Length;

            this.BuildHeap(values, progressCallback);

            for (var i = heapSize - 1; i >= 1; i--)
            {
                progressCallback?.Invoke(new SortProgress(new[] { 0 }, values));
                SortHelpers.Swap(values, i, 0);
                progressCallback?.Invoke(new SortProgress(new[] { i }, values));

                heapSize--;
                this.Sink(values, heapSize, 0, progressCallback);
            }
        }
Example #5
0
        private static int Partition(int[] arr, int start, int end)
        {
            var pivot          = arr[end];
            var smallerElIndex = start - 1;

            for (var i = 0; i < arr.Length; ++i)
            {
                if (arr[i] < pivot)
                {
                    SortHelpers.Swap(arr, i, smallerElIndex);
                    ++smallerElIndex;
                }
            }

            ++smallerElIndex;
            SortHelpers.Swap(arr, smallerElIndex, end);

            return(smallerElIndex);
        }
        /// <summary>
        /// Takes last element as pivot, places the pivot element at its correct position in sorted array,
        /// and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="start">The start index.</param>
        /// <param name="end">The end index.</param>
        /// <param name="progressCallback">The progress callback.</param>
        /// <returns>A partitioning index.</returns>
        private int Partition(int[] values, int start, int end, Action <SortProgress> progressCallback)
        {
            var pivot = values[end];
            var i     = start - 1;

            for (var j = start; j <= end - 1; j++)
            {
                if (values[j] <= pivot)
                {
                    i++;
                    progressCallback?.Invoke(new SortProgress(new[] { j, start, end }, values));
                    SortHelpers.Swap(values, i, j);
                    progressCallback?.Invoke(new SortProgress(new[] { i, start, end }, values));
                }
            }

            progressCallback?.Invoke(new SortProgress(new[] { start, end }, values));
            SortHelpers.Swap(values, i + 1, end);
            progressCallback?.Invoke(new SortProgress(new[] { i + 1, start, end }, values));
            return(i + 1);
        }
Example #7
0
        public static void Sort(int[] array)
        {
            if (array.Length == 0)
            {
                return;
            }

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

                if (i != minIndex)
                {
                    SortHelpers.Swap(array, i, minIndex);
                }
            }
        }