Created
October 23, 2025 07:15
-
-
Save maycuatroi1/39d0937e65a87841dbf457f1477d5431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class InsertionSort { | |
| private static int[] 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; | |
| } | |
| return arr; | |
| } | |
| public static void main(String[] args) { | |
| int[] arr = {64, 25, 12, 22, 11}; | |
| arr = insertionSort(arr); | |
| System.out.println("Sorted array:"); | |
| for (int num : arr) { | |
| System.out.print(num + " "); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment