Skip to content
This repository has been archived by the owner on Oct 2, 2018. It is now read-only.

Added Sorting algorithms #38

Merged
merged 2 commits into from
Oct 20, 2017
Merged
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
26 changes: 26 additions & 0 deletions java/bubble_sort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions java/insertion_sort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
89 changes: 57 additions & 32 deletions java/merge_sort.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
65 changes: 39 additions & 26 deletions java/quick_sort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions java/selection_sort.java
Original file line number Diff line number Diff line change
@@ -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;
}

}