class Demo
{
public static void main(String[] args)
{
int[] arr = {12, 35, 23, 45, 100, 99, 18, 76};
bubbleSort(arr);
selectionSort(arr);
}
//选择排序
public static void selectionSort(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{
for (int j = i + 1; j < array.length; j++)
{
// 判断, 交换数值.
if (array[i] > array[j])
{
array[i] = array[i] ^ array[j];
array[j] = array[i] ^ array[j];
array[i] = array[i] ^ array[j];
}
}
}
}
//冒牌排序
public static void bubbleSort(int[] array)
{
for (int i = 0; i < 5-1; i++)
{
for (int j = 0; j < 5-1-i; j++)
{
System.out.print(j + "--" + (j+1) + "\t");
}
System.out.println();
}
}
} |