From d298efdbe8870a66983e544d658995c61a60dd24 Mon Sep 17 00:00:00 2001 From: Keira Dandy Date: Tue, 15 Oct 2024 17:31:33 -0400 Subject: [PATCH] Created java insertion sort for ints and strings and a readme file. --- code/languages/Java/README_insertion_sort.md | 10 +++ code/languages/Java/insertion_sort.java | 90 ++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 code/languages/Java/README_insertion_sort.md create mode 100644 code/languages/Java/insertion_sort.java diff --git a/code/languages/Java/README_insertion_sort.md b/code/languages/Java/README_insertion_sort.md new file mode 100644 index 0000000000..3ee2b5d59c --- /dev/null +++ b/code/languages/Java/README_insertion_sort.md @@ -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. diff --git a/code/languages/Java/insertion_sort.java b/code/languages/Java/insertion_sort.java new file mode 100644 index 0000000000..cef2c16c75 --- /dev/null +++ b/code/languages/Java/insertion_sort.java @@ -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(); + } +} \ No newline at end of file