Skip to content

Commit

Permalink
算法入门-排序-插入排序
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokexiang committed Nov 15, 2021
1 parent 3db71da commit 6cb1986
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
6 changes: 6 additions & 0 deletions spring-boot-dependencies/spring-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/edu.princeton.cs/algs4 -->
<dependency>
<groupId>edu.princeton.cs</groupId>
<artifactId>algs4</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
public class SelectSort implements SortExample<Integer> {

@Override
public void sort(Integer[] ts) {

public void sort(Integer[] ts, boolean show) {
for (int i = 0; i < ts.length; i++) {
int min = i;
// 找出最小的元素
Expand All @@ -22,12 +21,8 @@ public void sort(Integer[] ts) {
}
exchange(ts, i, min);
}
show(ts);
}

public static void main(String[] args) {
Integer[] arrays = new Integer[]{3, 1, 2, 5, 7};
SelectSort selectSort = new SelectSort();
selectSort.sort(arrays);
if (show) {
show(ts);
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public interface SortExample<T extends Comparable<T>> {

void sort(T[] ts);
void sort(T[] ts, boolean show);

/**
* 元素比较
Expand Down
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);
}
}

0 comments on commit 6cb1986

Please sign in to comment.