Class Program {// <summary> // Bubble Sorting // core idea: a round of sorting. Compare the size of the two numbers in the array, if the number in front is greater than the number in the back, switch the location, and so on until the I-1 wheel is complete, change the unordered number // group to an ordered array /// </summary> /// <param name = "args"> </param> static void Main (string [] args) {// first prepare an unordered array int [] ary = new int [] {5, 3,}; // start sorting. I indicates the number of rounds, A total of ary. length-1 round for (int I = 0; I <ary. length-1; I ++) {// j indicates the number of times in each round and the number of times in each round. for (int j = 0; j <ary. length-1-i; j ++) {// core of the bubble sort, two numbers compared, the front is greater than the back of the number, if (ary [j]> ary [j + 1]) {// defines a temporary variable to store ary [j] int temp = ary [j]; // switch position ary [j] = ary [j + 1]; ary [j + 1] = temp ;}}} // print the elements in the sorted array for (int I = 0; I <ary. length; I ++) {Console. writeLine (ary [I]) ;}}
This article is from the "Lanting drunk beauty" blog and is not reposted!