package basic_class_01;
/**
* @Author qgfzzzzzz
* @Date 19-2-28
* @Version 1.0
*
* This Algorithm is insertionsort
*
* time : o(n * n)
* space : o(1)
*/
public class Code_01_InsertionSort {
public static void insertionSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
for(int i = 1; i < arr.length; i++){
for(int j = i - 1; j >= 0; j--){
if(arr[j] > arr[j + 1]){
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];
}
public static void main(String[] args){
int[] arr = {2, 5, 4, 1, 6, 78, 12, 56};
insertionSort(arr);
for (int t:
arr) {
System.out.print(t + " ");
}
}
}