diff --git a/Java/BinaryInsertion.class b/Java/BinaryInsertion.class new file mode 100644 index 0000000..dff8f0d Binary files /dev/null and b/Java/BinaryInsertion.class differ diff --git a/Java/BinaryInsertion.java b/Java/BinaryInsertion.java new file mode 100644 index 0000000..959aa84 --- /dev/null +++ b/Java/BinaryInsertion.java @@ -0,0 +1,113 @@ +import java.util.Scanner; + +/** + * The Binary Insertion class provides a static method for sorting an + * array using an optimized binary insertion sort with half exchanges. + * + * The sorting algorithm is stable. + */ + + +public class BinaryInsertion { + + /** + * Constructs the object. + */ + private BinaryInsertion() { + + /** + * unused constructor. + */ + } + + /** + * this function is used to sort the given array. + * + * @param a this is the array that is to be sorted + */ + public static void sort(Comparable[] a) { + int n = a.length; + for (int i = 1; i < n; i++) { + + // binary search to determine index j at which to insert a[i] + Comparable v = a[i]; + int lo = 0, hi = i; + while (lo < hi) { + int mid = lo + (hi - lo) / 2; + if (less(v, a[mid])) hi = mid; + else lo = mid + 1; + } + + // insetion sort with "half exchanges" + // (insert a[i] at index j and shift a[j], ..., a[i-1] to right) + for (int j = i; j > lo; --j) + a[j] = a[j - 1]; + a[lo] = v; + } + assert isSorted(a); + } + + + + /** + * this function is used to compare between two elemnts. + * + * @param v this is the element 1 to compare. + * @param w this is the element 2 to compare. + * + * @return return true if v is less than w. + */ + private static boolean less(Comparable v, Comparable w) { + return v.compareTo(w) < 0; + } + + /** + * this function is used to check whether the + * array is sorted or not + * + * @param a array that is to be checked. + * + * @return True if sorted, False otherwise. + */ + private static boolean isSorted(Comparable[] a) { + return isSorted(a, 0, a.length - 1); + } + + /** + * Determines if sorted between the lo and high. + * + * @param a this is the array that is to be sorted. + * @param lo The lower + * @param hi The higher + * + * @return True if sorted, False otherwise. + */ + private static boolean isSorted(Comparable[] a, int lo, int hi) { + for (int i = lo + 1; i <= hi; i++) + if (less(a[i], a[i - 1])) return false; + return true; + } + + /** + * this is used to display the array on the console. + * + * @param a array that is to be displayed. + */ + private static void show(Comparable[] a) { + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + " "); + } + } + + /** + * this is to handle the input and output from the console. + * + * @param args The arguments + */ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + String[] a = scan.nextLine().split(" "); + BinaryInsertion.sort(a); + show(a); + } +} \ No newline at end of file diff --git a/Java/InsertionOpt.java b/Java/InsertionOpt.java new file mode 100644 index 0000000..5aa8f09 --- /dev/null +++ b/Java/InsertionOpt.java @@ -0,0 +1,121 @@ +/** + * this is the Optimized insertion sort. + * Sorts a sequence of standard input using an optimized + * version of insertion sort that uses half exchanges instead of + * full exchanges to reduce data movement. + */ +import java.util.Scanner; + +/** + * Class for insertion x. + */ +public class InsertionOpt { + + /** + * Constructs the object. + */ + private InsertionOpt() { + //unused Constructor. + } + /** + * The given array is arranged ihn ascending order. + * Time complexity of this function is O(N) + * + * @param a This is the Array that is to be sorted. + */ + public static void sort(Comparable[] a) { + int n = a.length; + + /** + * the smallest element is choosen so that it acts as the Guard and + * makes sure the index doesnt go beyond. + */ + + int exchanges = 0; + for (int i = n - 1; i > 0; i--) { + if (less(a[i], a[i - 1])) { + swap(a, i, i - 1); + exchanges++; + } + } + if (exchanges == 0) return; + + /** + * the insertion sort is done with half number of exchanges. + */ + for (int i = 2; i < n; i++) { + Comparable v = a[i]; + int j = i; + while (less(v, a[j - 1])) { + a[j] = a[j - 1]; + j--; + } + a[j] = v; + } + + assert isSorted(a); + } + + /** + * this is the helper funciton for the code. + * + * @param v object 1 to be compared. + * @param w object 2 to be compared. + * + * @return returns true if v is smaller to w + * and false if v is greater to w. + */ + private static boolean less(Comparable v, Comparable w) { + return v.compareTo(w) < 0; + } + + /** + * This function is used to swap two elements. + * + * + * @param a Object array + * @param i index of element1. + * @param j index of element2. + */ + private static void swap(Object[] a, int i, int j) { + Object swp = a[i]; + a[i] = a[j]; + a[j] = swp; + } + + + /** + * this function is used to check if the Array is Sorted or not. + * + * + * @param a this is the array that we need to check + * whether the array is sorted or not + * + * @return True if sorted, False otherwise. + */ + private static boolean isSorted(Comparable[] a) { + for (int i = 1; i < a.length; i++) + if (less(a[i], a[i - 1])) return false; + return true; + } + + /** + * this is to print the Array. + * + * @param a this is the array that we wish to print. + */ + private static void show(Comparable[] a) { + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + " "); + } + } + + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + String[] a = scan.nextLine().split(" "); + InsertionOpt.sort(a); + show(a); + } + +}