/// <summary> /// choses the way to sort with all users parametrs /// </summary> /// <param name="userAnswer">user chosed sorting by the parametr</param> /// <param name="secondUserAnswer">user chosed Asc or Desc sorting</param> /// <returns>the exact way of sorting</returns> public static BubbleSortOrderMethod ChooseBubbleSortOrderMetod(int userAnswer, int secondUserAnswer) { BubbleSortOrderMethod bubbleSortOrderMethod = BubbleSortDescMin; if (userAnswer == 1 && secondUserAnswer == 1) { bubbleSortOrderMethod = BubbleSortAscSum; } if (userAnswer == 1 && secondUserAnswer == 2) { bubbleSortOrderMethod = BubbleSortDescSum; } if (userAnswer == 2 && secondUserAnswer == 1) { bubbleSortOrderMethod = BubbleSortAscMax; } if (userAnswer == 2 && secondUserAnswer == 2) { bubbleSortOrderMethod = BubbleSortDescMax; } if (userAnswer == 3 && secondUserAnswer == 1) { bubbleSortOrderMethod = BubbleSortAscMin; } return(bubbleSortOrderMethod); }
/// <summary> /// bubble sorts the array in the chosen way /// </summary> /// <param name="array">array to sort</param> /// <param name="bubbleSortOrderMethod">if it desc or asc</param> private static void BubbleSortInGeneral(int[,] array, BubbleSortOrderMethod bubbleSortOrderMethod) { int rows = array.GetUpperBound(0) + 1; for (int i = rows - 1; i > 0; i--) { bool flag = false; for (int j = 0; j < i; j++) { if (bubbleSortOrderMethod(array, j)) { int[] temp = new int[rows]; for (int k = 0; k < rows; k++) // remember the first row { temp[k] = array[j, k]; } for (int k = 0; k < rows; k++) // rewrite the first row with the second one { array[j, k] = array[j + 1, k]; } for (int k = 0; k < rows; k++) // writes first(remembered) row to the second { array[j + 1, k] = temp[k]; } flag = true; } } if (flag == false) { return; } } }
public void BubbleSort(int[,] array, BubbleSortOrderMethod bubbleSortOrderMethod) { SortByElement sort = BubbleSortInGeneral; sort?.Invoke(array, bubbleSortOrderMethod); }
public void ExecuteAlgorithm(int[,] array, BubbleSortOrderMethod bubbleSortOrderMethod) { ContextStrategy.BubbleSort(array, bubbleSortOrderMethod); }