1.从小到大排序
1.1从左到右冒泡
void Sort1 ( int * a, int n)
{
for ( int i = 0 ; i < n - 1 ; ++ i)
{
for ( int j = 0 ; j < n - i - 1 ; ++ j)
{
if ( a[ j] > a[ j + 1 ] )
{
int temp = a[ j] ;
a[ j] = a[ j + 1 ] ;
a[ j + 1 ] = temp;
}
}
}
}
1.2从右到左冒泡
void Sort2 ( int * a, int n)
{
for ( int i = 0 ; i < n - 1 ; ++ i)
{
for ( int j = n- 1 ; j > i; -- j)
{
if ( a[ j] < a[ j - 1 ] )
{
int temp = a[ j] ;
a[ j] = a[ j - 1 ] ;
a[ j - 1 ] = temp;
}
}
}
}
2.从大到小排序
2.1从左到右冒泡
void Sort3 ( int * a, int n)
{
for ( int i = 0 ; i < n - 1 ; ++ i)
{
for ( int j = 0 ; j < n - i - 1 ; ++ j)
{
if ( a[ j] < a[ j + 1 ] )
{
int temp = a[ j] ;
a[ j] = a[ j + 1 ] ;
a[ j + 1 ] = temp;
}
}
}
}
2.2从右到左冒泡
void Sort4 ( int * a, int n)
{
for ( int i = 0 ; i < n - 1 ; ++ i)
{
for ( int j = n - 1 ; j > i; -- j)
{
if ( a[ j] > a[ j - 1 ] )
{
int temp = a[ j] ;
a[ j] = a[ j - 1 ] ;
a[ j - 1 ] = temp;
}
}
}
}