public static void binarySort(int[] array) {
int i, j;
int high, low, mid;
int temp;
for (i = 1; i < array.length; i++) {
temp = array[i];
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (array[mid] > temp) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (j = i - 1; j > high; j--) {
array[j + 1] = array[j];
array[high + 1] = temp;
}
}
}
以上为二分排序
public class BubbleSort {
final static int MAXLENGTH = 10;
/**
* @param args
*/
public static void main(String[] args) {
int[] nums = new int[BubbleSort.MAXLENGTH];
for (int i = 0; i < nums.length; i++) {
nums[i] = (int) (Math.random() * 100 + 1);
}
System.out.println("排序前:...");
for (int i : nums) {
System.out.print(i + "\t");
}
System.out.println();
System.out.println("排序后:...");
for (int m = 0; m < nums.length; m++) {
for (int n = m + 1; n < nums.length; n++) {
if(nums[m]>nums[n]){
int temp;
temp = nums[m];
nums[m]=nums[n];
nums[n]=temp;
}
}
}
for(int i : nums){
System.out.print(i+"\t");
}
}
}
这是冒泡排序