利用异或运算实现插入排序
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
//实现插入排序
int [] arr =new int[]{5,3,6,78,6,3,7};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int [] arr){
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);
}
}
}
private static void swap(int[] arr, int j, int i) {
arr[j]=arr[j]^arr[i];
arr[i]=arr[j]^arr[i];
arr[j]=arr[j]^arr[i];
}
}