June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,48 @@
import extensions.
import system'routines.
import system'collections.
extension $op
{
quickSort
[
if (self isEmpty) [ ^ self ].
var pivot := self[0].
array_list less := ArrayList new.
array_list pivotList := ArrayList new.
array_list more := ArrayList new.
self forEach(:item)
[
if (item < pivot)
[
less append(item)
];
if (item > pivot)
[
more append(item)
];
[
pivotList append(item)
]
].
less := less quickSort.
more := more quickSort.
less appendRange(pivotList).
less appendRange(more).
^ less
]
}
program =
[
var list := (3, 14, 1, 5, 9, 2, 6, 3).
console printLine("before:", list).
console printLine("after :", list quickSort).
].

View file

@ -1,12 +1,7 @@
defmodule QuickSort do
defmodule Sort do
def qsort([]), do: []
def qsort([_|_] = list) do
List.flatten do_qsort(list)
end
defp do_qsort([pivot | rest]) do
{left, right} = Enum.split_with(rest, &(&1 < pivot))
[qsort(left), pivot, qsort(right)]
def qsort([h | t]) do
{lesser, greater} = Enum.split_with(t, &(&1 < h))
qsort(lesser) ++ [h] ++ qsort(greater)
end
end

View file

@ -0,0 +1,30 @@
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {
if (arr.isEmpty())
return arr;
else {
E pivot = arr.get(0);
List<E> less = new LinkedList<E>();
List<E> pivotList = new LinkedList<E>();
List<E> more = new LinkedList<E>();
// Partition
for (E i: arr) {
if (i.compareTo(pivot) < 0)
less.add(i);
else if (i.compareTo(pivot) > 0)
more.add(i);
else
pivotList.add(i);
}
// Recursively sort sublists
less = quickSort(less);
more = quickSort(more);
// Concatenate results
less.addAll(pivotList);
less.addAll(more);
return less;
}
}

View file

@ -0,0 +1,11 @@
public static <E extends Comparable<E>> List<E> sort(List<E> col) {
if (col == null || col.isEmpty())
return Collections.emptyList();
else {
E pivot = col.get(0);
Map<Integer, List<E>> grouped = col.stream()
.collect(Collectors.groupingBy(pivot::compareTo));
return Stream.of(sort(grouped.get(1)), grouped.get(0), sort(grouped.get(-1)))
.flatMap(Collection::stream).collect(Collectors.toList());
}
}

View file

@ -1,31 +0,0 @@
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {
if (!arr.isEmpty()) {
E pivot = arr.get(0); //This pivot can change to get faster results
List<E> less = new LinkedList<E>();
List<E> pivotList = new LinkedList<E>();
List<E> more = new LinkedList<E>();
// Partition
for (E i: arr) {
if (i.compareTo(pivot) < 0)
less.add(i);
else if (i.compareTo(pivot) > 0)
more.add(i);
else
pivotList.add(i);
}
// Recursively sort sublists
less = quickSort(less);
more = quickSort(more);
// Concatenate results
less.addAll(pivotList);
less.addAll(more);
return less;
}
return arr;
}

View file

