/// <summary>Sorts an entire array in non-decreasing order using the bubble sort algorithm.</summary> /// <typeparam name="T">The type of objects stored within the array.</typeparam> /// <param name="compare">The compare function (returns a positive value if left is greater than right).</param> /// <param name="array">the array to be sorted</param> /// <param name="start">The starting index of the sort.</param> /// <param name="end">The ending index of the sort.</param> /// <remarks>Runtime: Omega(n), average(n^2), O(n^2). Memory: in place. Stability: yes.</remarks> public static void Bubble(Compare <T> compare, T[] array, int start, int end) { Get <T> get = (int index) => { return(array[index]); }; Assign <T> set = (int index, T value) => { array[index] = value; }; Sort <T> .Bubble(compare, get, set, start, end); }
public void Can_Sort_Empty() { var arr = new int[] { }; Sort.Bubble(arr); Assert.AreEqual(arr.Length, 0); }
public void Can_Sort_With_Length_One() { var arr = new int[] { 1 }; Sort.Bubble(arr); Assert.AreEqual(arr.Length, 1); }
public void Can_Sort_1000() { var arr = Test_Utility.Generate_Random_Numbers_In_Int_Array(1000); Sort.Bubble(arr); Assert.IsTrue(Test_Utility.CheckIncreasesInValue(arr)); }
public void Can_Sort_With_Length_Two() { var arr = new int[] { 2, 1 }; var desired = new int[] { 1, 2 }; Sort.Bubble(arr); CollectionAssert.AreEqual(arr, desired); }
public void Can_Sort_With_Length_10() { var arr = new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; var desired = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Sort.Bubble(arr); CollectionAssert.AreEqual(arr, desired); }
public void Can_Sort_With_Length_Complex() { var arr = new int[] { 99999, 10, 44, -1111, 0, 2, 2, 2, -1 }; var desired = new int[] { -1111, -1, 0, 2, 2, 2, 10, 44, 99999 }; Sort.Bubble(arr); CollectionAssert.AreEqual(arr, desired); }
public void Bubble_Comparison() { var actual = new[] { new[] { 1, 2, 3 }, new[] { 2, 3, 4, 5 }, new[] { 1, 4 } }; var expected = new[] { new[] { 2, 3, 4, 5 }, new[] { 1, 2, 3 }, new[] { 1, 4 } }; Sort.Bubble(actual, SomeMethod); CollectionAssert.AreEqual(expected, actual); }
public void Bubble_WhenArray_ResultSorted() { // Arrange var array = new[] { 4, 3, 7, 5, 8, 2 }; var expected = new[] { 2, 3, 4, 5, 7, 8 }; // Act Sort.Bubble(array); // Assert Assert.Equal(expected, array); }
public void BubbleSort_Fail_EmptyArray() { // ARRANGE Sort sortAlgos = new Sort(); int[] testNums = { }; // ACT sortAlgos.Bubble(testNums); // ASSERT // ExpectedException attribute }
static void Main() { Console.WriteLine("Введите количество строк в матрице"); int N = cin.Int(); int[][] matrix = new int[N][]; Console.WriteLine("Введите матрицу построчно"); for (int i = 0; i < N; i++) { matrix[i] = Arrays.ParseInt(Console.ReadLine()); Sort.Bubble(ref matrix[i]); } for (int i = 0; i < N; i++) { Console.WriteLine("Наименьший элемент строки №{0} равен {1}", i + 1, matrix[i][0]); } int min; int max = min = matrix[0][0]; foreach (var u in matrix) { foreach (int x in u) { if (x < min) { min = x; } else if (x > max) { max = x; } } } int Count = 0; double average = 0; foreach (var u in matrix) { foreach (int x in u) { average += x; Count++; } } average -= (min + max); Count -= 2; average /= Count; Console.WriteLine("Среднее значаение элементов матрицы без учета наибольшего и наименьшего равно " + average); Console.ReadKey(); }
public void BubbleSort_Success_Descending() { // ARRANGE Sort sortAlgos = new Sort(SortOrder.Desc); // ACT int[] result = sortAlgos.Bubble(testNumsUnordered); // ASSERT for (int i = 0; i < result.Length; i++) { Assert.AreEqual(testNumsOrderedDesc[i], result[i]); } }
public static void Main(string[] args) { Candy candy = new Candy(2.2, "ya", 7.4); Lollipop lollipop = new Lollipop(2.2, "me", 5.5, 1.1, "good"); Chocolate chocolate = new Chocolate(2.1, "opl", 0.11, "milk", true, "quad"); //Gift gift = new Gift(new Candy[]{new Candy(1.1,"roflyan",2.1),new Lollipop(2,"asd",2.3,4.3,"der"), }); Gift gift = new Gift(); gift.Add(candy); gift.Add(lollipop); gift.Add(chocolate); Sort.Bubble(gift); Console.WriteLine(gift); }
[Benchmark] public void BubbleRunTime() => Sort.Bubble(Values, Compare.Default);
public int[] BubbleSort() { Sort.Bubble(arr); return(arr); }
/// <summary>Sorts an entire array in non-decreasing order using the bubble sort algorithm.</summary> /// <typeparam name="T">The type of objects stored within the array.</typeparam> /// <param name="compare">The compare function (returns a positive value if left is greater than right).</param> /// <param name="array">the array to be sorted</param> /// <remarks>Runtime: Omega(n), average(n^2), O(n^2). Memory: in place. Stability: yes.</remarks> public static void Bubble(Compare <T> compare, T[] array) { Sort <T> .Bubble(compare, array, 0, array.Length); }
/// <summary>Sorts an entire array in non-decreasing order using the bubble sort algorithm.</summary> /// <typeparam name="T">The type of objects stored within the array.</typeparam> /// <param name="array">the array to be sorted</param> /// <remarks>Runtime: Omega(n), average(n^2), O(n^2). Memory: in place. Stability: yes.</remarks> public static void Bubble(T[] array) { Sort <T> .Bubble(Compare.Default, array, 0, array.Length); }
[Benchmark] public void BubbleCompileTime() => Sort.Bubble <int, CompareInt>(Values);
public void Bubble_IComparer(int[][] actual, int[][] expected, IComparer <int[]> comp) { Sort.Bubble(actual, comp); CollectionAssert.AreEqual(expected, actual); }