/// <summary> /// Sorts array by bubble sorting algorithm in normal order. /// </summary> /// <param name="currentArray">Input array.</param> /// <param name="GetRowValue">Method for computation of the row value.</param> private void BubbleSortNormal(int[,] currentArray, GetValueOfRow GetRowValue) { for (var i = 0; i <= this.yMax; i++) { for (int j = 0; j <= this.yMax - 1; j++) { if (GetRowValue(currentArray, j + 1) < GetRowValue(currentArray, j)) { this.SwapRows(currentArray, j, j + 1); } } } }
/// <summary> /// Performs sorting of the array. /// </summary> /// <param name="array">Input array.</param> /// <param name="sortBy">Sorting type, see <see cref="SortCriteria"/> enum.</param> /// <returns>New sorted array.</returns> public int[,] SortMatrix(int[,] array, SortCriteria sortBy) { int[,] currentArray = (int[, ])array.Clone(); this.xMax = currentArray.GetUpperBound(1); this.yMax = currentArray.GetUpperBound(0); GetValueOfRow GetRowValue = null; BubbleSortDelegate BubbleSort = null; switch (sortBy) { // normal sorting order case SortCriteria.RowSum: GetRowValue = GetRowValueRowSum; BubbleSort = BubbleSortNormal; break; case SortCriteria.RowMax: GetRowValue = GetRowValueRowMax; BubbleSort = BubbleSortNormal; break; case SortCriteria.RowMin: GetRowValue = GetRowValueRowMin; BubbleSort = BubbleSortNormal; break; // reverse sorting order case SortCriteria.RowSumReverse: GetRowValue = GetRowValueRowSumReverse; BubbleSort = BubbleSortReverse; break; case SortCriteria.RowMaxReverse: GetRowValue = GetRowValueRowMaxReverse; BubbleSort = BubbleSortReverse; break; case SortCriteria.RowMinReverse: GetRowValue = GetRowValueRowMinReverse; BubbleSort = BubbleSortReverse; break; } BubbleSort(currentArray, GetRowValue); return(currentArray); }
/// <summary> /// Sorts array by bubble sorting algorithm in reverse order. /// </summary> /// <param name="currentArray">Input array.</param> /// <param name="GetRowValue">Method for computation of the row value.</param> private void BubbleSortReverse(int[,] currentArray, GetValueOfRow GetRowValue) { // This is not a consistent version of the sorting algorith // i.e. the order is not known if the values are equal //for (var i = 0; i <= this.yMax; i++) //{ // for (int j = this.yMax; j >= 1; j--) // { // if (GetValue(currentArray, j - 1, sortBy) < GetValue(currentArray, j, sortBy)) // { // this.SwapRows(currentArray, j, j - 1); // } // } //} // this is a consistent version BubbleSortNormal(currentArray, GetRowValue); this.ReverseArray(currentArray); }