Bubble Sort, bubblesort
8 numbers. Sort as ascend.
1st loop, compare 7 times (for 8 numbers), and found the largest 8.
2nd loop, compare 6 times (for 7 numbers), and found the largest 7.
...
1, 7, 8
2, 6, 7
3, 5, 6
4, 4, 5
5, 3, 4
6, 2, 3
7, 1, 2
In conclusion: For sorting 8 numbers, we need an outer loop of 7 times, each time for finding a largest number; and an inner loop from comparing 7 times to comparing 1 time (as in the center column ).
Implementation in PHP:
1 <?php 2 /* bubble sort: 3 1. operate directly on the input array (&), not on a copy 4 2. sort as ascend 5 6 a is array 7 m is length of a 8 n is times of outer loop, n-i is times of comparing for each outer loop 9 i/j is for-loop counter10 w is for value swap11 */12 function sortBubble(&$a){13 $m = count($a);14 $n = $m - 1;15 for($i=0; $i<$n; $i++){16 for($j=0; $j<$n-$i; $j++){17 if($a[$j] > $a[$j+1]){18 $w = $a[$j];19 $a[$j] = $a[$j+1];20 $a[$j+1] = $w;21 }22 else{23 // do nothing24 }25 }26 // see the results after each outer loop27 // echo implode(', ', $a).'<br />';28 }29 }30 31 $arr = array(9, 5, 2, 7, 3);32 sortBubble($arr);33 echo implode(', ', $arr);34 35 // 2, 3, 5, 7, 937 ?>
Bubble sort c
Detailed notes on Bubble Sorting:
/* Sort the ten numbers of one-dimensional integer arrays in ascending order using the bubble sort method */
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Int I, j, t, a [10];
Printf ("Please input 10 integers: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <9; I ++)/* bubble sort */
For (j = 0; j <10-i-1; j ++)
If (a [j]> a [j + 1])
{T = a [j];/* exchange a [I] And a [j] */
A [j] = a [j + 1];
A [j + 1] = t;
}
Printf ("The sequence after sort is: \ n ");
For (I = 0; I <10; I ++)
Printf ("%-5d", a [I]);
Printf ("\ n ");
System ("pause ");
Return 0;
}
Where I = 0:
J starts from 0. a [0], a [1] compares the size, and gives the larger part to a [1], then j ++, compare a [1] and a [2], and then compare
The greater one gives a [2], so that the Creator in a [0], a [1], a [2] has been exchanged to a [2]. This process continues, until j = 10-i-1 = 9
The value in a [9] is the maximum number of 10 numbers.
Then I = 1:
Since the maximum number has been found and placed in a [9], the maximum number of loop j is 10-i-1 = 8, that is, a [8, compare and exchange a [j] And a [j + 1] From j = 0 again, and put the last large number in a [8 ].
Then I ++, continue...
When I = 9, all sorts have been completed after 9 comparisons, and I <9 is no longer true.
For the number of n, you only need to perform the comparison of n-1 External loops to complete the sorting.
In descending order, you only need to change if (a [j]> a [j + 1]) to if (a [j] <a [j + 1.
-------------------------------------------------------------------
/* Use the improved Bubble sorting method to sort the ten numbers of one-dimensional integer arrays in ascending order */
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{Int I, j, t, a [10], flag;
Printf ("Please input 10 integers: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <9; I ++)/* improved bubble sort */
{Flag = 0;
For (j = 0; j <10-i-1; j ++)
If (a [j]> a [j + 1])
{T = a [j];/* exchange a [I] And a [j] */
A [j] = a [j + 1];
A [j + 1] = t;
Flag = 1;
}
If (flag = 0) break;
}
Printf ("The sequence after sort is: \ n &... The remaining full text>
Bubble sort c
Detailed notes on Bubble Sorting:
/* Sort the ten numbers of one-dimensional integer arrays in ascending order using the bubble sort method */
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Int I, j, t, a [10];
Printf ("Please input 10 integers: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <9; I ++)/* bubble sort */
For (j = 0; j <10-i-1; j ++)
If (a [j]> a [j + 1])
{T = a [j];/* exchange a [I] And a [j] */
A [j] = a [j + 1];
A [j + 1] = t;
}
Printf ("The sequence after sort is: \ n ");
For (I = 0; I <10; I ++)
Printf ("%-5d", a [I]);
Printf ("\ n ");
System ("pause ");
Return 0;
}
Where I = 0:
J starts from 0. a [0], a [1] compares the size, and gives the larger part to a [1], then j ++, compare a [1] and a [2], and then compare
The greater one gives a [2], so that the Creator in a [0], a [1], a [2] has been exchanged to a [2]. This process continues, until j = 10-i-1 = 9
The value in a [9] is the maximum number of 10 numbers.
Then I = 1:
Since the maximum number has been found and placed in a [9], the maximum number of loop j is 10-i-1 = 8, that is, a [8, compare and exchange a [j] And a [j + 1] From j = 0 again, and put the last large number in a [8 ].
Then I ++, continue...
When I = 9, all sorts have been completed after 9 comparisons, and I <9 is no longer true.
For the number of n, you only need to perform the comparison of n-1 External loops to complete the sorting.
In descending order, you only need to change if (a [j]> a [j + 1]) to if (a [j] <a [j + 1.
-------------------------------------------------------------------
/* Use the improved Bubble sorting method to sort the ten numbers of one-dimensional integer arrays in ascending order */
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{Int I, j, t, a [10], flag;
Printf ("Please input 10 integers: \ n ");
For (I = 0; I <10; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <9; I ++)/* improved bubble sort */
{Flag = 0;
For (j = 0; j <10-i-1; j ++)
If (a [j]> a [j + 1])
{T = a [j];/* exchange a [I] And a [j] */
A [j] = a [j + 1];
A [j + 1] = t;
Flag = 1;
}
If (flag = 0) break;
}
Printf ("The sequence after sort is: \ n &... The remaining full text>