Skip to content

Instantly share code, notes, and snippets.

@JiaruiTracy
Created July 12, 2017 14:52
Show Gist options
  • Save JiaruiTracy/245b89b9867b03ecc7ed625e242087e7 to your computer and use it in GitHub Desktop.
Save JiaruiTracy/245b89b9867b03ecc7ed625e242087e7 to your computer and use it in GitHub Desktop.
Insertion sorting
package arr;
public class insertionSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {5,1,2,10,12,7,8,3,9,100};
sort(arr);
}
public static void sort(int[] arr){
int cur=1;
int j;
for(int i=0;i<arr.length;i++){
cur = arr[i];
j=i-1;
while(j>=0&&arr[j]>cur){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=cur;
}
for(int a : arr){
System.out.print(a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment