Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of ThreeWayMerge, Insertion, Selection and Bubble sorting algorithms #310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
89 changes: 89 additions & 0 deletions src/com/interview/sort/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.interview.sort;

/*
* Introduction: Swaps the adjacent elements to sort those elements,
* so that at the end, all the elements bubbles up in their sorted position.
*
* Algorithm (Ascending order):
* 1. Mark the pointer at the first index
* 2. Take the pointer element and the one after the pointer element
* 3. If pointer element is greater than next element, swap them
* 4. Move the pointer across by one position
* 5. Continue 2,3,4 until the pointer reaches sorted index
* In every iteration the largest element of the unsorted part will bubble up at the end of the array in its sorted position.)
* 6. Repeat the above steps until the whole array is sorted (until the sorted index equals to the beginning index of the array.)
*
* Time Complexity:
* Best case -> O(n)
* Average case -> O(n^2)
* Worst Case -> O(n^2)
* */

public class BubbleSort {
public void swap(int[] input, int firstPosition, int secondPosition){
int temp = input[firstPosition];
input[firstPosition] = input[secondPosition];
input[secondPosition] = temp;
}

public void bubbleSortAsc(int[] input){
if(input.length>1){
for(int j = (input.length-1); j>0; j--){
for(int i=0; i<j; i++){
if(input[i]>input[i+1]){
swap(input,i,(i+1));
}
}
}
}
}

public void bubbleSortDesc(int[] input){
if(input.length>1){
for(int j = (input.length-1); j>0; j--){
for(int i=0; i<j; i++){
if(input[i]<input[i+1]){
swap(input,i,(i+1));
}
}
}
}
}

public void displaySortedArray(int[] input){
for(int x =0; x<input.length; x++){
System.out.print(input[x] + "\t");
}
System.out.println();
}

public static void main(String[] args){
//one element
int[] A = {3};
//Two elements
int[] B = {5,8};
//Best case -> sorted
int[] C = {1,3,5,6,7,9};
//Average case -> unsorted
int[] D = {4,5,9,1,4,2,7,2,4};
//Worst case - sorted in descending order
int[] E = {9,7,4,3,2,1};

BubbleSort bs = new BubbleSort();

bs.bubbleSortAsc(A);
bs.displaySortedArray(A);

bs.bubbleSortAsc(B);
bs.displaySortedArray(B);

bs.bubbleSortAsc(C);
bs.displaySortedArray(C);

bs.bubbleSortAsc(D);
bs.displaySortedArray(D);

bs.bubbleSortAsc(E);
bs.displaySortedArray(E);
}
}
88 changes: 88 additions & 0 deletions src/com/interview/sort/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.interview.sort;

/*
* Introduction: The array will be gradually separated into two parts as unsorted part and sorted part.
* Get the values one by one from the unsorted part and place them in the correct positions of the sorted part.
*
* Algorithm (Ascending order):
* 1. 1st element (single element) is always sorted. Start with the second element (pointer starts from 2nd element)
* 2. Take the element at the pointer and check if it's less than it's left element, if so swap them
* 3. Continue 2 until the left side of the pointer is correctly sorted
* 4. Increase the pointer by one and continue 2 & 3
*
* Time Complexity:
* Best case -> O(n)
* Average case -> O(n^2)
* Worst Case -> O(n^2)
* */

