其他分享
首页 > 其他分享> > 插入排序

插入排序

作者:互联网

public static void main(String[] args) {
int[] arr ={1,2,3,5,4,9,8,6,7};
int[] arr2 ={1,1,3,3,3,3,9,9,9,9,6,6,99,99,6,7,101,101,111,111,144,144,7,7,8,8,1,1,2,2,2,2,4,4,4,4};
insertSortNumber(arr2);
for (int i=0; i<arr2.length ; i++){
System.out.println(arr2[i]);
}

}
//插入排序
public static void insertSortNumber(int[] arr){
if (arr == null || arr.length < 2){
return;
}
for (int i=1; i < arr.length; i++){
for (int j=i-1; j >=0 && arr[j] > arr[j+1]; j--){
swap(arr,j,j+1);
}
}
}
//交换
public static void swap(int[]arr,int i, int j){
arr[i] = arr[i]^arr[j];
arr[j] = arr[i]^arr[j];
arr[i] = arr[i]^arr[j];
}

标签:arr,int,插入排序,public,static,arr2,void
来源: https://www.cnblogs.com/cdlyy/p/16448881.html