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
32 changes: 8 additions & 24 deletions app/src/main/java/datastructures/DsVector.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package datastructures;
import java.util.Collections;

import java.util.Vector;

Expand Down Expand Up @@ -40,16 +41,7 @@ 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++) {
if (ret.get(j) > ret.get(j + 1)) {
int temp = ret.get(j);
ret.set(j, ret.get(j + 1));
ret.set(j + 1, temp);
}
}
}
Collections.sort(ret);
return ret;
}

Expand All @@ -60,11 +52,8 @@ public static Vector<Integer> sortVector(Vector<Integer> v) {
* @return the reversed vector
*/
public static Vector<Integer> reverseVector(Vector<Integer> v) {
Vector<Integer> ret = new Vector<Integer>();

for (int i = v.size() - 1; i >= 0; i--) {
ret.add(v.get(i));
}
Vector<Integer> ret = new Vector<Integer>(v);
Collections.reverse(ret);
return ret;
}

Expand Down Expand Up @@ -96,14 +85,9 @@ public static Vector<Integer> rotateVector(Vector<Integer> v, int n) {
*/
public static Vector<Integer> mergeVectors(Vector<Integer> v1,
Vector<Integer> v2) {
Vector<Integer> ret = new Vector<Integer>();

for (int i = 0; i < v1.size(); i++) {
ret.add(v1.get(i));
}
for (int i = 0; i < v2.size(); i++) {
ret.add(v2.get(i));
}
Vector<Integer> ret = new Vector<Integer>(v1.size() + v2.size());
ret.addAll(v1);
ret.addAll(v2);
return ret;
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/generator/GenVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static Vector<Integer> generateVector(int n, int m) {
ret.add(rand.nextInt(m));
}

// Convert back to vector if the return type must be Vector.
// Convert back to vector if the return type must be Vector. If not, returning the ArrayList directly would be more efficient.
return new Vector<>(ret);
}
}
}
78 changes: 33 additions & 45 deletions app/src/main/java/run/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@ 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();
}

Expand All @@ -44,35 +38,30 @@ public static void vector() {

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",
System.out.printf("ModifyVector(%s): %s%n", inputVec.toString(),
DsVector.modifyVector(inputVec).toString());
System.out.printf("SearchVector(%s, 5): %s%n",
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()));
DsVector.searchVector(inputVec, 5));
System.out.printf("SortVector(%s): %s%n", inputVec.toString(),
DsVector.sortVector(inputVec).toString());
System.out.printf("ReverseVector(%s): %s%n", inputVec.toString(),
DsVector.reverseVector(inputVec).toString());
System.out.printf("RotateVector(%s, 3): %s%n", inputVec.toString(),
DsVector.rotateVector(inputVec, 3).toString());
System.out.printf("MergeVectors(%s, %s): %s%n", inputVec.toString(), 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();
}

Expand All @@ -82,15 +71,14 @@ public static void sort() {
System.out.println("------");
Vector<Integer> inputVec0 = new Vector<Integer>(initialVec);
Sort.SortVector(inputVec0);
System.out.println(String.format(
"SortVector(%s): %s", initialVec.toString(), inputVec0.toString()));
System.out.printf("SortVector(%s): %s%n", initialVec.toString(), inputVec0.toString());
Vector<Integer> inputVec1 = new Vector<Integer>(initialVec);
Sort.DutchFlagPartition(inputVec1, 5);
System.out.println(String.format("DutchFlagPartition(%s, 5): %s",
System.out.printf("DutchFlagPartition(%s, 5): %s%n",
inputVec1.toString(),
inputVec1.toString()));
System.out.println(String.format("MaxN(%s, 5): %s", initialVec.toString(),
Sort.MaxN(initialVec, 5).toString()));
inputVec1.toString());
System.out.printf("MaxN(%s, 5): %s%n", initialVec.toString(),
Sort.MaxN(initialVec, 5).toString());
System.out.println();
}

Expand All @@ -101,4 +89,4 @@ public static void main(String[] args) {
primes();
sort();
}
}
}