add chapter_sorting by dart (#366)

* add chapter_sorting by dart

* fixed style of dart and bug mentioned before

* alter min value of dart

* Update radix_sort.dart

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
Leo.Cai 2023-02-19 01:03:42 +08:00 committed by GitHub
parent 53f45984f9
commit d249c8802e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 349 additions and 21 deletions

View file

@ -9,13 +9,10 @@
/* 冒泡排序 */ /* 冒泡排序 */
void bubbleSort(int nums[], int size) { void bubbleSort(int nums[], int size) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1 // 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = 0; i < size - 1; i++) for (int i = 0; i < size - 1; i++) {
{
// 内循环:冒泡操作 // 内循环:冒泡操作
for (int j = 0; j < size - 1 - i; j++) for (int j = 0; j < size - 1 - i; j++) {
{ if (nums[j] > nums[j + 1]) {
if (nums[j] > nums[j + 1])
{
int temp = nums[j]; int temp = nums[j];
nums[j] = nums[j + 1]; nums[j] = nums[j + 1];
nums[j + 1] = temp; nums[j + 1] = temp;
@ -27,14 +24,11 @@ void bubbleSort(int nums[], int size) {
/* 冒泡排序(标志优化)*/ /* 冒泡排序(标志优化)*/
void bubbleSortWithFlag(int nums[], int size) { void bubbleSortWithFlag(int nums[], int size) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1 // 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = 0; i < size - 1; i++) for (int i = 0; i < size - 1; i++) {
{
bool flag = false; bool flag = false;
// 内循环:冒泡操作 // 内循环:冒泡操作
for (int j = 0; j < size - 1 - i; j++) for (int j = 0; j < size - 1 - i; j++) {
{ if (nums[j] > nums[j + 1]) {
if (nums[j] > nums[j + 1])
{
int temp = nums[j]; int temp = nums[j];
nums[j] = nums[j + 1]; nums[j] = nums[j + 1];
nums[j + 1] = temp; nums[j + 1] = temp;
@ -46,22 +40,20 @@ void bubbleSortWithFlag(int nums[], int size) {
} }
} }
/* Driver Code */ /* Driver Code */
int main() { int main() {
int nums[6] = {4, 1, 3, 1, 5, 2}; int nums[6] = {4, 1, 3, 1, 5, 2};
printf("冒泡排序后: "); printf("冒泡排序后: ");
bubbleSort(nums, 6); bubbleSort(nums, 6);
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++) {
{
printf("%d ", nums[i]); printf("%d ", nums[i]);
} }
int nums1[6] = {4, 1, 3, 1, 5, 2};
printf("\n优化版冒泡排序后: "); printf("\n优化版冒泡排序后: ");
bubbleSortWithFlag(nums, 6); bubbleSortWithFlag(nums1, 6);
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++) {
{ printf("%d ", nums1[i]);
printf("%d ", nums[i]);
} }
printf("\n"); printf("\n");

View file

@ -62,6 +62,6 @@ public class bubble_sort
int[] nums1 = { 4, 1, 3, 1, 5, 2 }; int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1); bubbleSortWithFlag(nums1);
Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums)); Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums1));
} }
} }

View file

@ -0,0 +1,51 @@
/**
* File: bubble_sort.dart
* Created Time: 2023-02-14
* Author: what-is-me (whatisme@outlook.jp)
*/
/* 冒泡排序 */
void bubbleSort(List<int> nums) {
// n-1, n-2, ..., 1
for (int i = nums.length - 1; i > 0; i--) {
//
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// nums[j] nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
/* 冒泡排序(标志优化)*/
void bubbleSortWithFlag(List<int> nums) {
// n-1, n-2, ..., 1
for (int i = nums.length - 1; i > 0; i--) {
bool flag = false; //
//
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// nums[j] nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
flag = true; //
}
}
if (!flag) break; //
}
}
/* Driver Code */
void main() {
List<int> nums = [4, 1, 3, 1, 5, 2];
bubbleSort(nums);
print("冒泡排序完成后 nums = $nums");
List<int> nums1 = [4, 1, 3, 1, 5, 2];
bubbleSortWithFlag(nums1);
print("冒泡排序完成后 nums1 = $nums1");
}

View file

@ -0,0 +1,26 @@
/**
* File: insertion_sort.dart
* Created Time: 2023-02-14
* Author: what-is-me (whatisme@outlook.jp)
*/
/* 插入排序 */
void insertionSort(List<int> nums) {
// base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < nums.length; i++) {
int base = nums[i], j = i - 1;
// base
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // 1. nums[j]
j--;
}
nums[j + 1] = base; // 2. base
}
}
/* Driver Code */
void main() {
List<int> nums = [4, 1, 3, 1, 5, 2];
insertionSort(nums);
print("插入排序完成后 nums = $nums");
}

View file

@ -0,0 +1,51 @@
/**
* File: merge_sort.dart
* Created Time: 2023-02-14
* Author: what-is-me (whatisme@outlook.jp)
*/
/* 合并左子数组和右子数组 */
// [left, mid]
// [mid + 1, right]
void merge(List<int> nums, int left, int mid, int right) {
//
List<int> tmp = nums.sublist(left, right + 1);
//
int leftStart = left - left, leftEnd = mid - left;
//
int rightStart = mid + 1 - left, rightEnd = right - left;
// i, j
int i = leftStart, j = rightStart;
// nums
for (int k = left; k <= right; k++) {
// j++
if (i > leftEnd)
nums[k] = tmp[j++];
// <= i++
else if (j > rightEnd || tmp[i] <= tmp[j])
nums[k] = tmp[i++];
// > j++
else
nums[k] = tmp[j++];
}
}
/* 归并排序 */
void mergeSort(List<int> nums, int left, int right) {
//
if (left >= right) return; // 1
//
int mid = (left + right) ~/ 2; //
mergeSort(nums, left, mid); //
mergeSort(nums, mid + 1, right); //
//
merge(nums, left, mid, right);
}
/* Driver Code */
void main() {
/* 归并排序 */
List<int> nums = [7, 3, 2, 6, 0, 1, 5, 4];
mergeSort(nums, 0, nums.length - 1);
print("归并排序完成后 nums = $nums");
}

