Skip to content
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
25 changes: 16 additions & 9 deletions app/src/main/java/algorithms/Sort.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package algorithms;
import java.util.PriorityQueue;

import java.util.Collections;
import java.util.Vector;
Expand Down Expand Up @@ -45,16 +46,22 @@ public static void DutchFlagPartition(Vector<Integer> v, int pivot_value) {
* @return A vector of the largest n elements in v
*/
public static Vector<Integer> MaxN(Vector<Integer> v, int n) {
// Use a PriorityQueue to store the largest n elements
java.util.PriorityQueue<Integer> maxHeap = new java.util.PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < v.size(); i++) {
maxHeap.offer(v.get(i));
// If the heap size exceeds n, remove the smallest element
if (maxHeap.size() > n) {
maxHeap.poll();
}
}
// Extract the elements from the heap
Vector<Integer> ret = new Vector<Integer>();
// Copy the vector so we don't modify the original
Vector<Integer> temp = new Vector<Integer>(v);

Collections.sort(temp);

for (int i = temp.size() - 1; i > temp.size() - n - 1; i--) {
ret.add(temp.get(i));
while (!maxHeap.isEmpty()) {
ret.add(maxHeap.poll());
}

// Reverse the vector to get the elements in ascending order
Collections.reverse(ret);
return ret;
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/datastructures/DsVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static Vector<Integer> searchVector(Vector<Integer> v, int n) {
public static Vector<Integer> sortVector(Vector<Integer> v) {
Vector<Integer> ret = new Vector<Integer>(v);

for (int i = 0; i < ret.size(); i++) {
for (int j = 0; j < ret.size() - 1; j++) {
for (int i = 0; i < ret.size() - 1; i++) {
for (int j = 0; j < ret.size() - i - 1; j++) {
if (ret.get(j) > ret.get(j + 1)) {
int temp = ret.get(j);
ret.set(j, ret.get(j + 1));
Expand Down Expand Up @@ -106,4 +106,4 @@ public static Vector<Integer> mergeVectors(Vector<Integer> v1,
}
return ret;
}
}
}
74 changes: 24 additions & 50 deletions app/src/main/java/run/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,59 @@ public class App {
public static void single() {
System.out.println("SingleForLoop");
System.out.println("-------------");
System.out.println(String.format("SumRange(10): %s", Single.sumRange(10)));
System.out.println(
String.format("MaxArray([1, 2, 3, 4, 5]): %s",
Single.maxArray(new int[] { 1, 2, 3, 4, 5 })));
System.out.println(
String.format("SumModulus(100, 3): %s", Single.sumModulus(100, 3)));
System.out.printf("SumRange(10): %s%n", Single.sumRange(10));
System.out.printf("MaxArray([1, 2, 3, 4, 5]): %s%n", Single.maxArray(new int[]{1, 2, 3, 4, 5}));
System.out.printf("SumModulus(100, 3): %s%n", Single.sumModulus(100, 3));
System.out.println();
}

public static void double_() {
System.out.println("DoubleForLoop");
System.out.println("-------------");
System.out.println(
String.format("SumSquare(10): %s", Double.sumSquare(10)));
System.out.println(
String.format("SumTriangle(10): %s", Double.sumTriangle(10)));
System.out.println(
String.format("CountPairs([1, 2, 3, 4, 5]): %s",
Double.countPairs(new int[] { 1, 2, 3, 4, 5, 2 })));
System.out.println(
String.format("CountDuplicates([1, 2, 3, 4, 5], [1, 3, 2, 4, 5]): %s",
Double.countDuplicates(new int[] { 1, 2, 3, 4, 5 },
new int[] { 1, 3, 2, 4, 5 })));
System.out.printf("SumSquare(10): %s%n", Double.sumSquare(10));
System.out.printf("SumTriangle(10): %s%n", Double.sumTriangle(10));
System.out.printf("CountPairs([1, 2, 3, 4, 5]): %s%n", Double.countPairs(new int[]{1, 2, 3, 4, 5, 2}));
System.out.printf("CountDuplicates([1, 2, 3, 4, 5], [1, 3, 2, 4, 5]): %s%n", Double.countDuplicates(new int[]{1, 2, 3, 4, 5}, new int[]{1, 3, 2, 4, 5}));
System.out.println();
}

public static void vector() {
Vector<Integer> inputVec = GenVector.generateVector(10, 10);
Vector<Integer> inputVec2 = GenVector.generateVector(10, 10);
String inputVecStr = inputVec.toString();

System.out.println("Vector");
System.out.println("------");
System.out.println(
String.format("ModifyVector(%s): %s", inputVec.toString(),
DsVector.modifyVector(inputVec).toString()));
System.out.println(String.format("SearchVector(%s, 5): %s",
inputVec.toString(),
DsVector.searchVector(inputVec, 5)));
System.out.println(String.format("SortVector(%s): %s", inputVec.toString(),
DsVector.sortVector(inputVec).toString()));
System.out.println(
String.format("ReverseVector(%s): %s", inputVec.toString(),
DsVector.reverseVector(inputVec).toString()));
System.out.println(
String.format("RotateVector(%s, 3): %s", inputVec.toString(),
DsVector.rotateVector(inputVec, 3).toString()));
System.out.println(String.format(
"MergeVectors(%s, %s): %s", inputVec.toString(), inputVec2.toString(),
DsVector.mergeVectors(inputVec, inputVec2).toString()));

System.out.printf("ModifyVector(%s): %s%n", inputVecStr, DsVector.modifyVector(inputVec).toString());
System.out.printf("SearchVector(%s, 5): %s%n", inputVecStr, DsVector.searchVector(inputVec, 5));
System.out.printf("SortVector(%s): %s%n", inputVecStr, DsVector.sortVector(inputVec).toString());
System.out.printf("ReverseVector(%s): %s%n", inputVecStr, DsVector.reverseVector(inputVec).toString());
System.out.printf("RotateVector(%s, 3): %s%n", inputVecStr, DsVector.rotateVector(inputVec, 3).toString());
System.out.printf("MergeVectors(%s, %s): %s%n", inputVecStr, inputVec2.toString(), DsVector.mergeVectors(inputVec, inputVec2).toString());
System.out.println();
}

public static void primes() {
System.out.println("Primes");
System.out.println("------");
System.out.println(String.format("IsPrime(10): %s", Primes.IsPrime(10)));
System.out.println(
String.format("SumPrimes(10): %s", Primes.SumPrimes(10)));
System.out.println(String.format("PrimeFactors(10): %s",
Primes.PrimeFactors(10).toString()));
System.out.printf("IsPrime(10): %s%n", Primes.IsPrime(10));
System.out.printf("SumPrimes(10): %s%n", Primes.SumPrimes(10));
System.out.printf("PrimeFactors(10): %s%n", Primes.PrimeFactors(10).toString());
System.out.println();
}

public static void sort() {
Vector<Integer> initialVec = GenVector.generateVector(20, 10);
String initialVecStr = initialVec.toString();
System.out.println("Sort");
System.out.println("------");
Vector<Integer> inputVec0 = new Vector<Integer>(initialVec);
Vector<Integer> inputVec0 = new Vector<>(initialVec);
Sort.SortVector(inputVec0);
System.out.println(String.format(
"SortVector(%s): %s", initialVec.toString(), inputVec0.toString()));
Vector<Integer> inputVec1 = new Vector<Integer>(initialVec);
System.out.printf("SortVector(%s): %s%n", initialVecStr, inputVec0.toString());
Vector<Integer> inputVec1 = new Vector<>(initialVec);
Sort.DutchFlagPartition(inputVec1, 5);
System.out.println(String.format("DutchFlagPartition(%s, 5): %s",
inputVec1.toString(),
inputVec1.toString()));
System.out.println(String.format("MaxN(%s, 5): %s", initialVec.toString(),
Sort.MaxN(initialVec, 5).toString()));
System.out.printf("DutchFlagPartition(%s, 5): %s%n", initialVecStr, inputVec1.toString());
System.out.printf("MaxN(%s, 5): %s%n", initialVecStr, Sort.MaxN(initialVec, 5).toString());
System.out.println();
}

Expand All @@ -101,4 +75,4 @@ public static void main(String[] args) {
primes();
sort();
}
}
}
2 changes: 1 addition & 1 deletion app/src/test/java/control/SingleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public void testSumModulus() {
assertEquals(18, Single.sumModulus(10, 3));
assertEquals(12, Single.sumModulus(10, 4));
}
}
}
Empty file modified gradlew
100755 → 100644
Empty file.