diff --git a/java/bubble_sort.java b/java/bubble_sort.java new file mode 100644 index 0000000..8eb703f --- /dev/null +++ b/java/bubble_sort.java @@ -0,0 +1,26 @@ +/** + * Created by renu.yadav on 20/10/17. + */ +public class bubble_sort { + /** + * bubble sort , compares adjacent values and swap them if needed, + * after every ith iteration ,ith biggest element would be in place + * takes O(n^2) and an inplace algorithm with no extra space. + * @param arr + * @return + */ + // + public int[] bubbleSort(int[] arr) { + for (int i = 0; i < arr.length; i++) { + for (int j = 0; j < arr.length - i - 1; j++) { + if (arr[j + 1] < arr[j]) { + // swap elements + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + return arr; + } +} diff --git a/java/insertion_sort.java b/java/insertion_sort.java new file mode 100644 index 0000000..141d617 --- /dev/null +++ b/java/insertion_sort.java @@ -0,0 +1,23 @@ +/** + * Created by renu.yadav on 20/10/17. + */ +public class insertion_sort { + /** + * insertion sort picks up the current element(i) and checks it against its previous element(j = i-1 until j>=0) and place it + * takes O(n^2) and inplace algo with no extra space + * @param arr + * @return + */ + + public static int[] insertionSort(int[] arr) { + for (int i = 0; i < arr.length; i++) { + int keyToBeInserted = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > keyToBeInserted) { + arr[j + 1] = arr[j]; + } + arr[j + 1] = keyToBeInserted; + } + return arr; + } +} diff --git a/java/merge_sort.java b/java/merge_sort.java index 368cbd7..3056c9d 100644 --- a/java/merge_sort.java +++ b/java/merge_sort.java @@ -1,39 +1,64 @@ -class MergeSort -{ - public void mergeSort(int[] array, int low, int high){ - if(low < high){ - int middle = (low + high) / 2; - mergeSort(array, low, middle); - mergeSort(array, middle+1, high); - merge(array, low, middle, high); - } + +/** + * Created by renu.yadav on 20/10/17. + */ +public class merge_sort { + /** + * Merge sort also works on divide and conquer , by dividing array in halves and then merging them back in order + * Complexity: O(nlgn) in average , best and worst case + * SpaceComplexity: because of merging in order , we need extra space O(n) (its not an in place algorithm) + * + * @param arr + * @param low + * @param high + */ + public static void mergeSort(int[] arr, int low, int high) { + if (low >= high) + return; + int medium = low + (high - low) / 2; + mergeSort(arr, low, medium); + mergeSort(arr, medium + 1, high); + merge(arr, low, medium, high); } - - private void merge(int[] array, int low, int middle, int high){ - int[] helper = new int[array.length]; - for (int i = low; i <= high; i++) { - helper[i] = array[i]; + + public static void merge(int[] arr, int low, int medium, int high) { + int s1 = medium - low + 1;// two sizes + int s2 = high - medium; + int[] a1 = new int[s1]; + int[] a2 = new int[s2]; + for (int i = 0; i < s1; i++) { + a1[i] = arr[low + i]; } - - int helperLeft = low; - int helperRight = middle+1; - int current = low; - - while (helperLeft <= middle && helperRight <=high) { - if(helper[helperLeft] <= helper[helperRight]){ - array[current] = helper[helperLeft]; - helperLeft++; - - }else{ - array[current] = helper[helperRight]; - helperRight++; + for (int i = 0; i < s2; i++) { + a2[i] = arr[medium + i + 1]; + } + // now merge them in original array + int i = 0; + int j = 0; + int k = low; + // keep on merging them back in original , with correct order + while (i < s1 && j < s2) { + if (a1[i] <= a2[j]) { + arr[k] = a1[i]; + i++; + } else { + arr[k] = a2[j]; + j++; } - current ++; + k++; + } + // place all remaining elements of s1 + while (i < s1) { + arr[k] = a1[i]; + i++; + k++; + } - - int remaining = middle - helperLeft; - for (int i = 0; i <= remaining; i++) { - array[current+i] = helper[helperLeft+ i]; + // place all remaining elements of s2 + while (j < s2) { + arr[k] = a2[j]; + j++; + k++; } } } \ No newline at end of file diff --git a/java/quick_sort.java b/java/quick_sort.java index 6682d1d..5dff25c 100644 --- a/java/quick_sort.java +++ b/java/quick_sort.java @@ -1,29 +1,42 @@ -public class QuickSort -{ - public static void swap (int A[], int x, int y) - { - int temp = A[x]; - A[x] = A[y]; - A[y] = temp; - } +public class quick_sort { + /** + * quick sort works on divide and conquer technique , by chossing a pivot element + * below code chooses last arr[n-1] element as pivot element and recursively placing all elements < pivot element on left + * and all elements >pivot on right side of pivot , and thus placing pivot in correct place + * Complexity: takes O(n^2) in worst case (array is already sorted ) ,and O(nlgn) in avg and best case + * Space complexity ; No extra space required + * @param arr + * @param low + * @param high + */ - public static int partition(int A[], int f, int l) - { - int pivot = A[f]; - while (f < l) - { - while (A[f] < pivot) f++; - while (A[l] > pivot) l--; - swap (A, f, l); - } - return f; - } + public static void quickSort(int[] arr, int low, int high) { + if (low >= high) + return; + int pivot = partition(arr, low, high); + quickSort(arr, low, pivot - 1); + quickSort(arr, pivot + 1, high); - public static void Quicksort(int A[], int f, int l) - { - if (f >= l) return; - int pivot_index = partition(A, f, l); - Quicksort(A, f, pivot_index); - Quicksort(A, pivot_index+1, l); - } + } + + public static int partition(int[] arr, int low, int high) { + int pivot = arr[high]; + int i = low - 1; + int j = low; + while (j < high) { + if (arr[j] <= pivot) { + i++; + // swap elements + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + j++; + } + // swap arr[i+1] with arr[high] + int temp = arr[high]; + arr[high] = arr[i + 1]; + arr[i + 1] = temp; + return i + 1; + } } \ No newline at end of file diff --git a/java/selection_sort.java b/java/selection_sort.java new file mode 100644 index 0000000..03e9fce --- /dev/null +++ b/java/selection_sort.java @@ -0,0 +1,29 @@ +/** + * Created by renu.yadav on 20/10/17. + */ +public class selection_sort { + /** + * selection sort in iteration , picks up the ith min element and keep it in place + * so after every ith iteration ith minimum element is in place + * takes O(n^2) + * @param arr + * @return + */ + public static int[] selectionSort(int[] arr) { + for (int i = 0; i < arr.length; i++) { + int minIndex = i; + int minElement = arr[minIndex]; + for (int j = i + 1; j < arr.length; j++) { + if (arr[j] < minElement) { + minIndex = j; + } + } + // swap , arr[i] with minElement + int temp = arr[i]; + arr[i] = arr[minIndex]; + arr[minIndex] = temp; + } + return arr; + } + +}