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

@ -8,12 +8,10 @@ fun merge(left, right):
let result = []
while not (left.isempty or right.isempty):
if left[1] <= right[1]:
result.push left.shift()
result.push! left.shift!()
else:
result.push right.shift()
result.push left.push right
result.push! right.shift!()
result.push! left.push! right
let arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
print mergesort arr
print mergeSort arr

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;
}
}

View file

@ -1,69 +1,68 @@
subroutine Merge(A,NA,B,NB,C,NC)
program TestMergeSort
implicit none
integer, parameter :: N = 8
integer :: A(N) = (/ 1, 5, 2, 7, 3, 9, 4, 6 /)
integer :: work((size(A) + 1) / 2)
write(*,'(A,/,10I3)')'Unsorted array :',A
call MergeSort(A, work)
write(*,'(A,/,10I3)')'Sorted array :',A
contains
integer, intent(in) :: NA,NB,NC ! Normal usage: NA+NB = NC
integer, intent(in out) :: A(NA) ! B overlays C(NA+1:NC)
integer, intent(in) :: B(NB)
integer, intent(in out) :: C(NC)
subroutine merge(A, B, C)
implicit none
! The targe attribute is necessary, because A .or. B might overlap with C.
integer, target, intent(in) :: A(:), B(:)
integer, target, intent(inout) :: C(:)
integer :: i, j, k
integer :: I,J,K
if (size(A) + size(B) > size(C)) stop(1)
I = 1; J = 1; K = 1;
do while(I <= NA .and. J <= NB)
if (A(I) <= B(J)) then
C(K) = A(I)
I = I+1
else
C(K) = B(J)
J = J+1
endif
K = K + 1
enddo
do while (I <= NA)
C(K) = A(I)
I = I + 1
K = K + 1
enddo
return
i = 1; j = 1
do k = 1, size(C)
if (i <= size(A) .and. j <= size(B)) then
if (A(i) <= B(j)) then
C(k) = A(i)
i = i + 1
else
C(k) = B(j)
j = j + 1
end if
else if (i <= size(A)) then
C(k) = A(i)
i = i + 1
else if (j <= size(B)) then
C(k) = B(j)
j = j + 1
end if
end do
end subroutine merge
end subroutine merge
subroutine swap(x, y)
implicit none
integer, intent(inout) :: x, y
integer :: tmp
tmp = x; x = y; y = tmp
end subroutine
recursive subroutine MergeSort(A,N,T)
integer, intent(in) :: N
integer, dimension(N), intent(in out) :: A
integer, dimension((N+1)/2), intent (out) :: T
integer :: NA,NB,V
if (N < 2) return
if (N == 2) then
if (A(1) > A(2)) then
V = A(1)
A(1) = A(2)
A(2) = V
endif
return
endif
NA=(N+1)/2
NB=N-NA
call MergeSort(A,NA,T)
call MergeSort(A(NA+1),NB,T)
if (A(NA) > A(NA+1)) then
T(1:NA)=A(1:NA)
call Merge(T,NA,A(NA+1),NB,A,N)
endif
return
end subroutine MergeSort
program TestMergeSort
integer, parameter :: N = 8
integer, dimension(N) :: A = (/ 1, 5, 2, 7, 3, 9, 4, 6 /)
integer, dimension ((N+1)/2) :: T
call MergeSort(A,N,T)
write(*,'(A,/,10I3)')'Sorted array :',A
end program TestMergeSort
recursive subroutine MergeSort(A, work)
implicit none
integer, intent(inout) :: A(:)
integer, intent(inout) :: work(:)
integer :: half
half = (size(A) + 1) / 2
if (size(A) < 2) then
continue
else if (size(A) == 2) then
if (A(1) > A(2)) then
call swap(A(1), A(2))
end if
else
call MergeSort(A( : half), work)
call MergeSort(A(half + 1 :), work)
if (A(half) > A(half + 1)) then
work(1 : half) = A(1 : half)
call merge(work(1 : half), A(half + 1:), A)
endif
end if
end subroutine MergeSort
end program TestMergeSort

View file

@ -1,45 +1,52 @@
func mergeSort<T:Comparable>(inout list:[T]) {
if list.count <= 1 {
return
// Merge Sort in Swift 4.2
// Source: https://github.com/raywenderlich/swift-algorithm-club/tree/master/Merge%20Sort
// NOTE: by use of generics you can make it sort arrays of any type that conforms to
// Comparable protocol, however this is not always optimal
import Foundation
func mergeSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array }
let middleIndex = array.count / 2
let leftPart = mergeSort(Array(array[0..<middleIndex]))
let rightPart = mergeSort(Array(array[middleIndex..<array.count]))
func merge(left: [Int], right: [Int]) -> [Int] {
var leftIndex = 0
var rightIndex = 0
var merged = [Int]()
merged.reserveCapacity(left.count + right.count)
while leftIndex < left.count && rightIndex < right.count {
if left[leftIndex] < right[rightIndex] {
merged.append(left[leftIndex])
leftIndex += 1
} else if left[leftIndex] > right[rightIndex] {
merged.append(right[rightIndex])
rightIndex += 1
} else {
merged.append(left[leftIndex])
leftIndex += 1
merged.append(right[rightIndex])
rightIndex += 1
}
}
func merge(var left:[T], var right:[T]) -> [T] {
var result = [T]()
while left.count != 0 && right.count != 0 {
if left[0] <= right[0] {
result.append(left.removeAtIndex(0))
} else {
result.append(right.removeAtIndex(0))
}
}
while left.count != 0 {
result.append(left.removeAtIndex(0))
}
while right.count != 0 {
result.append(right.removeAtIndex(0))
}
return result
while leftIndex < left.count {
merged.append(left[leftIndex])
leftIndex += 1
}
var left = [T]()
var right = [T]()
let mid = list.count / 2
for i in 0..<mid {
left.append(list[i])
while rightIndex < right.count {
merged.append(right[rightIndex])
rightIndex += 1
}
for i in mid..<list.count {
right.append(list[i])
}
return merged
}
mergeSort(&left)
mergeSort(&right)
list = merge(left, right)
return merge(left: leftPart, right: rightPart)
}