Skip to content

Instantly share code, notes, and snippets.

@maycuatroi1
Created October 23, 2025 07:15
Show Gist options
  • Select an option

  • Save maycuatroi1/39d0937e65a87841dbf457f1477d5431 to your computer and use it in GitHub Desktop.

Select an option

Save maycuatroi1/39d0937e65a87841dbf457f1477d5431 to your computer and use it in GitHub Desktop.
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