Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -0,0 +1,60 @@
|
|||
class HeapSort {
|
||||
@:generic
|
||||
private static function siftDown<T>(arr: Array<T>, start:Int, end:Int) {
|
||||
var root = start;
|
||||
while (root * 2 + 1 <= end) {
|
||||
var child = root * 2 + 1;
|
||||
if (child + 1 <= end && Reflect.compare(arr[child], arr[child + 1]) < 0)
|
||||
child++;
|
||||
if (Reflect.compare(arr[root], arr[child]) < 0) {
|
||||
var temp = arr[root];
|
||||
arr[root] = arr[child];
|
||||
arr[child] = temp;
|
||||
root = child;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@:generic
|
||||
public static function sort<T>(arr:Array<T>) {
|
||||
if (arr.length > 1)
|
||||
{
|
||||
var start = (arr.length - 2) >> 1;
|
||||
while (start > 0) {
|
||||
siftDown(arr, start - 1, arr.length - 1);
|
||||
start--;
|
||||
}
|
||||
}
|
||||
|
||||
var end = arr.length - 1;
|
||||
while (end > 0) {
|
||||
var temp = arr[end];
|
||||
arr[end] = arr[0];
|
||||
arr[0] = temp;
|
||||
siftDown(arr, 0, end - 1);
|
||||
end--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
static function main() {
|
||||
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
|
||||
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
|
||||
3.5, 0.0, -4.1, -9.5];
|
||||
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
|
||||
'be', 'self-evident', 'that', 'all',
|
||||
'men', 'are', 'created', 'equal'];
|
||||
Sys.println('Unsorted Integers: ' + integerArray);
|
||||
HeapSort.sort(integerArray);
|
||||
Sys.println('Sorted Integers: ' + integerArray);
|
||||
Sys.println('Unsorted Floats: ' + floatArray);
|
||||
HeapSort.sort(floatArray);
|
||||
Sys.println('Sorted Floats: ' + floatArray);
|
||||
Sys.println('Unsorted Strings: ' + stringArray);
|
||||
HeapSort.sort(stringArray);
|
||||
Sys.println('Sorted Strings: ' + stringArray);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
def siftDown(a, start, end):
|
||||
// (end represents the limit of how far down the heap to sift)
|
||||
var root = start
|
||||
|
||||
while root * 2 + 1 <= end: // (While the root has at least one child)
|
||||
var child = root * 2 + 1 // (root*2+1 points to the left child)
|
||||
// (If the child has a sibling and the child's value is less than its sibling's...)
|
||||
if child + 1 <= end and a[child] < a[child + 1]:
|
||||
child += 1 // (... then point to the right child instead)
|
||||
if a[root] < a[child]: // (out of max-heap order)
|
||||
let r = a[root] // swap(a[root], a[child])
|
||||
a[root] = a[child]
|
||||
a[child] = r
|
||||
root = child // (repeat to continue sifting down the child now)
|
||||
else:
|
||||
return
|
||||
|
||||
def heapify(a, count):
|
||||
//(start is assigned the index in a of the last parent node)
|
||||
var start = (count - 2) >> 1
|
||||
|
||||
while start >= 0:
|
||||
// (sift down the node at index start to the proper place
|
||||
// such that all nodes below the start index are in heap order)
|
||||
siftDown(a, start, count-1)
|
||||
start -= 1
|
||||
// (after sifting down the root all nodes/elements are in heap order)
|
||||
|
||||
def heapSort(a):
|
||||
// input: an unordered array a of length count
|
||||
let count = a.length
|
||||
// (first place a in max-heap order)
|
||||
heapify(a, count)
|
||||
|
||||
var end = count - 1
|
||||
while end > 0:
|
||||
//(swap the root(maximum value) of the heap with the last element of the heap)
|
||||
let z = a[0]
|
||||
a[0] = a[end]
|
||||
a[end] = z
|
||||
//(decrement the size of the heap so that the previous max value will stay in its proper place)
|
||||
end -= 1
|
||||
// (put the heap back in max-heap order)
|
||||
siftDown(a, 0, end)
|
||||
|
||||
let inputi = [1,10,2,5,-1,5,-19,4,23,0]
|
||||
print ("input: " + inputi)
|
||||
heapSort(inputi)
|
||||
print ("sorted: " + inputi)
|
||||
let inputf = [1,-3.2,5.2,10.8,-5.7,7.3,3.5,0,-4.1,-9.5]
|
||||
print ("input: " + inputf)
|
||||
heapSort(inputf)
|
||||
print ("sorted: " + inputf)
|
||||
let inputs = ["We","hold","these","truths","to","be","self-evident","that","all","men","are","created","equal"]
|
||||
print ("input: " + inputs)
|
||||
heapSort(inputs)
|
||||
print ("sorted: " + inputs)
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
void swap(int[] array, int i1, int i2) {
|
||||
if (array[i1] == array[i2])
|
||||
return;
|
||||
var tmp = array[i1];
|
||||
array[i1] = array[i2];
|
||||
array[i2] = tmp;
|
||||
}
|
||||
|
||||
void shift_down(int[] heap, int i, int max) {
|
||||
int i_big, c1, c2;
|
||||
while (i < max) {
|
||||
i_big = i;
|
||||
c1 = (2 * i) + 1;
|
||||
c2 = c1 + 1;
|
||||
if (c1 < max && heap[c1] > heap[i_big])
|
||||
i_big = c1;
|
||||
if (c2 < max && heap[c2] > heap[i_big])
|
||||
i_big = c2;
|
||||
if (i_big == i) return;
|
||||
swap(heap, i, i_big);
|
||||
i = i_big;
|
||||
}
|
||||
}
|
||||
|
||||
void to_heap(int[] array) {
|
||||
int i = (array.length / 2) - 1;
|
||||
while (i >= 0) {
|
||||
shift_down(array, i, array.length);
|
||||
--i;
|
||||
}
|
||||
}
|
||||
|
||||
void heap_sort(int[] array) {
|
||||
to_heap(array);
|
||||
int end = array.length - 1;
|
||||
while (end > 0) {
|
||||
swap(array, 0, end);
|
||||
shift_down(array, 0, end);
|
||||
--end;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
int[] data = {
|
||||
12, 11, 15, 10, 9,
|
||||
1, 2, 13, 3, 14,
|
||||
4, 5, 6, 7, 8
|
||||
};
|
||||
heap_sort(data);
|
||||
foreach (int i in data) {
|
||||
stdout.printf("%d ", i);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue