-
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
3db71da
commit 6cb1986
Showing
6 changed files
with
101 additions
and
10 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
23 changes: 23 additions & 0 deletions
23
...-dependencies/spring-common/src/main/java/io/spring/common/algorithm/sort/InsertSort.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,23 @@ | ||
package io.spring.common.algorithm.sort; | ||
|
||
/** | ||
* @author xiaokexiang | ||
* @since 2021/11/12 | ||
* 插入排序:元素依次与左边的元素进行比较,如果大于就进行交换 | ||
* 时间复杂度: 1 + 2 + ... + (N - 1)次比较 与 1 + 2 + ... + (N - 1)次交换 -> N^2 | ||
*/ | ||
public class InsertSort implements SortExample<Integer> { | ||
|
||
@Override | ||
public void sort(Integer[] ts, boolean show) { | ||
for (int i = 1; i < ts.length; i++) { | ||
// 依次和左边的元素进行比较,再换位 | ||
for (int j = i; j > 0 && less(ts[j], ts[j - 1]); j--) { | ||
exchange(ts, j, j - 1); | ||
} | ||
} | ||
if (show) { | ||
show(ts); | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...dependencies/spring-common/src/main/java/io/spring/common/algorithm/sort/SortCompare.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,49 @@ | ||
package io.spring.common.algorithm.sort; | ||
|
||
import edu.princeton.cs.algs4.StdOut; | ||
import edu.princeton.cs.algs4.StdRandom; | ||
import edu.princeton.cs.algs4.Stopwatch; | ||
import org.springframework.util.StopWatch; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* @author xiaokexiang | ||
* @since 2021/11/15 | ||
*/ | ||
public class SortCompare { | ||
|
||
public static double time(String func, Integer[] a) { | ||
Stopwatch stopwatch = new Stopwatch(); | ||
if (func.equals("Insertion")) { | ||
new InsertSort().sort(a, false); | ||
} else if (func.equals("Selection")) { | ||
new SelectSort().sort(a, false); | ||
} | ||
return stopwatch.elapsedTime(); | ||
} | ||
|
||
|
||
public static double timeRandomInput(String alg, int N, int T) { | ||
double total = 0.0; | ||
Integer[] a = new Integer[N]; | ||
for (int i = 0; i < T; i++) { | ||
for (int j = 0; j < N; j++) { | ||
a[j] = StdRandom.uniform(100); | ||
} | ||
total += time(alg, a); | ||
} | ||
return total; | ||
} | ||
|
||
public static void main(String[] args) { | ||
String alg1 = "Insertion"; | ||
String alg2 = "Selection"; | ||
int t = 100; | ||
int n = 1000; | ||
double t1 = timeRandomInput(alg1, n, t); | ||
double t2 = timeRandomInput(alg2, n, t); | ||
StdOut.printf("For %d random Double \n %s is ", n, alg1); | ||
StdOut.printf("%.1f times faster than %s\n", t2 / t1, alg2); | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
...ot-dependencies/spring-common/src/main/java/io/spring/common/algorithm/sort/SortMain.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 io.spring.common.algorithm.sort; | ||
|
||
/** | ||
* @author xiaokexiang | ||
* @since 2021/11/12 | ||
*/ | ||
public class SortMain { | ||
|
||
public static void main(String[] args) { | ||
Integer[] arrays = new Integer[]{3, 1, 2, 5, 7}; | ||
|
||
SelectSort selectSort = new SelectSort(); | ||
selectSort.sort(arrays, true); | ||
System.out.println(); | ||
InsertSort insertSort = new InsertSort(); | ||
insertSort.sort(arrays, true); | ||
} | ||
} |