diff --git a/src/main/java/sortalgorithms/Sort.java b/src/main/java/sortalgorithms/Sort.java index 6cfc0c6..424de44 100644 --- a/src/main/java/sortalgorithms/Sort.java +++ b/src/main/java/sortalgorithms/Sort.java @@ -6,23 +6,23 @@ public class Sort { private int[] inputArray; - public int[] bubbleSort(int[] inputArray){ + public int[] bubbleSort(int[] inputArray) { - int size=inputArray.length-1; - int temp=-1; + int size = inputArray.length - 1; + int temp = -1; - if(size==1) + if (size == 1) return inputArray; - for(int i=0;i<=size;i++){ + for (int i = 0; i <= size; i++) { - for(int j=0;jinputArray[j+1]){ + if (inputArray[j] > inputArray[j + 1]) { - temp=inputArray[j+1]; - inputArray[j+1]=inputArray[j]; - inputArray[j]=temp; + temp = inputArray[j + 1]; + inputArray[j + 1] = inputArray[j]; + inputArray[j] = temp; } @@ -35,6 +35,128 @@ public int[] bubbleSort(int[] inputArray){ } + + public int[] selectionSort(int[] inputArray) { + + int size = inputArray.length - 1; + + if (size == 1) + return inputArray; + + for (int i = 0; i < size; i++) { + int index = i; + for (int j = i + 1; j < size; j++) { + if (inputArray[j] < inputArray[index]) { + index = j;//searching for lowest index + } + } + int smallerNumber = inputArray[index]; + inputArray[index] = inputArray[i]; + inputArray[i] = smallerNumber; + } + return inputArray; + + } + + public int[] insertionSort(int[] inputArray) { + + int size = inputArray.length - 1; + if (size == 1) + return inputArray; + for (int i = 1; i < size; ++i) { + int key = inputArray[i]; + int j = i - 1; + + + while (j >= 0 && inputArray[j] > key) { + inputArray[j + 1] = inputArray[j]; + j = j - 1; + } + inputArray[j + 1] = key; + } + return inputArray; + + } + + + + + // Main function that sorts inputArray[l..r] using + // merge() + public int[] mergeSort(int[] inputArray, int l, int r) { + if (l < r) { + // Find the middle point + int m = (l + r) / 2; + + // Sort first and second halves + mergeSort(inputArray, l, m); + mergeSort(inputArray, m + 1, r); + + // Merge the sorted halves + merge(inputArray, l, m, r); + } + return inputArray; + } + + + /* Java program for Merge Sort */ + // Merges two subarrays of inputArray[]; + // First subarray is inputArray[l..m] + // Second subarray is inputArray[m+1..r] + + public void merge(int[] inputArray, int l, int m, int r) { + // Find sizes of two subarrays to be merged + int n1 = m - l + 1; + int n2 = r - m; + + /* Create temp arrays */ + int L[] = new int[n1]; + int R[] = new int[n2]; + + /*Copy data to temp arrays*/ + for (int i = 0; i < n1; ++i) + L[i] = inputArray[l + i]; + for (int j = 0; j < n2; ++j) + R[j] = inputArray[m + 1 + j]; + + + /* Merge the temp arrays */ + + // Initial indexes of first and second subarrays + int i = 0, j = 0; + + // Initial index of merged subarray array + int k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + inputArray[k] = L[i]; + i++; + } else { + inputArray[k] = R[j]; + j++; + } + k++; + } + + /* Copy remaining elements of L[] if any */ + while (i < n1) { + inputArray[k] = L[i]; + i++; + k++; + } + + /* Copy remaining elements of R[] if any */ + while (j < n2) { + inputArray[k] = R[j]; + j++; + k++; + } + + } + + + + public Sort(int[] inputArray) { this.inputArray = inputArray; } @@ -46,4 +168,6 @@ public int[] getInputArray() { public void setInputArray(int[] inputArray) { this.inputArray = inputArray; } + + } diff --git a/src/main/java/sortalgorithms/Test.java b/src/main/java/sortalgorithms/Test.java index 7607fc7..c0a887b 100644 --- a/src/main/java/sortalgorithms/Test.java +++ b/src/main/java/sortalgorithms/Test.java @@ -11,7 +11,9 @@ public static void main(String[] args) { Sort doSort=new Sort(inputs); System.out.println(Arrays.toString(doSort.bubbleSort(inputs))); - + System.out.println(Arrays.toString(doSort.selectionSort(inputs))); + System.out.println(Arrays.toString(doSort.mergeSort(inputs))); + System.out.println(Arrays.toString(doSort.insertionSort(inputs)));