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

Java Insertion Sort #6811

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions code/languages/Java/README_insertion_sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Insertion Sort

Insertion sort is an algorithm for sorting arrays/lists that operates by iterating through the array and inserting each element into its place within the sorted part of the array. In the best case (where array is already sorted) its runtime would be linear, or $O(n)$, and in the worst case (where the array is in reverse order) its runtime would be quadratic, or $O(n^2)$, so it is quite nice for sorting smaller arrays, but quickly becomes impractical for larger arrays.

Steps:

1. Begin with the second element of the array.
2. Compare it to the first element, and swap them if the second element is smaller, doing nothing otherwise.
3. Move on to the third element of the array, comparing it to the second element and then the first element, swapping if smaller, or staying in place and moving on to the next step otherwise.
4. Repeat until the end of the array is reached.
90 changes: 90 additions & 0 deletions code/languages/Java/insertion_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Insertion sort for integer and string arrays
class instertion_sort {
// Integer version
public static void insertion_sort_int(int[] arr) {
int i, j, current;

// Loops through array
for (i = 1; i < arr.length; i++) {
j = i - 1;
current = arr[i];

// Loops backward from current element to find its spot
while (j >= 0 && current < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
// Inserts the number in its correct place within the sorted half
arr[j + 1] = current;
}
}

// String version
public static void insertion_sort_str(String[] arr) {
int i, j;
String current;

// Loops through array
for (i = 1; i < arr.length; i++) {
j = i - 1;
current = arr[i];

// Loops backward from current element to find its spot
while (j >= 0 && current.compareToIgnoreCase(arr[j]) < 0) {
arr[j + 1] = arr[j];
j--;
}
// Inserts the string in its correct place within the sorted half
arr[j + 1] = current;
}

}

// Tests insertion_sort_int
public static void int_tester() {
int[] test_arr = {77, 52, -27, 1, 7, 0, 127, 45, 25};

// Prints original array
System.out.println("Original array: ");
for (int i : test_arr) {
System.out.println(i + " ");
}

// Sorting occurs
insertion_sort_int(test_arr);

// Prints results
System.out.println("After sort: ");
for (int i : test_arr) {
System.out.println(i + " ");
}
}

// Tests insertion_sort_string
public static void str_tester() {
String[] test_arr2 = {"hello", "hi", "tomorrow", "zoo", "kitten", "Help", "dog", "climb", "I"};

// Prints original array
System.out.println("Original array: ");
for (String str : test_arr2) {
System.out.println(str + " ");
}

// Sorting occurs
insertion_sort_str(test_arr2);

// Prints resulting array
System.out.println("After sort: ");
for (String str : test_arr2) {
System.out.println(str + " ");
}
}

// Main method calling test functions
public static void main(String[] args) {
int_tester();
System.out.println();
str_tester();
System.out.println();
}
}