Skip to content

Commit f8d0e9c

Browse files
author
tianqing.liang
committed
归并排序优化,快速排序
1 parent 1814ea6 commit f8d0e9c

File tree

6 files changed

+214
-4
lines changed

6 files changed

+214
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:math';
2+
3+
class ArrayGenerator{
4+
5+
static List generateOrderedArray(int n){
6+
List arr = List.filled(n, true);
7+
for(int i = 0;i<n;i++){
8+
arr[i] =i;
9+
}
10+
return arr;
11+
}
12+
13+
static List generateRandomArray(int n,int bound){
14+
List arr = List.filled(n, true);
15+
Random random = new Random();
16+
for(int i = 0; i < n; i ++){
17+
arr[i] = random.nextInt(bound);
18+
}
19+
return arr;
20+
}
21+
}

07MergeSort-More/MergeSort.dart

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import 'ArrayGenerator.dart';
2+
import 'dart:math';
3+
4+
/**
5+
* 自底向上归并排序
6+
*/
7+
class MergeSort{
8+
9+
static sort(List arr){
10+
_sortContent(arr,0,arr.length-1);
11+
}
12+
13+
static sortBU(List arr){
14+
List temp = List.filled(arr.length, true);
15+
List.copyRange(temp,0,arr);
16+
int n = arr.length;
17+
// 遍历合并的区间长度
18+
for(int sz = 1; sz < n; sz += sz){
19+
// 遍历合并的两个区间的起始位置 i
20+
// 合并 [i, i + sz - 1] 和 [i + sz, Math.min(i + sz + sz - 1, n - 1)]
21+
for(int i = 0; i + sz < n; i += sz + sz)
22+
if(arr[i + sz - 1].compareTo(arr[i + sz]) > 0)
23+
_merge2(arr, i, i + sz - 1, [i + sz + sz - 1, n - 1].reduce(min), temp);
24+
}
25+
}
26+
27+
static sort2(List arr){
28+
List temp = List.filled(arr.length, true);
29+
List.copyRange(temp,0,arr);
30+
_sortContent2(arr, 0, arr.length - 1, temp);
31+
}
32+
33+
static _sortContent2(List arr,int left,int right,List temp){
34+
if (left >= right)
35+
return;
36+
37+
int mid = ((right - left) / 2).toInt() + left;
38+
_sortContent2(arr, left, mid, temp);
39+
_sortContent2(arr, mid + 1, right, temp);
40+
41+
if(arr[mid].compareTo(arr[mid + 1]) > 0)
42+
_merge2(arr, left, mid, right, temp);
43+
}
44+
static _merge2(List arr,int left,int mid,int right,List temp){
45+
List.copyRange(temp, left, arr,left,right+1);
46+
int i = left, j = mid + 1;
47+
48+
// 每轮循环为 arr[k] 赋值
49+
for(int k = left; k <= right; k ++){
50+
if(i > mid){
51+
arr[k] = temp[j];
52+
j ++;
53+
}
54+
else if(j > right){
55+
arr[k] = temp[i];
56+
i ++;
57+
}
58+
else if(temp[i].compareTo(temp[j]) <= 0){
59+
arr[k] = temp[i];
60+
i ++;
61+
}
62+
else{
63+
arr[k] = temp[j];
64+
j ++;
65+
}
66+
}
67+
}
68+
69+
70+
71+
static _sortContent (List arr,int left,int right){
72+
/**
73+
* 当数组过小时,可以使用插入排序
74+
*/
75+
if(left >= right){
76+
return;
77+
}
78+
int mid = ((right-left)/2).toInt() +left;
79+
_sortContent(arr, left, mid);
80+
_sortContent(arr, mid+1, right);
81+
//提前进行判断,少一步merge
82+
if(arr[mid].compareTo(arr[mid+1])>0){
83+
_merge(arr,left,mid,right);
84+
}
85+
}
86+
87+
static _merge(List arr,int l,int mid,int right){
88+
List temp = List.filled(right+1-l, true);
89+
List.copyRange(temp, 0, arr,l,right+1);
90+
int i = l, j = mid + 1;
91+
92+
// 每轮循环为 arr[k] 赋值
93+
for(int k = l; k <= right; k ++){
94+
if(i > mid){
95+
arr[k] = temp[j - l]; j ++;
96+
}
97+
else if(j > right){
98+
arr[k] = temp[i - l]; i ++;
99+
}
100+
else if(temp[i - l].compareTo(temp[j - l]) <= 0){
101+
arr[k] = temp[i - l]; i ++;
102+
}
103+
else{
104+
arr[k] = temp[j - l]; j ++;
105+
}
106+
}
107+
}
108+
}
109+
void main(){
110+
int n = 100;
111+
// List arr = ArrayGenerator.generateRandomArray(n, n);
112+
// MergeSort.sort(arr);
113+
// print(arr);
114+
List arr2 = ArrayGenerator.generateRandomArray(n, n);
115+
print(arr2);
116+
MergeSort.sortBU(arr2);
117+
print(arr2);
118+
}

