|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:flutter_constraintlayout/flutter_constraintlayout.dart'; |
| 7 | + |
| 8 | +import 'custom_app_bar.dart'; |
| 9 | + |
| 10 | +class ChenSortExample extends StatefulWidget { |
| 11 | + const ChenSortExample({Key? key}) : super(key: key); |
| 12 | + |
| 13 | + @override |
| 14 | + State createState() => ChenSortExampleState(); |
| 15 | +} |
| 16 | + |
| 17 | +class ChenSortExampleState extends State<ChenSortExample> { |
| 18 | + int defaultNumberCount = 10000; |
| 19 | + late TextEditingController numberCountController = |
| 20 | + TextEditingController(text: '$defaultNumberCount'); |
| 21 | + List<String> sortResult = []; |
| 22 | + bool isSorting = false; |
| 23 | + ScrollController scrollController = ScrollController(); |
| 24 | + |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + return Scaffold( |
| 28 | + appBar: const CustomAppBar( |
| 29 | + title: 'Chen sort', |
| 30 | + codePath: 'example/chen_sort.dart', |
| 31 | + ), |
| 32 | + body: ConstraintLayout().open(() { |
| 33 | + const Text( |
| 34 | + 'The time complexity is O(n), the space complexity is O(n), and it is stable', |
| 35 | + style: TextStyle( |
| 36 | + fontSize: 30, |
| 37 | + ), |
| 38 | + ).applyConstraint( |
| 39 | + topCenterTo: parent, |
| 40 | + ); |
| 41 | + TextField( |
| 42 | + controller: numberCountController, |
| 43 | + decoration: const InputDecoration( |
| 44 | + hintText: 'Please enter the number of random numbers', |
| 45 | + ), |
| 46 | + inputFormatters: [FilteringTextInputFormatter.digitsOnly], |
| 47 | + ).applyConstraint( |
| 48 | + width: 500, |
| 49 | + outBottomCenterTo: sId(-1).topMargin(20), |
| 50 | + ); |
| 51 | + ElevatedButton( |
| 52 | + onPressed: () async { |
| 53 | + if (isSorting) { |
| 54 | + setState(() { |
| 55 | + isSorting = false; |
| 56 | + }); |
| 57 | + } else { |
| 58 | + isSorting = true; |
| 59 | + sortResult.clear(); |
| 60 | + String text = numberCountController.text; |
| 61 | + if (text.isEmpty) { |
| 62 | + text = '$defaultNumberCount'; |
| 63 | + } |
| 64 | + int numberCount = int.parse(text); |
| 65 | + if (numberCount <= 0) { |
| 66 | + numberCount = defaultNumberCount; |
| 67 | + } |
| 68 | + while (isSorting) { |
| 69 | + sortResult.add(await compute(compareSort, numberCount)); |
| 70 | + setState(() { |
| 71 | + scrollController |
| 72 | + .jumpTo(scrollController.position.maxScrollExtent); |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | + child: Text(isSorting ? 'Stop sort' : 'Begin sort'), |
| 78 | + ).applyConstraint( |
| 79 | + centerRightTo: sId(-1), |
| 80 | + ); |
| 81 | + ListView.builder( |
| 82 | + controller: scrollController, |
| 83 | + itemBuilder: (_, index) { |
| 84 | + return Text(sortResult[index]); |
| 85 | + }, |
| 86 | + itemCount: sortResult.length, |
| 87 | + itemExtent: 50, |
| 88 | + ).applyConstraint( |
| 89 | + width: matchConstraint, |
| 90 | + height: matchConstraint, |
| 91 | + outBottomCenterTo: rId(1), |
| 92 | + bottom: parent.bottom, |
| 93 | + ); |
| 94 | + }), |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +String compareSort(int numbers) { |
| 100 | + Random random = Random(); |
| 101 | + var arr = [for (int i = 0; i < numbers; i++) random.nextInt(4294967296)]; |
| 102 | + List copy = List.of(arr); |
| 103 | + Stopwatch stopwatch = Stopwatch()..start(); |
| 104 | + chenSort(arr); |
| 105 | + int chenSortTimeUsage = stopwatch.elapsedMicroseconds; |
| 106 | + stopwatch.reset(); |
| 107 | + copy.sort(); |
| 108 | + int quickSortTimeUsage = stopwatch.elapsedMicroseconds; |
| 109 | + double percent = |
| 110 | + ((quickSortTimeUsage - chenSortTimeUsage) / quickSortTimeUsage) * 100; |
| 111 | + return 'chen sort time usage = $chenSortTimeUsage us, quick sort time usage = $quickSortTimeUsage us, ${percent.toStringAsFixed(2)}% faster'; |
| 112 | +} |
| 113 | + |
| 114 | +void chenSort(List<int> list) { |
| 115 | + int max = -2 ^ 63; |
| 116 | + for (final element in list) { |
| 117 | + if (element > max) { |
| 118 | + max = element; |
| 119 | + } |
| 120 | + } |
| 121 | + int slot; |
| 122 | + List<List<int>?> buckets = List.filled(list.length + 1, null); |
| 123 | + double factor = list.length / max; |
| 124 | + for (final element in list) { |
| 125 | + slot = (element * factor).toInt(); |
| 126 | + if (buckets[slot] == null) { |
| 127 | + buckets[slot] = []; |
| 128 | + } |
| 129 | + buckets[slot]!.add(element); |
| 130 | + } |
| 131 | + int compare(int left, int right) { |
| 132 | + return left - right; |
| 133 | + } |
| 134 | + |
| 135 | + int index = 0; |
| 136 | + for (final bucket in buckets) { |
| 137 | + if (bucket != null) { |
| 138 | + if (bucket.length > 1) { |
| 139 | + bucket.sort(compare); |
| 140 | + } |
| 141 | + for (final element in bucket) { |
| 142 | + list[index++] = element; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments