Skip to content

Commit 29bb537

Browse files
committed
Insertion Sort
1 parent 0603fcf commit 29bb537

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@ Algorithms:
2222
- **[Bubble Sort](https://github.com/jszlenk/Algorithms-and-Data-Structures-in-Java/tree/master/SortAlgorithms/src/BubbleSort)**
2323
- **[Selection Sort](https://github.com/jszlenk/Algorithms-and-Data-Structures-in-Java/tree/master/SortAlgorithms/src/SelectionSort)**
2424
- Insertion Sort
25+
- Shell Sort
26+
- Merge Sort
27+
- Quick Sort
28+
- Counting Sort
29+
- Radix Sort
30+
- Sorting Arrays Using the JDK
31+
32+
33+
- Recursion
2534

2635

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package InsertionSort;
2+
3+
class InsertionSort {
4+
5+
static void insertion(int[] intArray) {
6+
for (int firstIndex = 1; firstIndex < intArray.length; firstIndex++) {
7+
int newElement = intArray[firstIndex];
8+
9+
int i;
10+
for (i = firstIndex; i > 0 && intArray[i - 1] > newElement; i--) {
11+
intArray[i] = intArray[i - 1];
12+
}
13+
14+
intArray[i] = newElement;
15+
}
16+
}
17+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package InsertionSort;
2+
3+
import static InsertionSort.InsertionSort.insertion;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
8+
int[] intArray = {25, 0, -8, -15, -4, 22, 7, 7, 1, -22, 14, 1};
9+
10+
insertion(intArray);
11+
12+
for (int anIntArray : intArray) {
13+
System.out.println(anIntArray);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)