MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / Main

Class Main

Programs/InsertionSort.java:4–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2import java.util.Arrays;
3
4public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected