The classic bubble sorting method has always been one of the sorting methods used by many programs. It is said that the bubble sorting method is more efficient than the PHP system function sort. This chapter does not discuss performance, so we will not compare it with system performance.
Bubble sorting roughly means comparing two adjacent numbers in sequence, and then sorting according to size until the last two digits. Since in the sorting process, small numbers are always placed forward and large numbers are placed backward, which is equivalent to bubbles rising, so it is called bubble sorting. But in fact, in the actual process, you can also use it in reverse according to your own needs. The big trees are placed forward and the decimals are placed backward.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Execute the results through the above code
Original array
int [] array = new int
;
int temp = 0 ;
for (int i = 0 ; i < array.Length - 1 ; i++)
{
for (int j = i + 1 ; j < array.Length ; j++)
{
if (array[j] < array[i])
{
temp = array[i ] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
}
#include
#include
#define M 10
using namespace std;
void maopao1(int data[M])
{
int i,j,t;
for(i=1;i<=M-1;i++)//Outer loop control number of comparisons
for(j=0;j
{t=data[j];data[j]=data[j+1];data[j+1]=t;}
cout<<"After sorting from small to large:"<
void maopao2(int data[M])
{
int i,j,t;
for(i=1 ;i<=M-1;i++)//Outer loop control number of comparisons
for(j=0;j
cout<<"After sorting from small to large:"<
int main()
{
int i,data[M];
cout<<"Please enter "<
maopao1(data);//from small to large
maopao2(data );//From big to small
system("pause");
return 0;
}