07MergeSort/MergeSort.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ class MergeSort{
1010
}
1111

1212
static _sortContent (List arr,int left,int right){
13+
/**
14+
* 当数组过小时,可以使用插入排序
15+
*/
1316
if(left >= right){
1417
return;
1518
}
1619
int mid = ((right-left)/2).toInt() +left;
1720
_sortContent(arr, left, mid);
1821
_sortContent(arr, mid+1, right);
19-
_merge(arr,left,mid,right);
22+
//提前进行判断,少一步merge
23+
if(arr[mid].compareTo(arr[mid+1])>0){
24+
_merge(arr,left,mid,right);
25+
}
2026
}
2127

2228
static _merge(List arr,int l,int mid,int right){
@@ -43,7 +49,7 @@ class MergeSort{
4349
}
4450
}
4551
void main(){
46-
int n = 100;
52+
int n = 20;
4753
List arr = ArrayGenerator.generateRandomArray(n, n);
4854
MergeSort.sort(arr);
4955
print(arr);

08-QuickSort/ArrayGenerator.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:math';
2+
3+
class ArrayGenerator{
4+
5+
static List generateOrderedArray(int n){
6+
List arr = List.filled(n, true);
7+
for(int i = 0;i<n;i++){
8+
arr[i] =i;
9+
}
10+
return arr;
11+
}
12+
13+
static List generateRandomArray(int n,int bound){
14+
List arr = List.filled(n, true);
15+
Random random = new Random();
16+
for(int i = 0; i < n; i ++){
17+
arr[i] = random.nextInt(bound);
18+
}
19+
return arr;
20+
}
21+
}

08-QuickSort/QuickSort.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'ArrayGenerator.dart';
2+
class QuickSort{
3+
_QuickSort(){
4+
}
5+
static sort(List arr){
6+
_sortDetail(arr,0,arr.length-1);
7+
}
8+
9+
static _sortDetail(List arr,int left,int right){
10+
if(left >= right){
11+
return;
12+
}
13+
int p = _partition(arr, left, right);
14+
_sortDetail(arr, left, p-1);
15+
_sortDetail(arr, p+1, right);
16+
}
17+
18+
static int _partition(List arr,int left,int right){
19+
int j =left;
20+
for(var i = left+1;i<=right;i++){
21+
if(arr[i].compareTo(arr[left])<0){
22+
j++;
23+
_swap(arr,i,j);
24+
}
25+
}
26+
_swap(arr, left, j);
27+
return j;
28+
}
29+
static _swap(List arr,int i,int j){
30+
var t =arr[i];
31+
arr[i] = arr[j];
32+
arr[j] = t;
33+
}
34+
}
35+
void main(){
36+
int n =10;
37+
List arr2 = ArrayGenerator.generateRandomArray(n, n);
38+
print(arr2);
39+
QuickSort.sort(arr2);
40+
print(arr2);
41+
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# AlgorithmAndDataArchitecture
22

33
#### 介绍
4-
使用Dart语言编写一套算法与数据结构
4+
使用Dart语言编写 慕课网算法与数据结构[https://class.imooc.com/sale/datastructure]
55
1. 线性搜索
66
2. 选择排序
77
3. 插入排序
88
4. 栈,队列,循环队列
99
5. 链表,链表实现栈,链表实现队列
1010
6. 递归
11-
7. 归并排序
11+
7. 归并排序,归并排序排序优化,自底向上排序
12+
8. 快速排序
1213

14+
#### SDK版本
15+
1. 版本:2.12.3

0 commit comments

Comments
 (0)