@ -9,7 +9,7 @@ function sort(array, less) {
function quicksort(left, right) {
if (left < right) {
var pivot = array[left + Math.floor((right - right) / 2)],
var pivot = array[left + Math.floor((right - left) / 2)],
left_new = left,
right_new = right;

View file

@ -1,11 +1,10 @@
fun quicksort(list: List<Int>): List<Int> {
fun <T : Comparable<T>> quicksort(list: List<T>): List<T> {
if (list.isEmpty()) return emptyList()
val head = list.first()
val tail = list.takeLast(list.size - 1)
val less = quicksort(tail.filter { it < head })
val high = quicksort(tail.filter { it >= head })
val (less, high) = tail.partition { it < head }
return less + head + high
}

View file

@ -0,0 +1,29 @@
swap := proc(arr, a, b)
local temp := arr[a]:
arr[a] := arr[b]:
arr[b] := temp:
end proc:
quicksort := proc(arr, low, high)
local pi:
if (low < high) then
pi := qpart(arr,low,high):
quicksort(arr, low, pi-1):
quicksort(arr, pi+1, high):
end if:
end proc:
qpart := proc(arr, low, high)
local i,j,pivot;
pivot := arr[high]:
i := low-1:
for j from low to high-1 by 1 do
if (arr[j] <= pivot) then
i++:
swap(arr, i, j):
end if;
end do;
swap(arr, i+1, high):
return (i+1):
end proc:
a:=Array([12,4,2,1,0]);
quicksort(a,1,5);
a;

View file

@ -1,5 +1,5 @@
function quicksort($arr){
$loe = $gt = array();
$lte = $gt = array();
if(count($arr) < 2){
return $arr;
}
@ -7,12 +7,12 @@ function quicksort($arr){
$pivot = array_shift($arr);
foreach($arr as $val){
if($val <= $pivot){
$loe[] = $val;
}elseif ($val > $pivot){
$lte[] = $val;
} else {
$gt[] = $val;
}
}
return array_merge(quicksort($loe),array($pivot_key=>$pivot),quicksort($gt));
return array_merge(quicksort($lte),array($pivot_key=>$pivot),quicksort($gt));
}
$arr = array(1, 3, 5, 7, 9, 8, 6, 4, 2);

View file

@ -0,0 +1,49 @@
# Project : Sorting algorithms/Quicksort
# Date : 2018/03/04
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
test = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
see "before sort:" + nl
showarray(test)
quicksort(test, 1, 10)
see "after sort:" + nl
showarray(test)
func quicksort(a, s, n)
if n < 2
return
ok
t = s + n - 1
l = s
r = t
p = a[floor((l + r) / 2)]
while l <= r
while a[l] < p
l = l + 1
end
while a[r] > p
r = r - 1
end
if l <= r
temp = a[l]
a[l] = a[r]
a[r] = temp
l = l + 1
r = r - 1
ok
end
if s < r
quicksort(a, s, r - s + 1)
ok
if l < t
quicksort(a, l, t - l + 1 )
ok
func showarray(vect)
svect = ""
for n = 1 to len(vect)
svect = svect + vect[n] + " "
next
svect = left(svect, len(svect) - 1)
see svect + nl

View file

@ -0,0 +1,7 @@
fun quicksort [] = []
| quicksort (x::xs) =
let
val (left, right) = List.partition (fn y => y<x) xs
in
quicksort left @ [x] @ quicksort right
end

View file

@ -1,17 +1,3 @@
fun quicksort [] = []
| quicksort (x::xs) =
let
val (left, right) = List.partition (fn y => y<x) xs
in
quicksort left @ [x] @ quicksort right
end
------------------------------------------------------------
Solution 2:
Without using List.partition
fun par_helper([], x, l, r) = (l, r) |
par_helper(h::t, x, l, r) =
if h <= x then

View file

@ -0,0 +1,64 @@
/**
Generic quicksort function using typescript generics.
Follows quicksort as done in CLRS.
*/
export type Comparator<T> = (o1: T, o2: T) => number;
export function quickSort<T>(array: T[], compare: Comparator<T>) {
if (array.length <= 1 || array == null) {
return;
}
sort(array, compare, 0, array.length - 1);
}
function sort<T>(
array: T[], compare: Comparator<T>, low: number, high: number) {
if (low < high) {
const partIndex = partition(array, compare, low, high);
sort(array, compare, low, partIndex - 1);
sort(array, compare, partIndex + 1, high);
}
}
function partition<T>(
array: T[], compare: Comparator<T>, low: number, high: number): number {
const pivot: T = array[high];
let i: number = low - 1;
for (let j = low; j <= high - 1; j++) {
if (compare(array[j], pivot) == -1) {
i = i + 1;
swap(array, i, j)
}
}
if (compare(array[high], array[i + 1]) == -1) {
swap(array, i + 1, high);
}
return i + 1;
}
function swap<T>(array: T[], i: number, j: number) {
const newJ: T = array[i];
array[i] = array[j];
array[j] = newJ;
}
export function testQuickSort(): void {
function numberComparator(o1: number, o2: number): number {
if (o1 < o2) {
return -1;
} else if (o1 == o2) {
return 0;
}
return 1;
}
let tests: number[][] = [
[], [1], [2, 1], [-1, 2, -3], [3, 16, 8, -5, 6, 4], [1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5]
];
for (let testArray of tests) {
quickSort(testArray, numberComparator);
console.log(testArray);
}
}