算法笔记----插入排序
package com.example.math.wjy;
public class Code03 {
public static int[] gennerateArray(int len, int max) {
int[] arr = new int[len];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * max);
}
return arr;
}
public static void process(int[] arr ){
if (null == arr||arr.length<2)
return;
for (int i = 1 ; i <arr.length ; i++) {
for (int j = i; j > 0 ; j -- ) {
if(arr[j]<arr[j-1]){
int tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
}
}
public static void process1(int[] arr ){
if (null == arr||arr.length<2)
return;
for (int i = 1 ; i <arr.length ; i++) {
for (int j = i; j > 0 && arr[j] < arr[j-1] ; j -- ) {
int tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
}
public static void main(String[] args) {
int[] arr = gennerateArray(10, 100);
process1(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
}
}