思路好多博客都有,这里之上代码
/*
冒泡
*/
public void Bubbling(int array[]) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tem = array[j];
array[j] = array[j + 1];
array[j + 1] = tem;
}
}
}
}
/*
插入
*/
public void Inset(int array[]){
for (int i = 0; i < array.length-1; i++) {
for (int j = i+1; j>0; j--) {
if(array[j]<array[j-1]){
int tem=array[j];
array[j]=array[j-1];
array[j-1]=tem;
}
}
}
}
/*
选择
*/
public void Select(int array[]){
for (int i = 0; i < array.length-1; i++) {
int min=array[i];
int index=i;
for (int j = i; j <array.length; j++) {
if(array[j]<min){
min=array[j];
index=j;
}
}
int tem=array[i];
array[i]=array[index];
array[index]=tem;
}
for (int i : array) {
System.out.println(i);
}
}
/*
快排
*/
public void FastRow(int array[],int start,int end){
if(start>end){
return;
}
int copyStart=start;
int copyEnd=end;
int value=array[start];
while (copyStart!=copyEnd){
while(copyStart<copyEnd&&array[copyEnd]>=value){
copyEnd--;
}
while(copyStart<copyEnd&&array[copyStart]<=value){
copyStart++;
}
if(copyEnd>copyStart){
int tem=array[copyStart];
array[copyStart]=array[copyEnd];
array[copyEnd]=tem;
}
}
array[start]=array[copyEnd];
array[copyEnd]=value;
FastRow(array,start,copyStart-1);
FastRow(array,copyEnd+1,end);
}
``