> int find(T[] array, T value) {
- for (int i = 0; i < array.length ; i++) {
- if (array[i].compareTo(value) == 0) {
- return i;
- }
- }
- return -1;
- }
-
-
- public static void main(String[] args) {
- //just generate data
- Random r = new Random();
- int size = 200;
- int maxElement = 100;
- Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[]::new);
-
-
- //the element that should be found
- Integer shouldBeFound = integers[r.nextInt(size - 1)];
-
- LinearSearch search = new LinearSearch();
- int atIndex = search.find(integers, shouldBeFound);
-
- System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
- , shouldBeFound, integers[atIndex], atIndex, size));
- }
-
-}
diff --git a/Searches/src/search/SaddlebackSearch.java b/Searches/src/search/SaddlebackSearch.java
deleted file mode 100644
index 68779db8f79f..000000000000
--- a/Searches/src/search/SaddlebackSearch.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package search;
-
-import java.util.Scanner;
-
-/**
- * Program to perform Saddleback Search
- * Given a sorted 2D array(elements are sorted across every row and column, assuming ascending order)
- * of size n*m we can search a given element in O(n+m)
- *
- * we start from bottom left corner
- * if the current element is greater than the given element then we move up
- * else we move right
- * Sample Input:
- * 5 5 ->Dimensions
- * -10 -5 -3 4 9
- * -6 -2 0 5 10
- * -4 -1 1 6 12
- * 2 3 7 8 13
- * 100 120 130 140 150
- * 140 ->element to be searched
- * output: 4 3 // first value is row, second one is column
- *
- * @author Nishita Aggarwal
- */
-public class SaddlebackSearch {
-
- /**
- * This method performs Saddleback Search
- *
- * @param arr The **Sorted** array in which we will search the element.
- * @param row the current row.
- * @param col the current column.
- * @param key the element that we want to search for.
- * @return The index(row and column) of the element if found.
- * Else returns -1 -1.
- */
- private static int[] find(int arr[][], int row, int col, int key) {
-
- //array to store the answer row and column
- int ans[] = {-1, -1};
- if (row < 0 || col >= arr[row].length) {
- return ans;
- }
- if (arr[row][col] == key) {
- ans[0] = row;
- ans[1] = col;
- return ans;
- }
- //if the current element is greater than the given element then we move up
- else if (arr[row][col] > key) {
- return find(arr, row - 1, col, key);
- }
- //else we move right
- return find(arr, row, col + 1, key);
- }
-
- /**
- * Main method
- *
- * @param args Command line arguments
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- int arr[][];
- int i, j, rows = sc.nextInt(), col = sc.nextInt();
- arr = new int[rows][col];
- for (i = 0; i < rows; i++) {
- for (j = 0; j < col; j++) {
- arr[i][j] = sc.nextInt();
- }
- }
- int ele = sc.nextInt();
- //we start from bottom left corner
- int ans[] = find(arr, rows - 1, 0, ele);
- System.out.println(ans[0] + " " + ans[1]);
- sc.close();
- }
-
-}
diff --git a/Searches/src/search/SearchAlgorithm.java b/Searches/src/search/SearchAlgorithm.java
deleted file mode 100644
index ca3bf59ba552..000000000000
--- a/Searches/src/search/SearchAlgorithm.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package search;
-
-/**
- * The common interface of most searching algorithms
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- **/
-public interface SearchAlgorithm {
-
- /**
- *
- * @param key is an element which should be found
- * @param array is an array where the element should be found
- * @param Comparable type
- * @return first found index of the element
- */
- > int find(T array[], T key);
-
-}
diff --git a/Searches/src/search/TernarySearch.java b/Searches/src/search/TernarySearch.java
deleted file mode 100644
index 7e60edf3aad7..000000000000
--- a/Searches/src/search/TernarySearch.java
+++ /dev/null
@@ -1,106 +0,0 @@
-package search;
-
-
-import java.util.Arrays;
-import java.util.Random;
-import java.util.stream.Stream;
-
-import static java.lang.String.format;
-
-/**
- *
- *
- *
- * A ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function
- * The algorithm determines either that the minimum or maximum cannot be in the first third of the domain
- * or that it cannot be in the last third of the domain, then repeats on the remaining third.
- *
- * Worst-case performance Θ(log3(N))
- * Best-case performance O(1)
- * Average performance Θ(log3(N))
- * Worst-case space complexity O(1)
- *
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- * @see SearchAlgorithm
- * @see IterativeBinarySearch
- *
- */
-
-public class TernarySearch implements SearchAlgorithm{
-
- /**
- * @param arr The **Sorted** array in which we will search the element.
- * @param value The value that we want to search for.
- * @return The index of the element if found.
- * Else returns -1.
- */
- @Override
- public > int find(T[] arr, T value){
- return ternarySearch(arr, value, 0, arr.length - 1);
- }
-
- /**
- * @param arr The **Sorted** array in which we will search the element.
- * @param key The value that we want to search for.
- * @param start The starting index from which we will start Searching.
- * @param end The ending index till which we will Search.
- * @return Returns the index of the Element if found.
- * Else returns -1.
- */
- private > int ternarySearch(T[] arr, T key, int start, int end) {
- if (start > end){
- return -1;
- }
- /* First boundary: add 1/3 of length to start */
- int mid1 = start + (end - start) / 3;
- /* Second boundary: add 2/3 of length to start */
- int mid2 = start + 2 * (end - start) / 3;
-
- if (key.compareTo(arr[mid1]) == 0) {
- return mid1;
- }
- else if (key.compareTo(arr[mid2]) == 0) {
- return mid2;
- }
-
- /* Search the first (1/3) rd part of the array.*/
-
- else if (key.compareTo(arr[mid1]) < 0) {
- return ternarySearch(arr, key, start, --mid1);
- }
- /* Search 3rd (1/3)rd part of the array */
-
- else if (key.compareTo(arr[mid2]) > 0) {
- return ternarySearch(arr, key, ++mid2, end);
- }
- /* Search middle (1/3)rd part of the array */
-
- else {
- return ternarySearch(arr, key, mid1, mid2);
- }
- }
-
- public static void main(String[] args) {
- //just generate data
- Random r = new Random();
- int size = 100;
- int maxElement = 100000;
- Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[]::new);
-
-
- //the element that should be found
- Integer shouldBeFound = integers[r.nextInt(size - 1)];
-
- TernarySearch search = new TernarySearch();
- int atIndex = search.find(integers, shouldBeFound);
-
- System.out.println(format("Should be found: %d. Found %d at index %d. An array length %d"
- , shouldBeFound, integers[atIndex], atIndex, size));
-
- int toCheck = Arrays.binarySearch(integers, shouldBeFound);
- System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));
-
- }
-}
\ No newline at end of file
diff --git a/SkylineProblem/SkylineProblem.java b/SkylineProblem/SkylineProblem.java
deleted file mode 100644
index 121116e16685..000000000000
--- a/SkylineProblem/SkylineProblem.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/**
- * Given n rectangular buildings in a 2-dimensional city, computes the skyline of these buildings,
- * eliminating hidden lines. The main task is to view buildings from a side and remove all sections
- * that are not visible.
- * Source for explanation: https://www.geeksforgeeks.org/the-skyline-problem-using-divide-and-conquer-algorithm/
- */
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.Scanner;
-
-public class SkylineProblem {
- Building[] building;
- int count;
-
- public void run() {
- Scanner sc = new Scanner(System.in);
-
- int num = sc.nextInt();
- this.building = new Building[num];
-
- for(int i = 0; i < num; i++) {
- String input = sc.next();
- String[] data = input.split(",");
- this.add(Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]));
- }
- this.print(this.findSkyline(0, num - 1));
-
- sc.close();
- }
-
- public void add(int left, int height, int right) {
- building[count++] = new Building(left, height, right);
- }
-
- public void print(ArrayList skyline) {
- Iterator it = skyline.iterator();
-
- while(it.hasNext()) {
- Skyline temp = it.next();
- System.out.print(temp.coordinates + "," + temp.height);
- if(it.hasNext()) {
- System.out.print(",");
- }
- }
-
- }
-
- public ArrayList findSkyline(int start, int end) {
- if(start == end) {
- ArrayList list = new ArrayList<>();
- list.add(new Skyline(building[start].left, building[start].height));
- list.add(new Skyline(building[end].right, 0));
-
- return list;
- }
-
- int mid = (start + end) / 2;
-
- ArrayList sky1 = this.findSkyline(start, mid);
- ArrayList sky2 = this.findSkyline(mid + 1, end);
-
- return this.mergeSkyline(sky1, sky2);
- }
-
- public ArrayList mergeSkyline(ArrayList sky1, ArrayList sky2) {
- int currentH1 = 0, currentH2 = 0;
- ArrayList skyline = new ArrayList<>();
- int maxH = 0;
-
- while(!sky1.isEmpty() && !sky2.isEmpty()) {
- if(sky1.get(0).coordinates < sky2.get(0).coordinates) {
- int currentX = sky1.get(0).coordinates;
- currentH1 = sky1.get(0).height;
-
- if(currentH1 < currentH2) {
- sky1.remove(0);
- if(maxH != currentH2) skyline.add(new Skyline(currentX, currentH2));
- } else {
- maxH = currentH1;
- sky1.remove(0);
- skyline.add(new Skyline(currentX, currentH1));
- }
- } else {
- int currentX = sky2.get(0).coordinates;
- currentH2 = sky2.get(0).height;
-
- if(currentH2 < currentH1) {
- sky2.remove(0);
- if(maxH != currentH1) skyline.add(new Skyline(currentX, currentH1));
- } else {
- maxH = currentH2;
- sky2.remove(0);
- skyline.add(new Skyline(currentX, currentH2));
- }
- }
- }
-
- while(!sky1.isEmpty()) {
- skyline.add(sky1.get(0));
- sky1.remove(0);
- }
-
- while(!sky2.isEmpty()) {
- skyline.add(sky2.get(0));
- sky2.remove(0);
- }
-
- return skyline;
- }
-
- public class Skyline {
- public int coordinates;
- public int height;
-
- public Skyline(int coordinates, int height) {
- this.coordinates = coordinates;
- this.height = height;
- }
- }
-
- public class Building {
- public int left;
- public int height;
- public int right;
-
- public Building(int left, int height, int right) {
- this.left = left;
- this.height = height;
- this.right = right;
- }
- }
-
- public static void main(String[] args) {
- SkylineProblem skylineProblem = new SkylineProblem();
- skylineProblem.run();
- }
-}
diff --git a/Sorts/src/sort/BinaryTreeSort.java b/Sorts/src/sort/BinaryTreeSort.java
deleted file mode 100644
index 59d58ab61987..000000000000
--- a/Sorts/src/sort/BinaryTreeSort.java
+++ /dev/null
@@ -1,96 +0,0 @@
-package sort;
-
-import static sort.SortUtils.less;
-import static sort.SortUtils.print;
-
-/**
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- * @see SortAlgorithm
- *
- */
-public class BinaryTreeSort implements SortAlgorithm {
-
- interface TreeVisitor> {
- void visit(Node node);
- }
-
- private static class SortVisitor> implements TreeVisitor {
-
- private final T[] array;
- private int counter;
-
- SortVisitor(T[] array) {
- this.array = array;
- }
-
- @Override
- public void visit(Node node) {
- array[counter++] = node.value;
- }
- }
-
- private static class Node>{
- private T value;
- private Node left;
- private Node right;
-
- Node(T value) {
- this.value = value;
- }
-
- void insert(Node node) {
- if (less(node.value, value)){
- if (left != null) left.insert(node);
- else left = node;
- }
- else {
- if (right != null) right.insert(node);
- else right = node;
- }
- }
-
- void traverse(TreeVisitor visitor) {
- if ( left != null)
- left.traverse(visitor);
-
- visitor.visit(this);
-
- if ( right != null )
- right.traverse(visitor );
- }
-
- }
-
-
- @Override
- public > T[] sort(T[] array) {
-
- Node root = new Node<>(array[0]);
- for (int i = 1; i < array.length; i++) {
- root.insert(new Node<>(array[i]));
- }
-
- root.traverse(new SortVisitor<>(array));
-
- return array;
- }
-
-
- public static void main(String args[]) {
-
- Integer[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12};
- BinaryTreeSort treeSort = new BinaryTreeSort();
- Integer[] sorted = treeSort.sort(intArray);
- print(sorted);
-
- Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6};
- print(treeSort.sort(decimalArray));
-
- String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"};
- print(treeSort.sort(stringArray));
- }
-
-}
-
diff --git a/Sorts/src/sort/BogoSort.java b/Sorts/src/sort/BogoSort.java
deleted file mode 100644
index 1079ca21c27a..000000000000
--- a/Sorts/src/sort/BogoSort.java
+++ /dev/null
@@ -1,59 +0,0 @@
-package sort;
-
-import java.util.Random;
-
-import static sort.SortUtils.*;
-
-
-/**
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- * @see SortAlgorithm
- *
- */
-public class BogoSort implements SortAlgorithm {
-
- private static final Random random = new Random();
-
-
- private static > boolean isSorted(T array[]){
- for(int i = 0; i void nextPermutation(T array[]){
- int length = array.length;
-
- for (int i = 0; i < array.length; i++) {
- int randomIndex = i + random.nextInt(length - i);
- swap(array, randomIndex, i);
- }
- }
-
- public > T[] sort(T array[]) {
- while(!isSorted(array)){
- nextPermutation(array);
- }
- return array;
- }
-
- // Driver Program
- public static void main(String[] args) {
- // Integer Input
- Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
-
- BogoSort bogoSort = new BogoSort();
-
- // print a sorted array
- print(bogoSort.sort(integers));
-
- // String Input
- String[] strings = {"c", "a", "e", "b","d"};
-
- print(bogoSort.sort(strings));
- }
-}
diff --git a/Sorts/src/sort/BubbleSort.java b/Sorts/src/sort/BubbleSort.java
deleted file mode 100644
index 1173245fcabf..000000000000
--- a/Sorts/src/sort/BubbleSort.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-/**
- *
- * @author Varun Upadhyay (https://github.com/varunu28)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- * @see SortAlgorithm
- */
-
-class BubbleSort implements SortAlgorithm {
- /**
- * This method implements the Generic Bubble Sort
- *
- * @param array The array to be sorted
- * Sorts the array in increasing order
- **/
-
- @Override
- public > T[] sort(T array[]) {
- int last = array.length;
- //Sorting
- boolean swap;
- do {
- swap = false;
- for (int count = 0; count < last-1; count++) {
- if (less(array[count], array[count + 1])) {
- swap = swap(array, count, count + 1);
- }
- }
- last--;
- } while (swap);
- return array;
- }
-
- // Driver Program
- public static void main(String[] args) {
-
- // Integer Input
- Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
- BubbleSort bubbleSort = new BubbleSort();
- bubbleSort.sort(integers);
-
- // Output => 231, 78, 54, 23, 12, 9, 6, 4, 1
- print(integers);
-
- // String Input
- String[] strings = {"c", "a", "e", "b","d"};
- //Output => e, d, c, b, a
- print(bubbleSort.sort(strings));
-
- }
-}
diff --git a/Sorts/src/sort/CocktailShakerSort.java b/Sorts/src/sort/CocktailShakerSort.java
deleted file mode 100644
index 7982b8fcfcae..000000000000
--- a/Sorts/src/sort/CocktailShakerSort.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-/**
- *
- * @author Mateus Bizzo (https://github.com/MattBizzo)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- */
-
-class CocktailShakerSort implements SortAlgorithm {
-
- /**
- * This method implements the Generic Cocktail Shaker Sort
- *
- * @param array The array to be sorted
- * Sorts the array in increasing order
- **/
-
- @Override
- public > T[] sort(T[] array) {
-
- int length = array.length;
- int left = 0;
- int right = length - 1;
- int swappedLeft, swappedRight;
- while (left < right) {
- // front
- swappedRight = 0;
- for (int i = left; i < right; i++) {
- if (less(array[i + 1], array[i])) {
- swap(array, i, i + 1);
- swappedRight = i;
- }
- }
- // back
- right = swappedRight;
- swappedLeft = length - 1;
- for (int j = right; j > left; j--) {
- if (less(array[j], array[j - 1])) {
- swap(array, j - 1, j);
- swappedLeft = j;
- }
- }
- left = swappedLeft;
- }
- return array;
-
- }
-
- // Driver Program
- public static void main(String[] args) {
- // Integer Input
- Integer[] integers = { 4, 23, 6, 78, 1, 54, 231, 9, 12 };
- CocktailShakerSort shakerSort = new CocktailShakerSort();
-
- // Output => 1 4 6 9 12 23 54 78 231
- print(shakerSort.sort(integers));
-
- // String Input
- String[] strings = { "c", "a", "e", "b", "d" };
- print(shakerSort.sort(strings));
- }
-
-
-}
diff --git a/Sorts/src/sort/CombSort.java b/Sorts/src/sort/CombSort.java
deleted file mode 100644
index 492605ca56e7..000000000000
--- a/Sorts/src/sort/CombSort.java
+++ /dev/null
@@ -1,76 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-
-/**
- *
- * Comb Sort algorithm implementation
- *
- * Best-case performance O(n * log(n))
- * Worst-case performance O(n ^ 2)
- * Worst-case space complexity O(1)
- *
- * Comb sort improves on bubble sort.
- *
- *
- * @see BubbleSort
- * @see SortAlgorithm
- *
- * @author Sandeep Roy (https://github.com/sandeeproy99)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- */
-class CombSort implements SortAlgorithm {
-
- // To find gap between elements
- private int nextGap(int gap) {
- // Shrink gap by Shrink factor
- gap = ( gap * 10 ) / 13;
- return ( gap < 1 ) ? 1 : gap;
- }
-
- /**
- * Function to sort arr[] using Comb
- * @param arr - an array should be sorted
- * @return sorted array
- */
- @Override
- public > T[] sort(T arr[]) {
- int size = arr.length;
-
- // initialize gap
- int gap = size;
-
- // Initialize swapped as true to make sure that loop runs
- boolean swapped = true;
-
- // Keep running while gap is more than 1 and last iteration caused a swap
- while (gap != 1 || swapped) {
- // Find next gap
- gap = nextGap(gap);
-
- // Initialize swapped as false so that we can check if swap happened or not
- swapped = false;
-
- // Compare all elements with current gap
- for (int i = 0; i < size - gap ; i++) {
- if (less(arr[i + gap], arr[i])) {
- // Swap arr[i] and arr[i+gap]
- swapped = swap(arr, i, i + gap);
- }
- }
- }
- return arr;
- }
-
- // Driver method
- public static void main(String args[]) {
- CombSort ob = new CombSort();
- Integer arr[] = {8, 4, 1, 56, 3, -44, -1 , 0 , 36, 34, 8, 12 , -66, - 78, 23, -6, 28, 0};
- ob.sort(arr);
-
- System.out.println("sorted array");
- print(arr);
- }
-}
diff --git a/Sorts/src/sort/CountingSort.java b/Sorts/src/sort/CountingSort.java
deleted file mode 100644
index 39442a00fbb8..000000000000
--- a/Sorts/src/sort/CountingSort.java
+++ /dev/null
@@ -1,102 +0,0 @@
-package sort;
-
-import java.util.*;
-import java.util.stream.IntStream;
-import java.util.stream.Stream;
-
-import static java.util.stream.Collectors.toList;
-import static java.util.stream.Collectors.toMap;
-import static sort.SortUtils.print;
-
-/**
- *
- * @author Youssef Ali (https://github.com/youssefAli11997)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- */
-class CountingSort implements SortAlgorithm {
-
-
-
- @Override
- public > T[] sort(T[] unsorted) {
- return sort(Arrays.asList(unsorted)).toArray(unsorted);
- }
-
- /**
- * This method implements the Generic Counting Sort
- *
- * @param list The list to be sorted
- *
- * Sorts the list in increasing order
- * The method uses list elements as keys in the frequency map
- **/
- @Override
- public > List sort(List list) {
-
- Map frequency = new TreeMap<>();
- // The final output array
- List sortedArray = new ArrayList<>(list.size());
-
- // Counting the frequency of @param array elements
- list.forEach(v -> frequency.put(v, frequency.getOrDefault(v, 0) + 1));
-
- // Filling the sortedArray
- for(Map.Entry element : frequency.entrySet()) {
- for(int j=0; j> List streamSort(List list) {
- return list.stream()
- .collect(toMap(k -> k, v -> 1, (v1, v2) -> v1 + v2, TreeMap::new))
- .entrySet()
- .stream()
- .flatMap(entry -> IntStream.rangeClosed(1, entry.getValue()).mapToObj(t -> entry.getKey()))
- .collect(toList());
- }
-
- // Driver Program
- public static void main(String[] args) {
- // Integer Input
- List unsortedInts = Stream.of(4, 23, 6, 78, 1, 54, 23, 1, 9, 231, 9, 12).collect(toList());
- CountingSort countingSort = new CountingSort();
-
- System.out.println("Before Sorting:");
- print(unsortedInts);
-
- // Output => 1 1 4 6 9 9 12 23 23 54 78 231
- System.out.println("After Sorting:");
- print(countingSort.sort(unsortedInts));
- System.out.println("After Sorting By Streams:");
- print(streamSort(unsortedInts));
-
- System.out.println("\n------------------------------\n");
-
- // String Input
- List unsortedStrings = Stream.of("c", "a", "e", "b","d", "a", "f", "g", "c").collect(toList());
-
- System.out.println("Before Sorting:");
- print(unsortedStrings);
-
- //Output => a a b c c d e f g
- System.out.println("After Sorting:");
- print(countingSort.sort(unsortedStrings));
-
- System.out.println("After Sorting By Streams:");
- print(streamSort(unsortedStrings));
-
- }
-}
diff --git a/Sorts/src/sort/CycleSort.java b/Sorts/src/sort/CycleSort.java
deleted file mode 100644
index ea4f05535c7a..000000000000
--- a/Sorts/src/sort/CycleSort.java
+++ /dev/null
@@ -1,81 +0,0 @@
-package sort;
-
-import static sort.SortUtils.less;
-import static sort.SortUtils.print;
-
-/**
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- */
-class CycleSort implements SortAlgorithm {
-
-
- @Override
- public > T[] sort(T[] arr) {
- int n = arr.length;
-
- // traverse array elements
- for (int j = 0; j <= n - 2; j++) {
- // initialize item as starting point
- T item = arr[j];
-
- // Find position where we put the item.
- int pos = j;
- for (int i = j + 1; i < n; i++)
- if (less(arr[i], item)) pos++;
-
- // If item is already in correct position
- if (pos == j) continue;
-
- // ignore all duplicate elements
- while (item.compareTo(arr[pos]) == 0)
- pos += 1;
-
- // put the item to it's right position
- if (pos != j) {
- item = replace(arr, pos, item);
- }
-
- // Rotate rest of the cycle
- while (pos != j) {
- pos = j;
-
- // Find position where we put the element
- for (int i = j + 1; i < n; i++)
- if (less(arr[i], item)){
- pos += 1;
- }
-
-
- // ignore all duplicate elements
- while (item.compareTo(arr[pos]) == 0)
- pos += 1;
-
- // put the item to it's right position
- if (item != arr[pos]) {
- item = replace(arr, pos, item);
- }
- }
- }
-
- return arr;
- }
-
- private > T replace(T[] arr, int pos, T item){
- T temp = item;
- item = arr[pos];
- arr[pos] = temp;
- return item;
- }
-
-
-
- public static void main(String[] args) {
- Integer arr[] = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 };
- CycleSort cycleSort = new CycleSort();
- cycleSort.sort(arr);
-
- System.out.println("After sort : ");
- print(arr);
- }
-
-}
diff --git a/Sorts/src/sort/GnomeSort.java b/Sorts/src/sort/GnomeSort.java
deleted file mode 100644
index 14af67c65bb6..000000000000
--- a/Sorts/src/sort/GnomeSort.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-/**
- * Implementation of gnome sort
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- * @since 2018-04-10
- *
- **/
-public class GnomeSort implements SortAlgorithm{
-
- @Override
- public > T[] sort(T[] arr) {
- int i = 1;
- int j = 2;
- while (i < arr.length){
- if ( less(arr[i - 1], arr[i]) ) i = j++;
- else {
- swap(arr, i - 1, i);
- if (--i == 0){ i = j++; }
- }
- }
-
- return null;
- }
-
- public static void main(String[] args) {
- Integer[] integers = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 };
- String[] strings = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"};
- GnomeSort gnomeSort = new GnomeSort();
-
- gnomeSort.sort(integers);
- gnomeSort.sort(strings);
-
- System.out.println("After sort : ");
- print(integers);
- print(strings);
-
-
- }
-
-}
diff --git a/Sorts/src/sort/HeapSort.java b/Sorts/src/sort/HeapSort.java
deleted file mode 100644
index 6fab3747fd2f..000000000000
--- a/Sorts/src/sort/HeapSort.java
+++ /dev/null
@@ -1,131 +0,0 @@
-package sort;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import static sort.SortUtils.*;
-
-/**
- * Heap Sort Algorithm
- * Implements MinHeap
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- */
-public class HeapSort implements SortAlgorithm {
-
-
- private static class Heap> {
- /** Array to store heap */
- private T[] heap;
-
- /**
- * Constructor
- *
- * @param heap array of unordered integers
- */
- public Heap(T[] heap) {
- this.heap = heap;
- }
-
- /**
- * Heapifies subtree from top as root to last as last child
- *
- * @param rootIndex index of root
- * @param lastChild index of last child
- */
- private void heapSubtree(int rootIndex, int lastChild) {
- int leftIndex = rootIndex * 2 + 1;
- int rightIndex = rootIndex * 2 + 2;
- T root = heap[rootIndex];
- if (rightIndex <= lastChild) { // if has right and left children
- T left = heap[leftIndex];
- T right = heap[rightIndex];
- if (less(left, right) && less(left, root)) {
- swap(heap, leftIndex, rootIndex);
- heapSubtree(leftIndex, lastChild);
- } else if (less(right, root)) {
- swap(heap, rightIndex, rootIndex);
- heapSubtree(rightIndex, lastChild);
- }
- } else if (leftIndex <= lastChild) { // if no right child, but has left child
- T left = heap[leftIndex];
- if (less(left, root)) {
- swap(heap, leftIndex, rootIndex);
- heapSubtree(leftIndex, lastChild);
- }
- }
- }
-
-
- /**
- * Makes heap with root as root
- *
- * @param root index of root of heap
- */
- private void makeMinHeap(int root) {
- int leftIndex = root * 2 + 1;
- int rightIndex = root * 2 + 2;
- boolean hasLeftChild = leftIndex < heap.length;
- boolean hasRightChild = rightIndex < heap.length;
- if (hasRightChild) { //if has left and right
- makeMinHeap(leftIndex);
- makeMinHeap(rightIndex);
- heapSubtree(root, heap.length - 1);
- } else if (hasLeftChild) {
- heapSubtree(root, heap.length - 1);
- }
- }
-
- /**
- * Gets the root of heap
- *
- * @return root of heap
- */
- private T getRoot(int size) {
- swap(heap, 0, size);
- heapSubtree(0, size - 1);
- return heap[size]; // return old root
- }
-
-
-
-
-
- }
-
- @Override
- public > T[] sort(T[] unsorted) {
- return sort(Arrays.asList(unsorted)).toArray(unsorted);
- }
-
- @Override
- public > List sort(List unsorted) {
- int size = unsorted.size();
-
- @SuppressWarnings("unchecked")
- Heap heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()]));
-
- heap.makeMinHeap(0); // make min heap using index 0 as root.
- List sorted = new ArrayList<>(size);
- while (size > 0) {
- T min = heap.getRoot(--size);
- sorted.add(min);
- }
-
- return sorted;
- }
-
- /**
- * Main method
- *
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Integer[] heap = {4, 23, 6, 78, 1, 54, 231, 9, 12};
- HeapSort heapSort = new HeapSort();
- print(heapSort.sort(heap));
- }
-
-}
diff --git a/Sorts/src/sort/InsertionSort.java b/Sorts/src/sort/InsertionSort.java
deleted file mode 100644
index 593614304c5b..000000000000
--- a/Sorts/src/sort/InsertionSort.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package sort;
-
-import static sort.SortUtils.less;
-import static sort.SortUtils.print;
-
-/**
- *
- * @author Varun Upadhyay (https://github.com/varunu28)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- */
-
-class InsertionSort implements SortAlgorithm {
-
- /**
- * This method implements the Generic Insertion Sort
- * Sorts the array in increasing order
- *
- * @param array The array to be sorted
- *
- **/
-
- @Override
- public > T[] sort(T[] array) {
- for (int j = 1; j < array.length; j++) {
-
- // Picking up the key(Card)
- T key = array[j];
- int i = j - 1;
-
- while (i >= 0 && less(key, array[i])) {
- array[i + 1] = array[i];
- i--;
- }
- // Placing the key (Card) at its correct position in the sorted subarray
- array[i + 1] = key;
- }
- return array;
- }
-
- // Driver Program
- public static void main(String[] args) {
- // Integer Input
- Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
-
- InsertionSort sort = new InsertionSort();
-
- sort.sort(integers);
-
- // Output => 1 4 6 9 12 23 54 78 231
- print(integers);
-
- // String Input
- String[] strings = {"c", "a", "e", "b","d"};
-
- sort.sort(strings);
-
- //Output => a b c d e
- print(strings);
- }
-}
diff --git a/Sorts/src/sort/MergeSort.java b/Sorts/src/sort/MergeSort.java
deleted file mode 100644
index 2c7a141bdc32..000000000000
--- a/Sorts/src/sort/MergeSort.java
+++ /dev/null
@@ -1,113 +0,0 @@
-package sort;
-
-import static sort.SortUtils.print;
-
-/**
- * This method implements the Generic Merge Sort
- *
- *
- * @author Varun Upadhyay (https://github.com/varunu28)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- *
- * @see SortAlgorithm
- *
- */
-
-class MergeSort implements SortAlgorithm {
-
-
- /**
- * This method implements the Generic Merge Sort
- * @param unsorted the array which should be sorted
- * @param Comparable class
- * @return sorted array
- */
- @Override
- @SuppressWarnings("unchecked")
- public > T[] sort(T[] unsorted) {
- T[] tmp = (T[]) new Comparable[unsorted.length];
- doSort(unsorted, tmp, 0, unsorted.length - 1);
- return unsorted;
- }
-
- /**
- *
- * @param arr The array to be sorted
- * @param temp The copy of the actual array
- * @param left The first index of the array
- * @param right The last index of the array
- * Recursively sorts the array in increasing order
- **/
- private static > void doSort(T[] arr, T[] temp, int left, int right) {
- if (left < right) {
- int mid = left + (right - left) / 2;
- doSort(arr, temp, left, mid);
- doSort(arr, temp,mid + 1, right);
- merge(arr, temp, left, mid, right);
- }
-
- }
-
- /**
- * This method implements the merge step of the merge sort
- *
- * @param arr The array to be sorted
- * @param temp The copy of the actual array
- * @param left The first index of the array
- * @param mid The middle index of the array
- * @param right The last index of the array
- * merges two parts of an array in increasing order
- **/
-
- private static > void merge(T[] arr, T[] temp, int left, int mid, int right) {
- System.arraycopy(arr, left, temp, left, right - left + 1);
-
-
- int i= left;
- int j = mid + 1;
- int k = left;
-
- while (i <= mid && j <= right) {
- if (temp[i].compareTo(temp[j]) <= 0) {
- arr[k] = temp[i];
- i++;
- }
- else {
- arr[k] = temp[j];
- j++;
- }
- k++;
- }
-
- while (i <= mid) {
- arr[k] = temp[i];
- i++;
- k++;
- }
-
- while (j <= right) {
- arr[k] = temp[j];
- j++;
- k++;
- }
- }
-
- // Driver program
- public static void main(String[] args) {
-
- // Integer Input
- Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
- MergeSort mergeSort = new MergeSort();
- mergeSort.sort(arr);
-
- // Output => 1 4 6 9 12 23 54 78 231
- print(arr);
-
- // String Inpu
- String[] stringArray = {"c", "a", "e", "b","d"};
- mergeSort.sort(stringArray);
- //Output => a b c d e
- print(stringArray);
- }
-}
diff --git a/Sorts/src/sort/PancakeSort.java b/Sorts/src/sort/PancakeSort.java
deleted file mode 100644
index 902db4b39cb8..000000000000
--- a/Sorts/src/sort/PancakeSort.java
+++ /dev/null
@@ -1,47 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-/**
- * Implementation of gnome sort
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- * @since 2018-04-10
- *
- **/
-public class PancakeSort implements SortAlgorithm {
-
-
- @Override
- public > T[] sort(T[] array){
- int size = array.length;
-
- for (int i = 0; i < size; i++) {
- T max = array[0];
- int index = 0;
- for (int j = 0; j < size - i; j++) {
- if ( less(max, array[j]) ) {
- max = array[j];
- index = j;
- }
- }
- flip(array, index, array.length - 1 - i);
- }
- return array;
- }
-
-
- public static void main(String[] args) {
-
- Integer[] arr = {10, 9, 8, 7, 6, 15, 14, 7, 4, 3, 8, 6, 3, 1 ,2, -2, -5, -8, -3, -1, 13, 12, 11, 5, 4, 3, 2, 1};
- PancakeSort pancakeSort = new PancakeSort();
- System.out.println("After sorting:");
- pancakeSort.sort(arr);
- print(arr);
- }
-
-
-
-
-
-}
diff --git a/Sorts/src/sort/QuickSort.java b/Sorts/src/sort/QuickSort.java
deleted file mode 100644
index 4159fc829881..000000000000
--- a/Sorts/src/sort/QuickSort.java
+++ /dev/null
@@ -1,97 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-/**
- *
- * @author Varun Upadhyay (https://github.com/varunu28)
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- *
- * @see SortAlgorithm
- *
- */
-class QuickSort implements SortAlgorithm {
-
-
-
- /**
- * This method implements the Generic Quick Sort
- *
- * @param array The array to be sorted
- * Sorts the array in increasing order
- **/
-
- @Override
- public > T[] sort(T[] array) {
- doSort(array, 0, array.length - 1);
- return array;
- }
-
-
- /**
- * The sorting process
- *
- * @param left The first index of an array
- * @param right The last index of an array
- * @param array The array to be sorted
- *
- **/
-
- private static > void doSort(T[] array, int left, int right) {
- if (left < right) {
- int pivot = partition(array, left, right);
- doSort(array, left, pivot - 1);
- doSort(array, pivot , right);
- }
- }
-
- /**
- * This method finds the partition index for an array
- *
- * @param array The array to be sorted
- * @param left The first index of an array
- * @param right The last index of an array
- * Finds the partition index of an array
- **/
-
- private static > int partition(T[] array, int left, int right) {
- int mid = (left + right) / 2;
- T pivot = array[mid];
-
- while(left <= right) {
- while(less(array[left], pivot)){
- ++left;
- }
- while(less(pivot, array[right])) {
- --right;
- }
- if(left <= right) {
- swap(array, left, right);
- ++left;
- --right;
- }
- }
- return left;
- }
-
- // Driver Program
- public static void main(String[] args) {
-
- // For integer input
- Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12 ,2, 5 ,7 ,8 ,9, 2, 44, 111, 5};
-
- QuickSort quickSort = new QuickSort();
- // quickSort.sort(array);
-
- //Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111
- print(array);
-
- String[] stringArray = {"c", "a", "e", "b", "d"};
- quickSort.sort(stringArray);
-
- //Output => a b c d e
- print(stringArray);
- }
-}
-
diff --git a/Sorts/src/sort/RadixSort.java b/Sorts/src/sort/RadixSort.java
deleted file mode 100644
index 0d1e6966101a..000000000000
--- a/Sorts/src/sort/RadixSort.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package sort;
-
-import java.util.Arrays;
-
-class RadixSort {
-
-
- private static int getMax(int arr[], int n) {
- int mx = arr[0];
- for (int i = 1; i < n; i++)
- if (arr[i] > mx)
- mx = arr[i];
- return mx;
- }
-
- private static void countSort(int arr[], int n, int exp)
- {
- int output[] = new int[n];
- int i;
- int count[] = new int[10];
- Arrays.fill(count,0);
-
- for (i = 0; i < n; i++)
- count[ (arr[i]/exp)%10 ]++;
-
- for (i = 1; i < 10; i++)
- count[i] += count[i - 1];
-
- for (i = n - 1; i >= 0; i--)
- {
- output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
- count[ (arr[i]/exp)%10 ]--;
- }
-
- for (i = 0; i < n; i++)
- arr[i] = output[i];
- }
-
- private static void radixsort(int arr[], int n) {
-
- int m = getMax(arr, n);
-
-
- for (int exp = 1; m/exp > 0; exp *= 10)
- countSort(arr, n, exp);
- }
-
-
- static void print(int arr[], int n)
- {
- for (int i=0; i> T[] sort(T[] arr) {
- int n = arr.length;
- for (int i = 0; i < n - 1; i++) {
- // Initial index of min
- int min = i;
-
- for (int j = i +1 ; j < n; j++) {
- if (less(arr[j], arr[min])) {
- min = j;
- }
- }
-
- // Swapping if index of min is changed
- if (min != i) {
- swap(arr, i , min);
- }
- }
-
- return arr;
- }
-
- // Driver Program
- public static void main(String[] args) {
-
- Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
-
- SelectionSort selectionSort = new SelectionSort();
-
- Integer[] sorted = selectionSort.sort(arr);
-
- // Output => 1 4 6 9 12 23 54 78 231
- print(sorted);
-
- // String Input
- String[] strings = {"c", "a", "e", "b","d"};
- String[] sortedStrings = selectionSort.sort(strings);
-
- //Output => a b c d e
- print(sortedStrings);
- }
-}
diff --git a/Sorts/src/sort/ShellSort.java b/Sorts/src/sort/ShellSort.java
deleted file mode 100644
index bafd19b145d7..000000000000
--- a/Sorts/src/sort/ShellSort.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package sort;
-
-import static sort.SortUtils.*;
-
-
-/**
- * @author dpunosevac
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- * @see SortAlgorithm
- *
- */
-public class ShellSort implements SortAlgorithm {
-
- /**
- * This method implements Generic Shell Sort.
- * @param array The array to be sorted
- */
- @Override
- public > T[] sort(T[] array) {
- int N = array.length;
- int h = 1;
-
- while (h < N/3) {
- h = 3 * h + 1;
- }
-
- while (h >= 1) {
- for (int i = h; i < N; i++) {
- for (int j = i; j >= h && less(array[j], array[j-h]); j -= h) {
- swap(array, j, j - h);
- }
- }
-
- h /= 3;
- }
-
- return array;
- }
-
- public static void main(String[] args) {
- Integer[] toSort = {4, 23, 6, 78, 1, 54, 231, 9, 12};
-
- ShellSort sort = new ShellSort();
- Integer[] sorted = sort.sort(toSort);
-
- print(sorted);
-
- }
-}
diff --git a/Sorts/src/sort/SortUtils.java b/Sorts/src/sort/SortUtils.java
deleted file mode 100644
index 8766e9d0e4d7..000000000000
--- a/Sorts/src/sort/SortUtils.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package sort;
-
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * The class contains util methods
- *
- * @author Podshivalov Nikita (https://github.com/nikitap492)
- *
- **/
-final class SortUtils {
-
-
- /**
- * Helper method for swapping places in array
- * @param array The array which elements we want to swap
- * @param idx index of the first element
- * @param idy index of the second element
- */
- static