MCPcopy Create free account
hub / github.com/apna-college/Alpha / InsertionSort

Class InsertionSort

2_Basic Sorting/InsertionSort.java:5–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3//Problem : Insertion Sort
4
5public class InsertionSort {
6 public static void insertionSort(int arr[]) {
7 for(int i=1; i<arr.length; i++) {
8 int curr = arr[i];
9 int prev = i-1;
10 //to find the index where curr is to be inserted
11 while(prev >= 0 && arr[prev] > curr) {
12 arr[prev+1] = arr[prev];
13 prev--;
14 }
15 arr[prev+1] = curr;
16 }
17 }
18
19 public static void insertionSortDescending(int arr[]) {
20 for(int i=1; i<arr.length; i++) {
21 int curr = arr[i];
22 int prev = i-1;
23 //to find the index where curr is to be inserted
24 while(prev >= 0 && arr[prev] < curr) {
25 arr[prev+1] = arr[prev];
26 prev--;
27 }
28 arr[prev+1] = curr;
29 }
30 }
31
32 public static void printArr(int arr[]) {
33 for(int i=0; i<arr.length; i++) {
34 System.out.print(arr[i]+" ");
35 }
36 System.out.println();
37 }
38
39 public static void main(String args[]) {
40 int arr[] = {5, 4, 1, 3, 2};
41 insertionSortDescending(arr);
42 printArr(arr);
43
44 //Inbuilt Sorting Algo
45 //Arrays.sort(arr);
46 }
47}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…