C # Bubble sort
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace sort {Class Bubblesorter {public static int[] Sort (int[] a) {Bubble Sort (a); return A; } private static void Bubblesort (int[] myArray) {for (int i = 0; i < myarray.length; i+ +)//number of cycles {for (int j = 0; J < myarray.length-1-I; j + +)//number of cycles per cycle { Compare adjacent elements, move the value to the back = = "The last number is the largest of each cycle." So each cycle is 1 less than the number of previous cycles, i.e. J < myarray.length-1-I if (Myarray[j] > myarray[j + 1]) {Swap (ref myarray[j], ref MYARRAY[J + 1]); }}}}//reference parameter with value parameter private static void Swap (ref int left, ref int rig HT) {int temp; temp = left; left = right; Right = temp; }}} The bubbling sort algorithm works as follows:
The bubbling sorting algorithm works as follows:
1, compare the adjacent elements. If the first one is bigger than the second one, swap them both.
2, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number. = = "The last number does not need to be compared, because it is the largest."
3. Repeat the above steps for all elements except the last one.
4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.
Above is the content of C # bubble sort, more related content please pay attention to topic.alibabacloud.com (www.php.cn)!