插入排序与数组查重
public static void main(String[] args) {
Random random = new Random();
int [] arr = new int[12];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(12)+8;
}
System.out.println(Arrays.toString(arr));
chaSort(arr);
System.out.println(Arrays.toString(arr));
System.out.println("index:"+removeRepeatArr(arr));
System.out.println(Arrays.toString(arr));
}
public static void chaSort(int [] arr){
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0 && arr[j] < arr[j-1] ; j--) {
swap(arr,j,j-1);
}
}
}
public static void swap(int [] arr,int a,int b){
int temp = 0;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static int removeRepeatArr(int[] arr) {
int n = arr.length;
int index = 0;
if(n < 0){
return index;
}
index = 1;
for (int i = 1; i < n; i++) {
if (arr[i] != arr[i-1]) {
arr[index] = arr[i];
index++;
}
}
return index;
}