diff --git a/src/com/interview/sort/BubbleSort.java b/src/com/interview/sort/BubbleSort.java new file mode 100644 index 00000000..8a89bfc1 --- /dev/null +++ b/src/com/interview/sort/BubbleSort.java @@ -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; iinput[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 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); + } +} diff --git a/src/com/interview/sort/InsertionSort.java b/src/com/interview/sort/InsertionSort.java new file mode 100644 index 00000000..c561b1eb --- /dev/null +++ b/src/com/interview/sort/InsertionSort.java @@ -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; pointer0 && (input[j]1){ + for(int pointer = 1; pointer0 && (input[j]>input[j-1])){ + swap(input,j,j-1); + j--; + } + } + } + } + + public void displaySortedArray(int[] input){ + for(int x =0; x 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); + } +} diff --git a/src/com/interview/sort/SelectionSort.java b/src/com/interview/sort/SelectionSort.java new file mode 100644 index 00000000..d054a545 --- /dev/null +++ b/src/com/interview/sort/SelectionSort.java @@ -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(sortedIndex1){ + int sortedIndex = 0; + int currentMaximumIndex = 0; + while(sortedIndexinput[currentMaximumIndex]){ + currentMaximumIndex = pointer; + } + } + swap(input,sortedIndex,currentMaximumIndex); + sortedIndex++; + currentMaximumIndex = sortedIndex; + } + } + } + + public void displaySortedArray(int[] input){ + for(int x =0; x 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); + } +} diff --git a/src/com/interview/sort/ThreeWayMergeSort.java b/src/com/interview/sort/ThreeWayMergeSort.java new file mode 100644 index 00000000..d63f050d --- /dev/null +++ b/src/com/interview/sort/ThreeWayMergeSort.java @@ -0,0 +1,169 @@ +package com.interview.sort; + +/* +*Introduction: Same as the merge sort. The only difference is instead of recursively splitting the problem into 2 sub problems, +* in 3 way merge sort, we recursively split the problem into three sub problems. +* +* Algorithm +* Three way merge sort-> +* 1. Recursively divide the problem into 3 sub problems until we have individual elements +* 2. Take those elements three by three and merge them (perform three way merge) +* 3. Repeat 2 until we get the sorted array +* Three way merge-> +* 1. Take the three arrays +* 2. Set pointer indexes at the beginning of each one of them +* 3. Take a temporary array and set a pointer index at the beginning of it +* 4. Until one of the pointers of the three input arrays reach the end of that particular array, +* compare the three elements at the three indexes, +* get the smallest element out of the three elements and put it in the temp array, +* increase the temp array index and the index of the array which contained the smallest element by one +* 5. If at least one input array index reach the end, +* take the rest of the 2 arrays and perform 2 way merge for the rest of the elements in those 2 arrays +* +*Time Complexity: +* Best case -> O(nlog3n) +* Average case -> O(nlog3n) +* Worst case-> O(nlog3n) +* */ + +public class ThreeWayMergeSort { + public void threeWayMergeSort(int[] input, int low, int high){ + if(lowmid1){ + while(j<=mid2 && k<=high){ + if(input[j]<=input[k]){ + temp[l] = input[j]; + j++; + } else if(input[j]>input[k]){ + temp[l] = input[k]; + k++; + } + l++; + } + for(;j<=mid2;j++){ + temp[l] = input[j]; + l++; + } + for(;k<=high;k++){ + temp[l] =input[k]; + l++; + } + } else if(j>mid2){ + while(i<=mid1 && k<=high){ + if(input[i]<=input[k]){ + temp[l] = input[i]; + i++; + } else if(input[i]>input[k]){ + temp[l] = input[k]; + k++; + } + l++; + } + for(;i<=mid1;i++){ + temp[l] = input[i]; + l++; + } + for(;k<=high;k++){ + temp[l] =input[k]; + l++; + } + } else if(k>high){ + while(i<=mid1 && j<=mid2){ + if(input[i]<=input[j]){ + temp[l] = input[i]; + i++; + } else if(input[i]>input[j]){ + temp[l] = input[j]; + j++; + } + l++; + } + for(;i<=mid1;i++){ + temp[l] = input[i]; + l++; + } + for(;j<=mid2;j++){ + temp[l] =input[j]; + l++; + } + } + + int y = low; + for(int x=0; x< temp.length; x++){ + input[y] = temp[x]; + y++; + } + } + + public void sort(int[] input){ + threeWayMergeSort(input,0,(input.length-1)); + } + + public void displaySortedArray(int[] input){ + for(int x =0; x 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}; + + ThreeWayMergeSort twms = new ThreeWayMergeSort(); + + twms.sort(A); + twms.displaySortedArray(A); + + twms.sort(B); + twms.displaySortedArray(B); + + twms.sort(C); + twms.displaySortedArray(C); + + twms.sort(D); + twms.displaySortedArray(D); + + twms.sort(E); + twms.displaySortedArray(E); + } +}