Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib:simulations:quick_sort:Quick sort implemented #188

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/simulations/QuickSortDark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/simulations/QuickSortLight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 15 additions & 3 deletions lib/src/data/simulations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:simulate/src/custom_items/simulation_card.dart';
import 'package:simulate/src/data/themedata.dart';
import 'package:simulate/src/simulations/bubble_sort.dart';
import 'package:simulate/src/simulations/quicksort.dart';
import 'package:simulate/src/simulations/selection_sort.dart';
import 'package:simulate/src/simulations/epicycloid.dart';
import 'package:simulate/src/simulations/fourier_series.dart';
Expand All @@ -14,8 +15,8 @@ import 'package:simulate/src/simulations/epicycloid_curve.dart';
import 'package:simulate/src/simulations/maurer_rose.dart';

class Simulations with ChangeNotifier {
static var _favorites = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
final _algorithm = [0, 1, 2, 9];
static var _favorites = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
final _algorithm = [0, 1, 2, 9, 10];
final _mathematics = [3, 4, 5, 6, 7, 8];
final _physics = [];
final _chemistry = [];
Expand All @@ -30,7 +31,8 @@ class Simulations with ChangeNotifier {
6: "epicycloid curve pattern mathematics animation pencil lines ",
7: "epicycloid curve pattern mathematics animation ",
8: "maurer rose pattern mathematics animation",
9: "selection sort algorithm sorting bars "
9: "selection sort algorithm sorting bars ",
10: "quick sort algorithm sorting bars"
};

Simulations() {
Expand Down Expand Up @@ -153,6 +155,16 @@ class Simulations with ChangeNotifier {
infoLink: 'https://en.wikipedia.org/wiki/Selection_sort',
fav: _favorites[9],
),
SimulationCard(
id: 10,
simulationName: 'Quick Sort',
image: theme.darkTheme
? 'assets/simulations/QuickSortDark.png'
: 'assets/simulations/QuickSortLight.png',
direct: QuickSort(),
infoLink: 'https://en.wikipedia.org/wiki/Quicksort',
fav: _favorites[10],
),
];
}

Expand Down
285 changes: 285 additions & 0 deletions lib/src/simulations/quicksort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class QuickSort extends StatefulWidget {
@override
_QuickSortState createState() => _QuickSortState();
}

class _QuickSortState extends State<QuickSort> {
int _numberOfElements = 2;
final ScrollController _scrollController = ScrollController();
int delay = 0;
bool sorting = false;
List<int> elements = [];
int top = -1;
int l = 0, h = 1;
int i = -1, j = 0;
int looping = 0;
double barwidth = 1;
List<Container> containerList = [];
List<int> stack = new List(2);

@override
void initState() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.initState();
Future.delayed(Duration.zero, () {
setState(() {
_randomize();
});
});
}

@override
void dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
super.dispose();
}

_randomize() {
elements = [];
containerList = [];
for (int k = 0; k < _numberOfElements; k++)
elements.add(Random().nextInt(400));
containerList = elements
.map((e) => Container(
color: Theme.of(context).primaryColor,
height: e + 0.5,
width: MediaQuery.of(context).size.width / (elements.length + 1),
))
.toList();
sorting = false;
l = 0;
h = _numberOfElements - 1;
top = -1;
i = -1;
j = 0;
looping = 0;
stack = new List(_numberOfElements);
stack[++top] = l;
stack[++top] = h;
barwidth = MediaQuery.of(context).size.width / (elements.length + 1);
}

nextStep() async {
await Future.delayed(Duration(milliseconds: delay));
if (looping == 0) {
h = stack[top--];
l = stack[top--];
i = (l - 1);
j = l;
looping = 1;
}
if (looping == 1) {
if (j < h) {
if (elements[j] <= elements[h]) {
i++;
int temp = elements[i];
elements[i] = elements[j];
elements[j] = temp;
containerList = elements
.map((e) => Container(
color: Theme.of(context).primaryColor,
height: e + 0.5,
width: barwidth,
))
.toList();
}
j++;
} else if (j >= h) looping = 2;
} else if (looping == 2) {
int temp = elements[i + 1];
elements[i + 1] = elements[h];
elements[h] = temp;
containerList = elements
.map((e) => Container(
color: Theme.of(context).primaryColor,
height: e + 0.5,
width: barwidth,
))
.toList();
int p = i + 1;
if (p - 1 > l) {
stack[++top] = l;
stack[++top] = p - 1;
}
if (p + 1 < h) {
stack[++top] = p + 1;
stack[++top] = h;
}
looping = 3;
}
setState(() {
if (looping == 3) {
if (top != -1)
looping = 0;
else
sorting = false;
}
});
}

@override
Widget build(BuildContext context) {
if (sorting == true && looping != 3) {
WidgetsBinding.instance.addPostFrameCallback((_) => nextStep());
}

return LayoutBuilder(
// ignore: missing_return
builder: (_, BoxConstraints constraints) {
if (constraints.maxWidth != 0) {
ScreenUtil.init(
constraints,
designSize: Size(512.0, 1024.0),
allowFontScaling: true,
);
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
sorting = false;
Navigator.pop(context);
},
),
centerTitle: true,
title: Text(
'Quick Sort',
style: Theme.of(context).textTheme.headline6,
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
child: (!sorting)
? Icon(
Icons.play_arrow,
color: Colors.black,
)
: Icon(
Icons.pause,
color: Colors.black,
),
onPressed: () {
if (!sorting) {
setState(() {
sorting = true;
});
} else
setState(() {
sorting = false;
});
},
),
floatingActionButtonLocation:
FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: Container(
height: ScreenUtil().setHeight(1024 / 5.5),
child: Material(
elevation: 30,
color: Theme.of(context).primaryColor,
child: Scrollbar(
controller: _scrollController,
isAlwaysShown: true,
child: ListView(
controller: _scrollController,
padding: EdgeInsets.all(8.0),
children: <Widget>[
SizedBox(
height: 20,
),
Slider(
min: 2,
max: 200,
activeColor: Theme.of(context).accentColor,
inactiveColor: Colors.grey,
onChanged: (value) {
sorting = false;
setState(() {
_numberOfElements = value.toInt();
_randomize();
});
},
value: _numberOfElements.toDouble(),
),
Center(
child: Text(
"Elements: ${_numberOfElements.toInt()}",
style: Theme.of(context).textTheme.subtitle2,
),
),
SizedBox(
height: 20,
),
Slider(
min: 0,
max: 100,
divisions: 10,
activeColor: Theme.of(context).accentColor,
inactiveColor: Colors.grey,
onChanged: (value) {
setState(() {
delay = value.toInt();
});
},
onChangeEnd: (value) {
setState(() {
delay = value.toInt();
});
},
value: delay.roundToDouble(),
),
Center(
child: Text(
"Delay: ${delay.toInt()} ms",
style: Theme.of(context).textTheme.subtitle2,
),
),
],
),
),
),
),
body: Stack(
children: <Widget>[
Container(
color: Colors.grey[900],
child: Column(
children: <Widget>[
Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: containerList,
),
Spacer(),
],
),
),
Positioned(
top: 5,
left: 5,
child: Text(
"Swap: \nPivot: ",
style: Theme.of(context).textTheme.subtitle2,
),
),
],
),
);
}
},
);
}
}