September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,54 +1,118 @@
merge(left, right, items) {
var a = 0;
var t;
while (left.length != 0 && right.length != 0) {
if (right[0] < left[0]) {
t = right[0];
right.removeRange(0,1);
} else {
t = left[0];
left.removeRange(0,1);
}
items[a++] = t;
}
while(left.length != 0) {
t = left[0];
left.removeRange(0,1);
items[a++] = t;
}
while(right.length != 0) {
t = right[0];
right.removeRange(0,1);
items[a++] = t;
}
}
mSort(items, tmp, l) {
if (l == 1) {
return;
}
var m = (l/2).floor().toInt();
var tmp_l = tmp.getRange(0, m);
var tmp_r = tmp.getRange(m, tmp.length-m);
mSort(tmp_l, items.getRange(0,m), m);
mSort(tmp_r, items.getRange(m, items.length-m), l-m);
merge(tmp_l, tmp_r, items);
}
merge_sort(items) {
mSort(items,items.getRange(0, items.length),items.length);
}
void main() {
var arr=[1,5,2,7,3,9,4,6,8];
print("Before sort");
arr.forEach((var i)=>print("$i"));
merge_sort(arr);
print("After sort");
arr.forEach((var i)=>print("$i"));
MergeSortInDart sampleSort = MergeSortInDart();
List<int> theResultingList = sampleSort.sortTheList([54, 89, 125, 47899, 32, 61, 42, 895647, 215, 345, 6, 21, 2, 78]);
print('Here\'s the sorted list: ${theResultingList}');
}
/////////////////////////////////////
class MergeSortInDart {
List<int> sortedList;
// In Dart we often put helper functions at the bottom.
// You could put them anywhere, we just like it this way
// for organizational purposes. It's nice to be able to
// read them in the order they're called.
// Start here
List<int> sortTheList(List<int> sortThis){
// My parameters are listed vertically for readability. Dart
// doesn't care how you list them, vertically or horizontally.
sortedList = mSort(
sortThis,
sortThis.sublist(0, sortThis.length),
sortThis.length,
);
return sortThis;
}
mSort(
List<int> sortThisList,
List<int> tempList,
int thisListLength) {
if (thisListLength == 1) {
return;
}
// In Dart using ~/ is more efficient than using .floor()
int middle = (thisListLength ~/ 2);
// If you use something in a try/on/catch/finally block then
// be sure to declare it outside the block (for scope)
List<int> tempLeftList;
// This was used for troubleshooting. It was left here to show
// how a basic block try/on can be better than a debugPrint since
// it won't print unless there's a problem.
try {
tempLeftList = tempList.sublist(0, middle);
} on RangeError {
print(
'tempLeftList length = ${tempList.length}, thisListLength '
'was supposedly $thisListLength and Middle was $middle');
}
// If you see "myList.getRange(x,y)" that's a sign the code is
// from Dart 1 and needs to be updated. It's "myList.sublist" in Dart 2
List<int> tempRightList = tempList.sublist(middle);
// Left side.
mSort(
tempLeftList,
sortThisList.sublist(0, middle),
middle,
);
// Right side.
mSort(
tempRightList,
sortThisList.sublist(middle),
sortThisList.length - middle,
);
// Merge it.
dartMerge(
tempLeftList,
tempRightList,
sortThisList,
);
}
dartMerge(
List<int> leftSide,
List<int> rightSide,
List<int> sortThisList,
) {
int index = 0;
int elementValue;
// This should be human readable.
while (leftSide.isNotEmpty && rightSide.isNotEmpty) {
if (rightSide[0] < leftSide[0]) {
elementValue = rightSide[0];
rightSide.removeRange(0, 1);
} else {
elementValue = leftSide[0];
leftSide.removeRange(0, 1);
}
sortThisList[index++] = elementValue;
}
while (leftSide.isNotEmpty) {
elementValue = leftSide[0];
leftSide.removeRange(0, 1);
sortThisList[index++] = elementValue;
}
while (rightSide.isNotEmpty) {
elementValue = rightSide[0];
rightSide.removeRange(0, 1);
sortThisList[index++] = elementValue;
}
sortedList = sortThisList;
}
}