Skip to content

Commit

Permalink
Merge pull request #6985 from Raghav-Bajaj/patch-1
Browse files Browse the repository at this point in the history
Create InsertionSorting.java
  • Loading branch information
ossamamehmood authored Oct 31, 2023
2 parents cb5da74 + 7e186b7 commit 00a11a3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Add Code Here/Java/InsertionSorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class InsertionSort {
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void printArray(int[] ar) {
for (int number : ar) {
System.out.print(number + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = {19,20,22,17,99,20,3,69,2};
System.out.println("Original array :");
printArray(arr);
insertionSort(arr);
System.out.println("\nSorted array :");
printArray(arr);
}
}

0 comments on commit 00a11a3

Please sign in to comment.