冒泡排序:
private static void bubbleSort( int[] array )
{
int temp;
int len = array.length;
for( int i = 0; i < len - 1; i++ )
{
for( int j = 0; j < len - i - 1; j++ )
{
if( array[j] > array[j + 1] )
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
选择排序:
private static void selectSort( int[] array )
{
int len = array.length;
int minIndex;
for( int i = 0; i < len - 1; i++ )
{
minIndex = i;
for( int j = i; j < len; j++ )
{
if( array[minIndex] > array[j] )
{
minIndex = j;
}
}
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
插入排序:
private static void insertSort( int[] array )
{
int len = array.length;
int preIndex;
int current;
for( int i = 1; i < len; i++ )
{
preIndex = i - 1;
current = array[i];
while( preIndex >= 0 && array[preIndex] > current )
{
array[preIndex + 1] = array[preIndex];
preIndex--;
}
array[preIndex + 1] = current;
}
}