冒泡排序
public static void BubbleSort1(int [] arr){
int temp;//临时变量
boolean flag;//是否交换的标志
for(int i=0; i<arr.length-1; i++){ //表示趟数,一共arr.length-1次。
flag = false;
for(int j=arr.length-1; j>i; j--){
if(arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
flag = true;
}
}
if(!flag) break;
}
}
选择排序(SelctionSort)
public static void select_sort(int array[],int lenth){
for(int i=0;i<lenth-1;i++){
int minIndex = i;
for(int j=i+1;j<lenth;j++){
if(array[j]<array[minIndex]){
minIndex = j;
}
}
if(minIndex != i){
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
}
插入排序
public static int[] sort2(int[] ins){
for(int i=1; i<ins.length; i++){
int temp = ins[i];//保存每次需要插入的那个数
int j;
for(j=i; j>0&&ins[j-1]>temp; j--){//这个较上面有一定的优化
ins[j] = ins[j-1];//吧大于需要插入的数往后移动。最后不大于temp的数就空出来j
}
ins[j] = temp;//将需要插入的数放入这个位置
}
return ins;
}