Skip to content
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

added Insertion Sort #49

Merged
merged 4 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions Programs/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//program that demonstrate insertion sort algorithm
import java.util.Arrays;

public class Main {
//Driver Function
public static void main(String[] args) {
int[] arr = {7, 9, 2, 4, 10};
insertionSort(arr);
System.out.println(Arrays.toString(arr));
}
//Insertion Sort Algorithm
static void insertionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i+1; j > 0; j--) {
if (arr[j] < arr[j-1]) {
swap(arr, j, j-1);
}
else {
break;
}
}
}
}
//Swapping Algorithm
static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ It is very easy to contribute, you may follow these steps -
55. [DFS](https://github.com/PrajaktaSathe/Java/blob/main/Programs/DFS.java) - For disconnected graph use the approach for checking connected components.
56. [Decimal to Binary](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Decimal_to_Binary.java) - Convert decimal to binary.
57. [Dijkstra](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Dijkstra.java) - Implements Dijkstra algorithm.
58. [Insertion Sort](https://github.com/PrajaktaSathe/Java/blob/main/Programs/InsertionSort.java) - Program that demonstrates insertion sort algorithm

# Contributors

Expand Down