Added Dart examples for chapter 3 to chapter 9 (#265)
* fix: corrected method return value following Dart's newest linter version * feat: added Dart recursion examples for chapter 3 * feat: added quicksort example in Dart for chapter 4 * feat: added examples in Dart for the chapter 5 * feat: added Dart example for chapter 6 bfs * feat: added djikstra example in Dart for chapter 7 * feat: added example of set covering in Dart for chapter 8 * feat: added examples for dynamic programming in dart for chapter 9
This commit is contained in:
21
04_quicksort/dart/04_quicksort/quicksort.dart
Normal file
21
04_quicksort/dart/04_quicksort/quicksort.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
void main(List<String> args) {
|
||||
final Stopwatch stopwatch = Stopwatch()..start();
|
||||
print(quickSort([0, 21, 3, 1, 6, 5, 0, 81, 2, 14, 56, 32, 1, 9, 8]));
|
||||
stopwatch.stop();
|
||||
print(stopwatch.elapsedMilliseconds);
|
||||
}
|
||||
|
||||
List<int> quickSort(List<int> toOrder) {
|
||||
if (toOrder.length < 2) {
|
||||
return toOrder;
|
||||
}
|
||||
final int mid = toOrder.length ~/ 2;
|
||||
|
||||
final int pivot = toOrder[mid];
|
||||
toOrder.removeAt(mid);
|
||||
final List<int> lowers =
|
||||
List.from(toOrder.where((element) => element <= pivot));
|
||||
final List<int> highers =
|
||||
List.from(toOrder.where((element) => element > pivot));
|
||||
return quickSort(lowers) + [pivot] + quickSort(highers);
|
||||
}
|
||||
Reference in New Issue
Block a user