Skip to content

Add LambdaExpressionUtils.java – Unique Lambda-Based Helper Functions #6288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Insertion Sort algorithm in Java with comments
  • Loading branch information
Farheen Shabbir Shaikh committed Jun 9, 2025
commit c0601d005ae5d7e09ec03bf86b4ea34bf9a65903
33 changes: 33 additions & 0 deletions Sorting/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* InsertionSort.java
* This program implements the Insertion Sort algorithm.
* Time Complexity: O(n^2) in worst case, O(n) in best case (already sorted).
*/

public class InsertionSort {

public static void insertionSort(int[] arr) {
// Loop from the second element to the end
for (int i = 1; i < arr.length; i++) {
int key = arr[i]; // Store the current element to be inserted
int j = i - 1;

// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // Shift element to the right
j = j - 1;
}
arr[j + 1] = key; // Insert the key into its correct position
}
}

public static void main(String[] args) {
int[] arr = {29, 10, 14, 37, 13};
insertionSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}