Created
July 12, 2017 14:52
-
-
Save JiaruiTracy/245b89b9867b03ecc7ed625e242087e7 to your computer and use it in GitHub Desktop.
Insertion sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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