Skip to content

Commit 7e15539

Browse files
author
tianqing.liang
committed
Dart实现 线性搜索,插入排序,选择排序
1 parent ad7f99f commit 7e15539

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

01Linear-Search/LinearSearch.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
void main(){
2+
var data = {24, 18, 12, 9, 16, 66, 32, 4};
3+
if(LinearSearch(data.toList(), 4)){
4+
print("搜索到数字");
5+
}else{
6+
print("没搜索到数字");
7+
}
8+
}
9+
bool search(List arr,Object target){
10+
return arr.contains(target);
11+
}
12+
13+
bool LinearSearch(List arr,Object target){
14+
for(int i =0;i<arr.length;i++){
15+
if(arr[i] == target){
16+
return true;
17+
}
18+
}
19+
return false;
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 选择排序
3+
*/
4+
void main(){
5+
6+
var list = [2, 4, 6, 3, 1, 5];
7+
var list2 = ["2","4","6","3","1","5"];
8+
var list3 = [new Student("xiaoming", 90),new Student("xiaoli", 70),new Student("xiaowang", 80),new Student("skyl", 100),];
9+
selectionSort(list2);
10+
selectionSort(list3);
11+
list3.forEach((element) {print(element.name);print(element.score);});
12+
}
13+
void selectionSort(List arr){
14+
print("---------------------选择排序");
15+
for(int i = 0; i < arr.length; i ++){
16+
int minIndex = i;
17+
for(int j = i; j < arr.length; j ++){
18+
if(arr[j].compareTo(arr[minIndex]) > 0)
19+
minIndex = j;
20+
}
21+
swap(arr, i, minIndex);
22+
}
23+
arr.forEach((element) {print(element);});
24+
}
25+
26+
void swap(arr, int i, int j) {
27+
var t = arr[i];
28+
arr[i] = arr[j];
29+
arr[j] = t;
30+
}
31+
32+
class Student {
33+
34+
String? name;
35+
int? score;
36+
37+
Student(this.name,this.score);
38+
39+
@override
40+
int compareTo(other) {
41+
return other.score - this.score;
42+
}
43+
}
File renamed without changes.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
#### 介绍
44
使用Dart语言编写一套算法与数据结构
5-
1. 插入排序
5+
1. 线性搜索
6+
2. 选择排序
7+
3. 插入排序
68

0 commit comments

Comments
 (0)