public class InsertionSort {
public void swap(int[] input, int firstPosition, int secondPosition){
int temp = input[firstPosition];
input[firstPosition] = input[secondPosition];
input[secondPosition] = temp;
}

//For ascending order
public void insertionSortAsc(int[] input){
if(input.length>1){
for(int pointer = 1; pointer<input.length; pointer++){
int j = pointer;
while(j>0 && (input[j]<input[j-1])){
swap(input,j,j-1);
j--;
}
}
}
}

//For descending order
public void insertionSortDesc(int[] input){
if(input.length>1){
for(int pointer = 1; pointer<input.length; pointer++){
int j = pointer;
while(j>0 && (input[j]>input[j-1])){
swap(input,j,j-1);
j--;
}
}
}
}

public void displaySortedArray(int[] input){
for(int x =0; x<input.length; x++){
System.out.print(input[x] + "\t");
}
System.out.println();
}

public static void main(String[] args){
//one element
int[] A = {3};
//Two elements
int[] B = {5,8};
//Best case -> sorted
int[] C = {1,3,5,6,7,9};
//Average case -> unsorted
int[] D = {4,5,9,1,4,2,7,2,4};
//Worst case - sorted in descending order
int[] E = {9,7,4,3,2,1};

InsertionSort is = new InsertionSort();

is.insertionSortAsc(A);
is.displaySortedArray(A);

is.insertionSortAsc(B);
is.displaySortedArray(B);

is.insertionSortAsc(C);
is.displaySortedArray(C);

is.insertionSortAsc(D);
is.displaySortedArray(D);

is.insertionSortAsc(E);
is.displaySortedArray(E);
}
}
101 changes: 101 additions & 0 deletions src/com/interview/sort/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.interview.sort;

/*
* Introduction: The array will be gradually separated into two parts as unsorted part and sorted part.
* Select the minimum value from the unsorted part and put it at the end of the sorted part.
*
* Algorithm (Ascending order):
* 1. Mark the first index of the array as the sortedIndex
* 2. Mark the first element of the array as the currentMinimum
* 3. Starting from the sortedIndex move the pointer across the array
* 4. If the pointer element is less than currentMinimum element, mark the pointer element as the new currentMinimum
* 5. Once the pointer reaches the end of the array, swap the element at the unsorted index with currentMinimum element
* 6. Move the sortedIndex to the next position and mark that element as the currentMinimum
* 7. Repeat 3,4,5 & 6 until the sortedIndex reaches the end of the array
*
* Time Complexity:
* Best case -> O(n^2)
* Average case -> O(n^2)
* Worst Case -> O(n^2)
* */

public class SelectionSort {
public void swap(int[] input, int firstPosition, int secondPosition){
int temp = input[firstPosition];
input[firstPosition] = input[secondPosition];
input[secondPosition] = temp;
}

//For ascending order
public void selectionSortAsc(int[] input){
if(input.length>1){
int sortedIndex = 0;
int currentMinimumIndex = 0;
while(sortedIndex<input.length){
for(int pointer = sortedIndex; pointer<input.length; pointer++){
if(input[pointer]<input[currentMinimumIndex]){
currentMinimumIndex = pointer;
}
}
swap(input,sortedIndex,currentMinimumIndex);
sortedIndex++;
currentMinimumIndex = sortedIndex;
}
}
}

//For descending order
public void selectionSortDesc(int[] input){
if(input.length>1){
int sortedIndex = 0;
int currentMaximumIndex = 0;
while(sortedIndex<input.length){
for(int pointer = sortedIndex; pointer<input.length; pointer++){
if(input[pointer]>input[currentMaximumIndex]){
currentMaximumIndex = pointer;
}
}
swap(input,sortedIndex,currentMaximumIndex);
sortedIndex++;
currentMaximumIndex = sortedIndex;
}
}
}

public void displaySortedArray(int[] input){
for(int x =0; x<input.length; x++){
System.out.print(input[x] + "\t");
}
System.out.println();
}

public static void main(String[] args){
//one element
int[] A = {3};
//Two elements
int[] B = {5,8};
//Best case -> sorted
int[] C = {1,3,5,6,7,9};
//Average case -> unsorted
int[] D = {4,5,9,1,4,2,7,2,4};
//Worst case - sorted in descending order
int[] E = {9,7,4,3,2,1};

SelectionSort ss = new SelectionSort();

ss.selectionSortAsc(A);
ss.displaySortedArray(A);

ss.selectionSortAsc(B);
ss.displaySortedArray(B);

ss.selectionSortAsc(C);
ss.displaySortedArray(C);

ss.selectionSortAsc(D);
ss.displaySortedArray(D);

ss.selectionSortAsc(E);
ss.displaySortedArray(E);
}
}
Loading