mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Restructured the repo
This commit is contained in:
@ -1,7 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<classpath>
|
|
||||||
<classpathentry excluding="data_structures/" kind="src" path=""/>
|
|
||||||
<classpathentry kind="src" path="data_structures"/>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
|
||||||
<classpathentry kind="output" path="bin"/>
|
|
||||||
</classpath>
|
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -1 +1,5 @@
|
|||||||
/bin/
|
/bin/
|
||||||
|
.idea/*
|
||||||
|
.classpath*
|
||||||
|
.project*
|
||||||
|
*.iml
|
||||||
|
17
.project
17
.project
@ -1,17 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<projectDescription>
|
|
||||||
<name>Java</name>
|
|
||||||
<comment></comment>
|
|
||||||
<projects>
|
|
||||||
</projects>
|
|
||||||
<buildSpec>
|
|
||||||
<buildCommand>
|
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
|
||||||
<arguments>
|
|
||||||
</arguments>
|
|
||||||
</buildCommand>
|
|
||||||
</buildSpec>
|
|
||||||
<natures>
|
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
|
||||||
</natures>
|
|
||||||
</projectDescription>
|
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The common interface of most searching algorithms
|
* The common interface of most searching algorithms
|
@ -1,4 +1,4 @@
|
|||||||
package search;
|
package Searches;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
@ -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> skyline) {
|
|
||||||
Iterator<Skyline> 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<Skyline> findSkyline(int start, int end) {
|
|
||||||
if(start == end) {
|
|
||||||
ArrayList<Skyline> 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<Skyline> sky1 = this.findSkyline(start, mid);
|
|
||||||
ArrayList<Skyline> sky2 = this.findSkyline(mid + 1, end);
|
|
||||||
|
|
||||||
return this.mergeSkyline(sky1, sky2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skyline> sky2) {
|
|
||||||
int currentH1 = 0, currentH2 = 0;
|
|
||||||
ArrayList<Skyline> 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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.less;
|
import static Sorts.SortUtils.less;
|
||||||
import static sort.SortUtils.print;
|
import static Sorts.SortUtils.print;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
@ -1,9 +1,7 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -19,7 +17,7 @@ public class BogoSort implements SortAlgorithm {
|
|||||||
|
|
||||||
private static <T extends Comparable<T>> boolean isSorted(T array[]){
|
private static <T extends Comparable<T>> boolean isSorted(T array[]){
|
||||||
for(int i = 0; i<array.length - 1; i++){
|
for(int i = 0; i<array.length - 1; i++){
|
||||||
if(less(array[i + 1], array[i])) return false;
|
if(SortUtils.less(array[i + 1], array[i])) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -30,7 +28,7 @@ public class BogoSort implements SortAlgorithm {
|
|||||||
|
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
int randomIndex = i + random.nextInt(length - i);
|
int randomIndex = i + random.nextInt(length - i);
|
||||||
swap(array, randomIndex, i);
|
SortUtils.swap(array, randomIndex, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,11 +47,11 @@ public class BogoSort implements SortAlgorithm {
|
|||||||
BogoSort bogoSort = new BogoSort();
|
BogoSort bogoSort = new BogoSort();
|
||||||
|
|
||||||
// print a sorted array
|
// print a sorted array
|
||||||
print(bogoSort.sort(integers));
|
SortUtils.print(bogoSort.sort(integers));
|
||||||
|
|
||||||
// String Input
|
// String Input
|
||||||
String[] strings = {"c", "a", "e", "b","d"};
|
String[] strings = {"c", "a", "e", "b","d"};
|
||||||
|
|
||||||
print(bogoSort.sort(strings));
|
SortUtils.print(bogoSort.sort(strings));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
@ -1,6 +1,4 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -29,8 +27,8 @@ class CocktailShakerSort implements SortAlgorithm {
|
|||||||
// front
|
// front
|
||||||
swappedRight = 0;
|
swappedRight = 0;
|
||||||
for (int i = left; i < right; i++) {
|
for (int i = left; i < right; i++) {
|
||||||
if (less(array[i + 1], array[i])) {
|
if (SortUtils.less(array[i + 1], array[i])) {
|
||||||
swap(array, i, i + 1);
|
SortUtils.swap(array, i, i + 1);
|
||||||
swappedRight = i;
|
swappedRight = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,8 +36,8 @@ class CocktailShakerSort implements SortAlgorithm {
|
|||||||
right = swappedRight;
|
right = swappedRight;
|
||||||
swappedLeft = length - 1;
|
swappedLeft = length - 1;
|
||||||
for (int j = right; j > left; j--) {
|
for (int j = right; j > left; j--) {
|
||||||
if (less(array[j], array[j - 1])) {
|
if (SortUtils.less(array[j], array[j - 1])) {
|
||||||
swap(array, j - 1, j);
|
SortUtils.swap(array, j - 1, j);
|
||||||
swappedLeft = j;
|
swappedLeft = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,11 +54,11 @@ class CocktailShakerSort implements SortAlgorithm {
|
|||||||
CocktailShakerSort shakerSort = new CocktailShakerSort();
|
CocktailShakerSort shakerSort = new CocktailShakerSort();
|
||||||
|
|
||||||
// Output => 1 4 6 9 12 23 54 78 231
|
// Output => 1 4 6 9 12 23 54 78 231
|
||||||
print(shakerSort.sort(integers));
|
SortUtils.print(shakerSort.sort(integers));
|
||||||
|
|
||||||
// String Input
|
// String Input
|
||||||
String[] strings = { "c", "a", "e", "b", "d" };
|
String[] strings = { "c", "a", "e", "b", "d" };
|
||||||
print(shakerSort.sort(strings));
|
SortUtils.print(shakerSort.sort(strings));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
@ -1,4 +1,4 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
@ -6,7 +6,7 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.toList;
|
||||||
import static java.util.stream.Collectors.toMap;
|
import static java.util.stream.Collectors.toMap;
|
||||||
import static sort.SortUtils.print;
|
import static Sorts.SortUtils.print;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
@ -1,7 +1,7 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.less;
|
import static Sorts.SortUtils.less;
|
||||||
import static sort.SortUtils.print;
|
import static Sorts.SortUtils.print;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of gnome sort
|
* Implementation of gnome sort
|
@ -1,10 +1,10 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Heap Sort Algorithm
|
* Heap Sort Algorithm
|
@ -1,7 +1,7 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.less;
|
import static Sorts.SortUtils.less;
|
||||||
import static sort.SortUtils.print;
|
import static Sorts.SortUtils.print;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.print;
|
import static Sorts.SortUtils.print;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method implements the Generic Merge Sort
|
* This method implements the Generic Merge Sort
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of gnome sort
|
* Implementation of gnome sort
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
@ -1,4 +1,4 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -1,6 +1,4 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -27,14 +25,14 @@ public class SelectionSort implements SortAlgorithm {
|
|||||||
int min = i;
|
int min = i;
|
||||||
|
|
||||||
for (int j = i +1 ; j < n; j++) {
|
for (int j = i +1 ; j < n; j++) {
|
||||||
if (less(arr[j], arr[min])) {
|
if (SortUtils.less(arr[j], arr[min])) {
|
||||||
min = j;
|
min = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swapping if index of min is changed
|
// Swapping if index of min is changed
|
||||||
if (min != i) {
|
if (min != i) {
|
||||||
swap(arr, i , min);
|
SortUtils.swap(arr, i , min);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,13 +49,13 @@ public class SelectionSort implements SortAlgorithm {
|
|||||||
Integer[] sorted = selectionSort.sort(arr);
|
Integer[] sorted = selectionSort.sort(arr);
|
||||||
|
|
||||||
// Output => 1 4 6 9 12 23 54 78 231
|
// Output => 1 4 6 9 12 23 54 78 231
|
||||||
print(sorted);
|
SortUtils.print(sorted);
|
||||||
|
|
||||||
// String Input
|
// String Input
|
||||||
String[] strings = {"c", "a", "e", "b","d"};
|
String[] strings = {"c", "a", "e", "b","d"};
|
||||||
String[] sortedStrings = selectionSort.sort(strings);
|
String[] sortedStrings = selectionSort.sort(strings);
|
||||||
|
|
||||||
//Output => a b c d e
|
//Output => a b c d e
|
||||||
print(sortedStrings);
|
SortUtils.print(sortedStrings);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import static sort.SortUtils.*;
|
import static Sorts.SortUtils.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
@ -1,4 +1,4 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -1,4 +1,4 @@
|
|||||||
package sort;
|
package Sorts;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
Reference in New Issue
Block a user