-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180273c
commit 6a3d379
Showing
46 changed files
with
2,937 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>AlgoJava</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
<filteredResources> | ||
<filter> | ||
<id>1711739015881</id> | ||
<name></name> | ||
<type>30</type> | ||
<matcher> | ||
<id>org.eclipse.core.resources.regexFilterMatcher</id> | ||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments> | ||
</matcher> | ||
</filter> | ||
</filteredResources> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/reneargento/algorithms-sedgewick-wayne/tree/master | ||
|
||
https://algs4.cs.princeton.edu/code/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.ali.algojava</groupId> | ||
<artifactId>AlgoJava</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>AlgoJava</name> | ||
<url>http://maven.apache.org</url> | ||
<repositories> | ||
<repository> | ||
<id>jitpack.io</id> | ||
<url>https://jitpack.io</url> | ||
</repository> | ||
</repositories> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.kevin-wayne</groupId> | ||
<artifactId>algs4</artifactId> | ||
<version>master-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.9.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.ali.algojava; | ||
|
||
import edu.princeton.cs.algs4.*; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App { | ||
public static void main(String[] args) { | ||
StdOut.print(Average.class); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/BinarySearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
import edu.princeton.cs.algs4.Counter; | ||
import edu.princeton.cs.algs4.In; | ||
import edu.princeton.cs.algs4.StdIn; | ||
import edu.princeton.cs.algs4.StdOut; | ||
import java.util.Arrays; | ||
|
||
// 1.2.9 | ||
|
||
public class BinarySearch { | ||
public static void main(String[] args) { | ||
Counter counter = new Counter("keys"); | ||
int[] whitelist = In.readInts(args[0]); | ||
Arrays.sort(whitelist); | ||
while (!StdIn.isEmpty()) { | ||
int key = StdIn.readInt(); | ||
if (rank(key, whitelist, counter) == -1) { | ||
StdOut.println(key); | ||
} | ||
} | ||
StdOut.println(counter); | ||
} | ||
|
||
public static int rank(int key, int[] a, Counter counter) { | ||
int lo = 0; | ||
int hi = a.length - 1; | ||
while (lo <= hi) { | ||
int mid = lo + (hi - lo) / 2; | ||
counter.increment(); | ||
if (key < a[mid]) { | ||
hi = mid - 1; | ||
} else if (key > a[mid]) { | ||
lo = mid + 1; | ||
} else { | ||
return mid; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/CircularRotationString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
import edu.princeton.cs.algs4.StdOut; | ||
|
||
// 1.2.6 | ||
|
||
public class CircularRotationString { | ||
public static void main(String[] args) { | ||
String s1 = "ACTGACG"; | ||
String s2 = "TGACGAC"; | ||
|
||
StdOut.print(isContained(s1, s2)); | ||
} | ||
|
||
private static boolean isContained(String s1, String s2) { | ||
return (s1.length() == s2.length()) && (s1 + s1).contains(s2); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/Counter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
public class Counter { | ||
private final String name; | ||
private int count; | ||
|
||
public Counter(String id) { | ||
name = id; | ||
} | ||
|
||
public void increment() { | ||
count++; | ||
} | ||
|
||
public int tally() { | ||
return count; | ||
} | ||
|
||
public String toString() { | ||
return count + " " + name; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/Flips.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
import edu.princeton.cs.algs4.StdOut; | ||
import edu.princeton.cs.algs4.StdRandom; | ||
|
||
public class Flips { | ||
|
||
public static void main(String[] args) { | ||
int T = Integer.parseInt(args[0]); | ||
|
||
Counter heads = new Counter("heads"); | ||
Counter tails = new Counter("tails"); | ||
|
||
for (int t = 0; t < T; t++) { | ||
if (StdRandom.bernoulli(0.5)) { | ||
heads.increment(); | ||
} else { | ||
tails.increment(); | ||
} | ||
} | ||
|
||
StdOut.println(heads); | ||
StdOut.println(tails); | ||
|
||
int d = heads.tally() - tails.tally(); | ||
StdOut.println("delta: " + Math.abs(d)); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/PrintClosestDistance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
import edu.princeton.cs.algs4.Point2D; | ||
import edu.princeton.cs.algs4.StdDraw; | ||
import edu.princeton.cs.algs4.StdOut; | ||
import edu.princeton.cs.algs4.StdRandom; | ||
import java.util.Arrays; | ||
|
||
// 1.2.1 | ||
|
||
public class PrintClosestDistance { | ||
|
||
public static void main(String[] args) { | ||
int N = Integer.parseInt(args[0]); | ||
|
||
Point2D[] points = generatePoints(N); | ||
|
||
printSmallestDistance(points); | ||
} | ||
|
||
private static Point2D[] generatePoints(int N) { | ||
StdDraw.setCanvasSize(1024, 512); | ||
StdDraw.setPenRadius(.015); | ||
StdDraw.setXscale(0, 1); | ||
StdDraw.setYscale(0, 1); | ||
|
||
Point2D[] points = new Point2D[N]; | ||
|
||
for (int i = 0; i < N; i++) { | ||
points[i] = new Point2D(StdRandom.uniform(0.0, 1.0), StdRandom.uniform(0.0, 1.0)); | ||
StdDraw.point(points[i].x(), points[i].y()); | ||
} | ||
|
||
Arrays.sort(points, Point2D.X_ORDER); | ||
|
||
return points; | ||
} | ||
|
||
private static void printSmallestDistance(Point2D[] points) { | ||
double result = Double.MAX_VALUE; | ||
Point2D point1 = null; | ||
Point2D point2 = null; | ||
for (int i = 0; i < points.length - 1; i++) { | ||
Point2D p1 = points[i]; | ||
Point2D p2 = points[i + 1]; | ||
double d = p1.distanceTo(p2); | ||
if (d < result) { | ||
result = d; | ||
point1 = p1; | ||
point2 = p2; | ||
} | ||
} | ||
|
||
StdOut.println(point1); | ||
StdOut.println(point2); | ||
StdOut.println(result); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/PrintOverlap2DInterval.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
import edu.princeton.cs.algs4.Interval1D; | ||
import edu.princeton.cs.algs4.Interval2D; | ||
import edu.princeton.cs.algs4.StdDraw; | ||
import edu.princeton.cs.algs4.StdOut; | ||
import edu.princeton.cs.algs4.StdRandom; | ||
import java.util.stream.IntStream; | ||
|
||
public class PrintOverlap2DInterval { | ||
public static void main(String[] args) { | ||
int N = Integer.parseInt(args[0]); | ||
double min = Double.parseDouble(args[1]); | ||
double max = Double.parseDouble(args[2]); | ||
|
||
Interval2D[] intervals = generateIntervals(N, min, max); | ||
|
||
drawIntervals(intervals, min, max); | ||
printOverlappingOrContaining(intervals); | ||
} | ||
|
||
private static Interval2D[] generateIntervals(int N, double min, double max) { | ||
return IntStream.range(0, N) | ||
.mapToObj(i -> new Interval2D(generate1DInterval(min, max), | ||
generate1DInterval(min, max))) | ||
.toArray(Interval2D[]::new); | ||
} | ||
|
||
private static Interval1D generate1DInterval(double min, double max) { | ||
double d1 = StdRandom.uniform(min, max); | ||
double d2 = StdRandom.uniform(min, max); | ||
if (d1 > d2) { | ||
return new Interval1D(d2, d1); | ||
} | ||
return new Interval1D(d1, d2); | ||
} | ||
|
||
private static void drawIntervals(Interval2D[] intervals, double min, | ||
double max) { | ||
StdDraw.setCanvasSize(1024, 512); | ||
StdDraw.setPenRadius(.015); | ||
StdDraw.setXscale(min, max); | ||
StdDraw.setYscale(min, max); | ||
for (Interval2D interval : intervals) { | ||
interval.draw(); | ||
} | ||
} | ||
|
||
private static void printOverlappingOrContaining(Interval2D[] intervals) { | ||
for (int i = 0; i < intervals.length - 1; i++) { | ||
if (intervals[i].intersects(intervals[i + 1])) { | ||
StdOut.print(intervals[i]); | ||
StdOut.print(" intersects with "); | ||
StdOut.println(intervals[i + 1]); | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section2/PrintOverlapInterval.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.ali.algojava.chapter1.section2; | ||
|
||
import edu.princeton.cs.algs4.Interval1D; | ||
import edu.princeton.cs.algs4.StdIn; | ||
import edu.princeton.cs.algs4.StdOut; | ||
import java.util.Arrays; | ||
|
||
// 1.2.2 | ||
|
||
public class PrintOverlapInterval { | ||
|
||
public static void main(String[] args) { | ||
int N = Integer.parseInt(args[0]); | ||
|
||
Interval1D[] intervals = new Interval1D[N]; | ||
|
||
for (int i = 0; i < N; i++) { | ||
StdOut.print("Enter min: "); | ||
double min = StdIn.readDouble(); | ||
|
||
StdOut.print("Enter max: "); | ||
double max = StdIn.readDouble(); | ||
|
||
intervals[i] = new Interval1D(min, max); | ||
} | ||
|
||
Arrays.sort(intervals, Interval1D.MIN_ENDPOINT_ORDER); | ||
|
||
for (int i = 0; i < intervals.length - 1; i++) { | ||
Interval1D interval1 = intervals[i]; | ||
Interval1D interval2 = intervals[i + 1]; | ||
|
||
if (interval1.intersects(interval2)) { | ||
StdOut.print(interval1); | ||
StdOut.print(" intersects with: "); | ||
StdOut.print(interval2); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.