View file

@ -0,0 +1,147 @@
/**
* File: quick_sort.dart
* Created Time: 2023-02-14
* Author: what-is-me (whatisme@outlook.jp)
*/
/* 快速排序类 */
class QuickSort {
/* 元素交换 */
static void _swap(List<int> nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 哨兵划分 */
static int _partition(List<int> nums, int left, int right) {
// nums[left]
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; //
while (i < j && nums[i] <= nums[left]) i++; //
_swap(nums, i, j); //
}
_swap(nums, i, left); // 线
return i; //
}
/* 快速排序 */
static void quickSort(List<int> nums, int left, int right) {
// 1
if (left >= right) return;
//
int pivot = _partition(nums, left, right);
//
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
}
/* 快速排序类(中位基准数优化) */
class QuickSortMedian {
/* 元素交换 */
static void _swap(List<int> nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 选取三个元素的中位数 */
static int _medianThree(List<int> nums, int left, int mid, int right) {
// 使
// 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
return left;
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
return mid;
else
return right;
}
/* 哨兵划分(三数取中值) */
static int _partition(List<int> nums, int left, int right) {
//
int med = _medianThree(nums, left, (left + right) ~/ 2, right);
//
_swap(nums, left, med);
// nums[left]
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; //
while (i < j && nums[i] <= nums[left]) i++; //
_swap(nums, i, j); //
}
_swap(nums, i, left); // 线
return i; //
}
/* 快速排序 */
static void quickSort(List<int> nums, int left, int right) {
// 1
if (left >= right) return;
//
int pivot = _partition(nums, left, right);
//
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
}
/* 快速排序类(尾递归优化) */
class QuickSortTailCall {
/* 元素交换 */
static void _swap(List<int> nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 哨兵划分 */
static int _partition(List<int> nums, int left, int right) {
// nums[left]
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; //
while (i < j && nums[i] <= nums[left]) i++; //
_swap(nums, i, j); //
}
_swap(nums, i, left); // 线
return i; //
}
/* 快速排序(尾递归优化) */
static void quickSort(List<int> nums, int left, int right) {
// 1
while (left < right) {
//
int pivot = _partition(nums, left, right);
//
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); //
left = pivot + 1; // [pivot + 1, right]
} else {
quickSort(nums, pivot + 1, right); //
right = pivot - 1; // [left, pivot - 1]
}
}
}
}
/* Driver Code */
void main() {
/* 快速排序 */
List<int> nums = [2, 4, 1, 0, 3, 5];
QuickSort.quickSort(nums, 0, nums.length - 1);
print("快速排序完成后 nums = $nums");
/* 快速排序(中位基准数优化) */
List<int> nums1 = [2, 4, 1, 0, 3, 5];
QuickSortMedian.quickSort(nums1, 0, nums1.length - 1);
print("快速排序(中位基准数优化)完成后 nums1 = $nums1");
/* 快速排序(尾递归优化) */
List<int> nums2 = [2, 4, 1, 0, 3, 5];
QuickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
print("快速排序(尾递归优化)完成后 nums2 = $nums2");
}

View file

@ -0,0 +1,61 @@
/**
* File: radix_sort.dart
* Created Time: 2023-02-14
* Author: what-is-me (whatisme@outlook.jp)
*/
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
int digit(int num, int exp) {
// exp k
return (num ~/ exp) % 10;
}
/* 计数排序(根据 nums 第 k 位排序) */
void countSort(List<int> nums, int exp) {
// 0~9 10
List<int> bucket = List<int>.filled(10, 0);
int n = nums.length;
// 0~9
for (int i = 0; i < n; i++) {
int d = digit(nums[i], exp); // nums[i] k d
bucket[d]++; // d
}
//
for (int i = 1; i < 10; i++) {
bucket[i] += bucket[i - 1];
}
// tmp
List<int> tmp = List<int>.filled(n, 0);
for (int i = n - 1; i >= 0; i--) {
int d = digit(nums[i], exp);
int j = bucket[d] - 1; // d j
tmp[j] = nums[i]; // j
bucket[d]--; // d 1
}
// tmp nums
for (int i = 0; i < n; i++) nums[i] = tmp[i];
}
/* 基数排序 */
void radixSort(List<int> nums) {
//
// dart int 64
int ma = -1 << 63;
for (int num in nums) if (num > ma) ma = num;
//
for (int exp = 1; ma >= exp; exp *= 10)
// k
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// k = 3 -> exp = 100
// exp = 10^(k-1)
countSort(nums, exp);
}
/* Driver Code */
void main() {
/* 基数排序 */
List<int> nums = [23, 12, 3, 4, 788, 192];
radixSort(nums);
print("基数排序完成后 nums = $nums");
}

View file

@ -51,6 +51,6 @@ public class bubble_sort {
int[] nums1 = { 4, 1, 3, 1, 5, 2 }; int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1); bubbleSortWithFlag(nums1);
System.out.println("冒泡排序完成后 nums1 = " + Arrays.toString(nums)); System.out.println("冒泡排序完成后 nums1 = " + Arrays.toString(nums1));
} }
} }