March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -33,9 +33,10 @@ begin
|
|||
end loop;
|
||||
exit when Left >= Right;
|
||||
Swap(Item(Left), Item(Right));
|
||||
if Left < Item'Last and Right > Item'First then
|
||||
Left := Index_Type'Succ(Left);
|
||||
Right := Index_Type'Pred(Right);
|
||||
if Pivot_Index = Left then
|
||||
Pivot_Index := Right;
|
||||
elsif Pivot_Index = Right then
|
||||
Pivot_Index := Left;
|
||||
end if;
|
||||
end loop;
|
||||
if Right > Item'First then
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
a := [4, 65, 2, -31, 0, 99, 83, 782, 7]
|
||||
for k, v in QuickSort(a)
|
||||
Out .= "," v
|
||||
MsgBox, % SubStr(Out, 2)
|
||||
return
|
||||
|
||||
QuickSort(a)
|
||||
{
|
||||
if (a.MaxIndex() <= 1)
|
||||
return a
|
||||
Less := [], Same := [], More := []
|
||||
Pivot := a[1]
|
||||
for k, v in a
|
||||
{
|
||||
if (v < Pivot)
|
||||
less.Insert(v)
|
||||
else if (v > Pivot)
|
||||
more.Insert(v)
|
||||
else
|
||||
same.Insert(v)
|
||||
}
|
||||
Less := QuickSort(Less)
|
||||
Out := QuickSort(More)
|
||||
if (Same.MaxIndex())
|
||||
Out.Insert(1, Same*) ; insert all values of same at index 1
|
||||
if (Less.MaxIndex())
|
||||
Out.Insert(1, Less*) ; insert all values of less at index 1
|
||||
return Out
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
( ( Q
|
||||
= Less Greater Equal pivot element
|
||||
. !arg:%(?pivot:?Equal) %?arg
|
||||
& :?Less:?Greater
|
||||
& whl
|
||||
' ( !arg:%?element ?arg
|
||||
& (.!element)+(.!pivot) { BAD: 1900+90 adds to 1990, GOOD: (.1900)+(.90) is sorted to (.90)+(.1900) }
|
||||
: ( (.!element)+(.!pivot)
|
||||
& !element !Less:?Less
|
||||
| (.!pivot)+(.!element)
|
||||
& !element !Greater:?Greater
|
||||
| ?&!element !Equal:?Equal
|
||||
)
|
||||
)
|
||||
& Q$!Less !Equal Q$!Greater
|
||||
| !arg
|
||||
)
|
||||
& out$Q$(1900 optimized variants of 4001/2 Quicksort (quick,sort) are (quick,sober) features of 90 languages)
|
||||
);
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func partition(a sort.Interface, first int, last int, pivotIndex int) int {
|
||||
a.Swap(first, pivotIndex) // move it to beginning
|
||||
left := first+1
|
||||
right := last
|
||||
for left <= right {
|
||||
for left <= last && a.Less(left, first) {
|
||||
left++
|
||||
}
|
||||
for right >= first && a.Less(first, right) {
|
||||
right--
|
||||
}
|
||||
if left <= right {
|
||||
a.Swap(left, right)
|
||||
left++
|
||||
right--
|
||||
}
|
||||
}
|
||||
a.Swap(first, right) // swap into right place
|
||||
return right
|
||||
}
|
||||
|
||||
func quicksortHelper(a sort.Interface, first int, last int) {
|
||||
if first >= last {
|
||||
return
|
||||
}
|
||||
pivotIndex := partition(a, first, last, rand.Intn(last - first + 1) + first)
|
||||
quicksortHelper(a, first, pivotIndex-1)
|
||||
quicksortHelper(a, pivotIndex+1, last)
|
||||
}
|
||||
|
||||
func quicksort(a sort.Interface) {
|
||||
quicksortHelper(a, 0, a.Len()-1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := []int{1, 3, 5, 7, 9, 8, 6, 4, 2}
|
||||
fmt.Printf("Unsorted: %v\n", a)
|
||||
quicksort(sort.IntSlice(a))
|
||||
fmt.Printf("Sorted: %v\n", a)
|
||||
b := []string{"Emil", "Peg", "Helen", "Juergen", "David", "Rick", "Barb", "Mike", "Tom"}
|
||||
fmt.Printf("Unsorted: %v\n", b)
|
||||
quicksort(sort.StringSlice(b))
|
||||
fmt.Printf("Sorted: %v\n", b)
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
function modes(values)
|
||||
dict = Dict() # Values => Number of repetitions
|
||||
modesArray = typeof(values[1])[] # Array of the modes so far
|
||||
max = 0 # Max of repetitions so far
|
||||
|
||||
for v in values
|
||||
# Add one to the dict[v] entry (create one if none)
|
||||
if v in keys(dict)
|
||||
dict[v] += 1
|
||||
else
|
||||
dict[v] = 1
|
||||
end
|
||||
|
||||
# Update modesArray if the number of repetitions
|
||||
# of v reaches or surpasses the max value
|
||||
if dict[v] >= max
|
||||
if dict[v] > max
|
||||
empty!(modesArray)
|
||||
max += 1
|
||||
end
|
||||
append!(modesArray, [v])
|
||||
end
|
||||
end
|
||||
|
||||
return modesArray
|
||||
end
|
||||
|
||||
println(modes([1,3,6,6,6,6,7,7,12,12,17]))
|
||||
println(modes((1,1,2,4,4)))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.Comparator
|
||||
import java.util.ArrayList
|
||||
|
||||
fun <T> quickSort(a : List<T>, c : Comparator<T>) : ArrayList<T> {
|
||||
return if (a.size == 0) ArrayList(a)
|
||||
else {
|
||||
val boxes = Array<ArrayList<T>>(3, {ArrayList<T>()})
|
||||
fun normalise(i : Int) = i / Math.max(1, Math.abs(i))
|
||||
a forEach {boxes[normalise(c.compare(it, a[0])) + 1] add(it)}
|
||||
array(0, 2) forEach {boxes[it] = quickSort(boxes[it], c)}
|
||||
boxes.flatMapTo(ArrayList<T>()) {it}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
void quicksortInPlace(NSMutableArray *array, NSInteger first, NSInteger last, NSComparator comparator) {
|
||||
if (first >= last) return;
|
||||
id pivot = array[(first + last) / 2];
|
||||
NSInteger left = first;
|
||||
NSInteger right = last;
|
||||
while (left <= right) {
|
||||
while (comparator(array[left], pivot) == NSOrderedAscending)
|
||||
left++;
|
||||
while (comparator(array[right], pivot) == NSOrderedDescending)
|
||||
right--;
|
||||
if (left <= right)
|
||||
[array exchangeObjectAtIndex:left++ withObjectAtIndex:right--];
|
||||
}
|
||||
quicksortInPlace(array, first, right, comparator);
|
||||
quicksortInPlace(array, left, last, comparator);
|
||||
}
|
||||
|
||||
NSArray* quicksort(NSArray *unsorted, NSComparator comparator) {
|
||||
NSMutableArray *a = [NSMutableArray arrayWithArray:unsorted];
|
||||
quicksortInPlace(a, 0, a.count - 1, comparator);
|
||||
return a;
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
@autoreleasepool {
|
||||
NSArray *a = @[ @1, @3, @5, @7, @9, @8, @6, @4, @2 ];
|
||||
NSLog(@"Unsorted: %@", a);
|
||||
NSLog(@"Sorted: %@", quicksort(a, ^(id x, id y) { return [x compare:y]; }));
|
||||
NSArray *b = @[ @"Emil", @"Peg", @"Helen", @"Juergen", @"David", @"Rick", @"Barb", @"Mike", @"Tom" ];
|
||||
NSLog(@"Unsorted: %@", b);
|
||||
NSLog(@"Sorted: %@", quicksort(b, ^(id x, id y) { return [x compare:y]; }));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue