Skip to content

Commit ad7f99f

Browse files
author
tianqing.liang
committed
Dart实现插入排序
1 parent d5c1a5b commit ad7f99f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1284
-1255
lines changed

.gitignore

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
# Compiled class file
2-
*.class
3-
4-
# Log file
5-
*.log
6-
7-
# BlueJ files
8-
*.ctxt
9-
10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
12-
.idea/*
13-
14-
# Package Files #
15-
*.jar
16-
*.war
17-
*.nar
18-
*.ear
19-
*.zip
20-
*.tar.gz
21-
*.rar
22-
23-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
24-
hs_err_pid*
1+
# See https://www.dartlang.org/tools/private-files.html
2+
3+
# Files and directories created by pub
4+
.packages
5+
.pub/
6+
build/
7+
# If you're building an application, you may want to check-in your pubspec.lock
8+
pubspec.lock
9+
10+
# Directory created by dartdoc
11+
# If you don't generate documentation locally you can remove this line.
12+
doc/api/
13+
.idea/

02Selection-Sort/InsertionSort.dart

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* 插入排序
3+
* 1、Dart自带插入排序算法
4+
* 2、手动实现插入排序算法
5+
* 3、 源码部分
6+
* dart数组天然支持泛型,
7+
* compare 是实现比对接口
8+
*/
9+
void main() {
10+
var list = [2, 4, 6, 3, 1, 5];
11+
var list2 = ["2","4","6","3","1","5"];
12+
sort(list);
13+
insertionSort(list);
14+
sort(list2);
15+
insertionSort(list2);
16+
}
17+
18+
//Dart语言原生方法
19+
void sort(List arr) {
20+
print("Dart自带排序算法----------------------");
21+
arr.sort();
22+
arr.forEach((element) {
23+
print(element);
24+
});
25+
26+
}
27+
28+
void insertionSort(List arr) {
29+
print("手动实现插入排序算法----------------------");
30+
for (int i = 0; i < arr.length; i++) {
31+
for (int j = i; j - 1 >= 0; j--) {
32+
if (Comparable.compare(arr[j] ,arr[j - 1]) < 0) {
33+
swap(arr, j - 1, j);
34+
}
35+
}
36+
}
37+
arr.forEach((element) {
38+
print(element);
39+
});
40+
41+
}
42+
43+
void swap(arr, int i, int j) {
44+
var t = arr[i];
45+
arr[i] = arr[j];
46+
arr[j] = t;
47+
}
48+
49+
// Dart 源码使用的是阀值,32
50+
// 当在32以内使用插入排序,当在32以外使用双向快排
51+
// static const int _INSERTION_SORT_THRESHOLD = 32;
52+
// /**
53+
// * Sorts all elements of the given list [:a:] according to the given
54+
// * [:compare:] function.
55+
// *
56+
// * The [:compare:] function takes two arguments [:x:] and [:y:] and returns
57+
// * -1 if [:x < y:],
58+
// * 0 if [:x == y:], and
59+
// * 1 if [:x > y:].
60+
// *
61+
// * The function's behavior must be consistent. It must not return different
62+
// * results for the same values.
63+
// */
64+
// static void sort<E>(List<E> a, int compare(E a, E b)) {
65+
// _doSort(a, 0, a.length - 1, compare);
66+
// }
67+
//
68+
// /**
69+
// * Sorts all elements in the range [:from:] (inclusive) to [:to:] (exclusive)
70+
// * of the given list [:a:].
71+
// *
72+
// * If the given range is invalid an "OutOfRange" error is raised.
73+
// * TODO(floitsch): do we want an error?
74+
// *
75+
// * See [:sort:] for requirements of the [:compare:] function.
76+
// */
77+
// static void sortRange<E>(List<E> a, int from, int to, int compare(E a, E b)) {
78+
// if ((from < 0) || (to > a.length) || (to < from)) {
79+
// throw "OutOfRange";
80+
// }
81+
// _doSort(a, from, to - 1, compare);
82+
// }
83+
//
84+
// /**
85+
// * Sorts the list in the interval [:left:] to [:right:] (both inclusive).
86+
// */
87+
// static void _doSort<E>(
88+
// List<E> a, int left, int right, int compare(E a, E b)) {
89+
// if ((right - left) <= _INSERTION_SORT_THRESHOLD) {
90+
// _insertionSort(a, left, right, compare);
91+
// } else {
92+
// _dualPivotQuicksort(a, left, right, compare);
93+
// }
94+
// }
95+
//
96+
//
97+
98+

Dart-Basic.iml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$" />
6+
<orderEntry type="inheritedJdk" />
7+
<orderEntry type="sourceFolder" forTests="false" />
8+
<orderEntry type="library" name="Dart SDK" level="project" />
9+
</component>
10+
</module>

LICENSE

-201
This file was deleted.

README.en.md

-36
This file was deleted.

0 commit comments

Comments
 (0)