Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InsertionSort {
- public static void insertionSort(int arr[]) { for (int j = 1; j < arr.length; j++) { int key = arr[j]; int i = j-1;
- while ( (i > -1) && ( arr[i] > key ) ) { arr[i+1] = arr[i]; i--; }
- arr[i+1] = key;
- }
- }
- static void printArray(int arr[]) { int len = arr.length;
- //simple for loop to print the elements of sorted array for (int i= 0; i<len; i++)
- System.out.print(arr[i] + " " );
- System.out.println();
- }
- public static void main(String args[]){ int[] arr1 = {21,18,15,23,52,12,61};
- //calling the sort function which performs insertion sort insertionSort(arr1);
- //calling the printArray function which performs printing of array printArray(arr1);
- }
- }
Add Comment
Please, Sign In to add comment