题目:实现冒泡排序、选择排序、插入排序
public class PaiXu {
public static void main(String[] args){
PaiXu px = new PaiXu();
System.out.println("冒泡排序为:");
px.MaoPao();
System.out.println("选择排序为:");
px.XuanZe();
System.out.println("插入排序为:");
px.ChaRu();
}
public void MaoPao(){
int[] arr = {100,60,80,90,75,38};
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
print(arr);
}
}
public void XuanZe(){
int[] arr = {100,60,80,90,75,38};
for(int i=0;i<arr.length-1;i++){
int s = i;
for(int j=i+1;j<arr.length;j++){
if(arr[s]>arr[j]){
s = j;
}
}
int temp = arr[i];
arr[i] = arr[s];
arr[s] = temp;
print(arr);
}
}
public void ChaRu(){
int[] arr = {100,60,80,90,75,38};
for(int i=1;i<arr.length;i++){
int j = i,num = arr[i];
while(j>0&&arr[j-1]>num){
arr[j] = arr[j-1];
j--;
}
arr[j] = num;
print(arr);
}
}
public void print(int[] arr){
for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" ");
System.out.println();
}
}