| 2 | import java.util.Arrays; |
| 3 | |
| 4 | public class Main { |
| 5 | //Driver Function |
| 6 | public static void main(String[] args) { |
| 7 | int[] arr = {7, 9, 2, 4, 10}; |
| 8 | insertionSort(arr); |
| 9 | System.out.println(Arrays.toString(arr)); |
| 10 | } |
| 11 | //Insertion Sort Algorithm |
| 12 | static void insertionSort(int[] arr) { |
| 13 | for (int i = 0; i < arr.length - 1; i++) { |
| 14 | for (int j = i+1; j > 0; j--) { |
| 15 | if (arr[j] < arr[j-1]) { |
| 16 | swap(arr, j, j-1); |
| 17 | } |
| 18 | else { |
| 19 | break; |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | //Swapping Algorithm |
| 25 | static void swap(int[] arr, int first, int second) { |
| 26 | int temp = arr[first]; |
| 27 | arr[first] = arr[second]; |
| 28 | arr[second] = temp; |
| 29 | } |
| 30 | } |
nothing calls this directly
no outgoing calls
no test coverage detected