Skip to content

Commit 994eaaa

Browse files
committed
fix: fixed quicksort to use randomized version and added radix sort
1 parent b958cc5 commit 994eaaa

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

lib/sort-algos/quick.dart

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import '../models/item.dart';
24
import '../models/step.dart';
35

@@ -41,12 +43,25 @@ void quickSortAlgo(List<Step> result, int low, int high) {
4143
}
4244

4345
int partition(List<Step> result, int low, int high) {
44-
var pivot = high;
46+
var random = Random();
47+
var pivot = low + random.nextInt(high - low);
4548

4649
var step = List<Item>.from(result.last.list);
4750

4851
var i = low - 1;
4952

53+
var temp1 = step[pivot].copyWith(color: matchActiveColor);
54+
step[pivot] = step[high].copyWith(color: matchActiveColor);
55+
step[high] = temp1;
56+
57+
result.add(Step(List<Item>.from(step),
58+
'Randomly choose ${step[high]} and swapped it with ${step[pivot]}'));
59+
60+
step[pivot] = step[pivot].copyWith(color: defaultColor);
61+
step[high] = step[high].copyWith(color: defaultColor);
62+
63+
pivot = high;
64+
5065
step[pivot] = step[pivot].copyWith(color: matchActiveColor);
5166

5267
for (var j = low; j < high; j++) {

lib/sort-algos/radix.dart

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'dart:math';
2+
3+
import '../models/item.dart';
4+
import '../models/step.dart';
5+
import '../utils.dart';
6+
7+
List<Step> radixSort(List<Item> input) {
8+
var result = <Step>[];
9+
var largest = max(input);
10+
11+
var step = Step(<Item>[], 'Starting Position');
12+
13+
for (var i = 0; i < input.length; i++) {
14+
step.list.add(
15+
Item(
16+
i,
17+
input[i].value,
18+
defaultColor,
19+
),
20+
);
21+
}
22+
23+
result.add(Step(List<Item>.from(step.list), step.reason));
24+
25+
for (var e = 1; (largest / e).floor() > 0; e *= 10) {
26+
countSort(
27+
input, result, e, (largest / e).floor() % 10 == (largest / e).floor());
28+
29+
result.add(Step(List<Item>.from(input),
30+
'Sorted according to significant digit no. ${logBase(e, 10) + 1}'));
31+
}
32+
33+
result.add(Step(List<Item>.from(input), 'Sorted'));
34+
35+
return result;
36+
}
37+
38+
void countSort(List<Item> a, List<Step> result, int e, bool last) {
39+
var cnt = List<int>.filled(10, 0);
40+
var res = List<Item>.filled(a.length, Item(0, 0, defaultColor));
41+
42+
for (var i = 0; i < a.length; ++i) {
43+
cnt[(a[i].value / e).floor() % 10]++;
44+
}
45+
46+
for (var i = 1; i < cnt.length; ++i) {
47+
cnt[i] += cnt[i - 1];
48+
}
49+
50+
for (var i = a.length - 1; i >= 0; --i) {
51+
res[cnt[(a[i].value / e).floor() % 10] - 1] =
52+
last ? a[i].copyWith(color: sortedColor) : a[i];
53+
result.add(Step(List<Item>.from(res),
54+
'put ${res[cnt[(a[i].value / e).floor() % 10] - 1]} in place'));
55+
56+
cnt[(a[i].value / e).floor() % 10]--;
57+
}
58+
59+
for (var i = 0; i < a.length; i++) {
60+
a[i] = res[i];
61+
}
62+
}
63+
64+
int max(List<Item> a) {
65+
var largest = -10e9.toInt();
66+
for (var i in a) {
67+
if (i.value > largest) {
68+
largest = i.value;
69+
}
70+
}
71+
72+
return largest;
73+
}
74+
75+
double logBase(num x, num base) => log(x) / log(base);

lib/utils.dart

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'sort-algos/heapsort.dart';
88
import 'sort-algos/insertion.dart';
99
import 'sort-algos/merge.dart';
1010
import 'sort-algos/quick.dart';
11+
import 'sort-algos/radix.dart';
1112
import 'sort-algos/selection.dart';
1213

1314
const matchActiveColor = Color(0xFF8CDAD8);
@@ -63,6 +64,14 @@ final List<Algorithm> sortAlgos = [
6364
url: 'https://en.wikipedia.org/wiki/Selection_sort',
6465
function: heapSort,
6566
),
67+
Algorithm(
68+
id: 6,
69+
name: 'Radix Sort',
70+
algoType: AlgoType.sorting,
71+
image: 'assets/thumbnails/heap.gif',
72+
url: 'https://en.wikipedia.org/wiki/Selection_sort',
73+
function: radixSort,
74+
),
6675
];
6776

6877
final defaultSteps = [

0 commit comments

Comments
 (0)