Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,7 +1,8 @@
|
|||
{{Sorting Algorithm}}[[Category:Recursion]]
|
||||
{{wikipedia|Quicksort}}
|
||||
|
||||
The task is to sort an array (or list) elements using the ''quicksort'' algorithm. The elements must have a strict weak order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
|
||||
The task is to sort an array (or list) elements using the ''quicksort'' algorithm.
|
||||
The elements must have a strict weak order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
|
||||
|
||||
Quicksort, also known as ''partition-exchange sort'', uses these steps.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
qsort ← {1≥⍴⍵:⍵⋄e←⍵[?⍴⍵]⋄ (∇(⍵<e)/⍵) , ((⍵=e)/⍵) , ∇(⍵>e)/⍵}
|
||||
qsort 1 3 5 7 9 8 6 4 2
|
||||
1 2 3 4 5 6 7 8 9
|
||||
qsort ← {1≥⍴⍵:⍵ ⋄ e←⍵[?⍴⍵] ⋄ (∇(⍵<e)/⍵) , ((⍵=e)/⍵) , (∇(⍵>e)/⍵)}
|
||||
qsort 31 4 1 5 9 2 6 5 3 5 8
|
||||
1 2 3 4 5 5 5 6 8 9 31
|
||||
|
|
|
|||
|
|
@ -1,29 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void quick_sort (int *a, int n) {
|
||||
int i, j, p, t;
|
||||
if (n < 2)
|
||||
return;
|
||||
int p = a[n / 2];
|
||||
int *l = a;
|
||||
int *r = a + n - 1;
|
||||
while (l <= r) {
|
||||
if (*l < p) {
|
||||
l++;
|
||||
continue;
|
||||
}
|
||||
if (*r > p) {
|
||||
r--;
|
||||
continue; // we need to check the condition (l <= r) every time we change the value of l or r
|
||||
}
|
||||
int t = *l;
|
||||
*l++ = *r;
|
||||
*r-- = t;
|
||||
p = a[n / 2];
|
||||
for (i = 0, j = n - 1;; i++, j--) {
|
||||
while (a[i] < p)
|
||||
i++;
|
||||
while (p < a[j])
|
||||
j--;
|
||||
if (i >= j)
|
||||
break;
|
||||
t = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = t;
|
||||
}
|
||||
quick_sort(a, r - a + 1);
|
||||
quick_sort(l, a + n - l);
|
||||
quick_sort(a, i);
|
||||
quick_sort(a + i, n - i);
|
||||
}
|
||||
|
||||
int main () {
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
int n = sizeof a / sizeof a[0];
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
quick_sort(a, n);
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array;
|
||||
|
||||
auto quickSort(T)(T[] items) /*pure*/ nothrow {
|
||||
auto quickSort(T)(T[] items) pure nothrow @safe {
|
||||
if (items.length < 2)
|
||||
return items;
|
||||
auto pivot = items[0];
|
||||
immutable pivot = items[0];
|
||||
return items[1 .. $].filter!(x => x < pivot).array.quickSort ~
|
||||
pivot ~
|
||||
items[1 .. $].filter!(x => x >= pivot).array.quickSort;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void quickSort(T)(T[] items) pure nothrow {
|
||||
void quickSort(T)(T[] items) pure nothrow @safe @nogc {
|
||||
if (items.length >= 2) {
|
||||
auto parts = partition3(items, items[$ / 2]);
|
||||
parts[0].quickSort;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
defer lessthan ( a@ b@ -- ? ) ' < is lessthan
|
||||
|
||||
: mid ( l r -- mid ) over - 2/ -cell and + ;
|
||||
|
||||
: exch ( addr1 addr2 -- ) dup @ >r over @ swap ! r> swap ! ;
|
||||
|
|
@ -7,8 +5,8 @@ defer lessthan ( a@ b@ -- ? ) ' < is lessthan
|
|||
: partition ( l r -- l r r2 l2 )
|
||||
2dup mid @ >r ( r: pivot )
|
||||
2dup begin
|
||||
swap begin dup @ r@ lessthan while cell+ repeat
|
||||
swap begin r@ over @ lessthan while cell- repeat
|
||||
swap begin dup @ r@ < while cell+ repeat
|
||||
swap begin r@ over @ < while cell- repeat
|
||||
2dup <= if 2dup exch >r cell+ r> cell- then
|
||||
2dup > until r> drop ;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
sort!(A, alg=QuickSort)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
function quicksort!(A,i=1,j=length(A))
|
||||
if j > i
|
||||
pivot = A[rand(i:j)] # random element of A
|
||||
left, right = i, j
|
||||
while left <= right
|
||||
while A[left] < pivot
|
||||
left += 1
|
||||
end
|
||||
while A[right] > pivot
|
||||
right -= 1
|
||||
end
|
||||
if left <= right
|
||||
A[left], A[right] = A[right], A[left]
|
||||
left += 1
|
||||
right -= 1
|
||||
end
|
||||
end
|
||||
quicksort!(A,i,right)
|
||||
quicksort!(A,left,j)
|
||||
end
|
||||
return A
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
qsort(L) = isempty(L) ? L : vcat(qsort(filter(x -> x < L[1], L[2:end])), L[1:1], qsort(filter(x -> x >= L[1], L[2:end])))
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
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)))
|
||||
|
|
@ -1 +1 @@
|
|||
1 2 3 4 5 6 7 8 9
|
||||
_f()
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
_f()
|
||||
:[....]
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
:[....]
|
||||
f:*x@1?#x
|
||||
|
|
|
|||
|
|
@ -1 +1,9 @@
|
|||
f:*x@1?#x
|
||||
:[
|
||||
0=#x; / if length of x is zero
|
||||
x; / then return x
|
||||
/ else
|
||||
,/( / join the results of:
|
||||
_f x@&x<f / sort (recursively) elements less than f (pivot)
|
||||
x@&x=f / element equal to f
|
||||
_f x@&x>f) / sort (recursively) elements greater than f
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,9 +1 @@
|
|||
:[
|
||||
0=#x; / if length of x is zero
|
||||
x; / then return x
|
||||
/ else
|
||||
,/( / join the results of:
|
||||
_f x@&x<f / sort (recursively) elements less than f (pivot)
|
||||
x@&x=f / element equal to f
|
||||
_f x@&x>f) / sort (recursively) elements greater than f
|
||||
]
|
||||
t@<t:1 3 5 7 9 8 6 4 2
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
function quicksort(t, start, endi)
|
||||
start, endi = start or 1, endi or #t
|
||||
--partition w.r.t. first element
|
||||
if(endi - start < 2) then return t end
|
||||
if(endi - start < 1) then return t end
|
||||
local pivot = start
|
||||
for i = start + 1, endi do
|
||||
if t[i] <= t[pivot] then
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
sub quick_sort {
|
||||
my @a = @_;
|
||||
return @a if @a < 2;
|
||||
my $p = pop @a;
|
||||
my $p = splice @a, int rand @a, 1;
|
||||
quick_sort(grep $_ < $p, @a), $p, quick_sort(grep $_ >= $p, @a);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
def qsort(array):
|
||||
if len(array) < 2:
|
||||
return array
|
||||
head, *tail = array
|
||||
less = qsort([i for i in tail if i < head])
|
||||
more = qsort([i for i in tail if i >= head])
|
||||
return less + [head] + more
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
def quicksort(array):
|
||||
_quicksort(array, 0, len(array) - 1)
|
||||
|
||||
def _quicksort(array, start, stop):
|
||||
if stop - start > 0:
|
||||
pivot, left, right = array[start], start, stop
|
||||
while left <= right:
|
||||
while array[left] < pivot:
|
||||
left += 1
|
||||
while array[right] > pivot:
|
||||
right -= 1
|
||||
if left <= right:
|
||||
array[left], array[right] = array[right], array[left]
|
||||
left += 1
|
||||
right -= 1
|
||||
_quicksort(array, start, right)
|
||||
_quicksort(array, left, stop)
|
||||
|
|
@ -1,119 +1,114 @@
|
|||
/*REXX program sorts a stemmed array using the quicksort method. */
|
||||
/*REXX program sorts a stemmed array using the quicksort algorithm.*/
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show before array elements.*/
|
||||
call quickSort highItem /*here come da judge, here come..*/
|
||||
call show@ ' after sort' /*show after array elements.*/
|
||||
call show@ 'before sort' /*show before array elements.*/
|
||||
call quickSort # /*invoke the quicksort routine.*/
|
||||
call show@ ' after sort' /*show after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────QUICKSORT subroutine────────────────*/
|
||||
quickSort: procedure expose @. /*access the caller's local var. */
|
||||
a.1=1; b.1=arg(1); $=1
|
||||
|
||||
do while $\==0; l=a.$; t=b.$; $=$-1
|
||||
if t<2 then iterate
|
||||
h=l+t-1
|
||||
?=l+t%2
|
||||
if @.h<@.l then if @.?<@.h then do; p=@.h; @.h=@.l; end
|
||||
else if @.?>@.l then p=@.l
|
||||
else do; p=@.?; @.?=@.l; end
|
||||
else if @.?<@.l then p=@.l
|
||||
else if @.?>@.h then do; p=@.h; @.h=@.l; end
|
||||
else do; p=@.?; @.?=@.l; end
|
||||
j=l+1
|
||||
do while $\==0; L=a.$; t=b.$; $=$-1; if t<2 then iterate
|
||||
h=L+t-1
|
||||
?=L+t%2
|
||||
if @.h<@.L then if @.?<@.h then do; p=@.h; @.h=@.L; end
|
||||
else if @.?>@.L then p=@.L
|
||||
else do; p=@.?; @.?=@.L; end
|
||||
else if @.?<@.l then p=@.L
|
||||
else if @.?>@.h then do; p=@.h; @.h=@.L; end
|
||||
else do; p=@.?; @.?=@.L; end
|
||||
j=L+1
|
||||
k=h
|
||||
do forever
|
||||
do j=j while j<=k & @.j<=p; end /*a tinie-tiny loop*/
|
||||
do k=k by -1 while j <k & @.k>=p; end /*another tiny loop*/
|
||||
if j>=k then leave
|
||||
_=@.j; @.j=@.k; @.k=_
|
||||
do j=j while j<=k & @.j<=p; end /*a tinie-tiny loop*/
|
||||
do k=k by -1 while j <k & @.k>=p; end /*another " " */
|
||||
if j>=k then leave /*segment finished?*/
|
||||
_=@.j; @.j=@.k; @.k=_ /*swap j&k elements*/
|
||||
end /*forever*/
|
||||
|
||||
k=j-1; @.l=@.k; @.k=p
|
||||
$=$+1
|
||||
if j<=? then do; a.$=j; b.$=h-j+1; $=$+1; a.$=l; b.$=k-l; end
|
||||
else do; a.$=l; b.$=k-l; $=$+1; a.$=j; b.$=h-j+1; end
|
||||
end /*while $\==0*/
|
||||
k=j-1; @.L=@.k; @.k=p; $=$+1
|
||||
if j<=? then do; a.$=j; b.$=h-j+1; $=$+1; a.$=L; b.$=k-L; end
|
||||
eLse do; a.$=L; b.$=k-L; $=$+1; a.$=j; b.$=h-j+1; end
|
||||
end /*whiLe $¬==0*/
|
||||
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.=; maxL=0 /*assign default value for array.*/
|
||||
@.1 =" Rivers that form part of a state's (USA) border " /*adj. later,*/
|
||||
@.2 ='=' /*this value is expanded later. */
|
||||
@.3 ="Perdido River: Alabama, Florida"
|
||||
@.4 ="Chattahoochee River: Alabama, Georgia"
|
||||
@.5 ="Tennessee River: Alabama, Kentucky, Mississippi, Tennessee"
|
||||
@.6 ="Colorado River: Arizona, California, Nevada, Baja California (Mexico)"
|
||||
@.7 ="Mississippi River: Arkansas, Illinois, Iowa, Kentucky, Minnesota, Mississippi, Missouri, Tennesse, Louisiana, Wisconsin"
|
||||
@.8 ="St. Francis River: Arkansas, Missouri"
|
||||
@.9 ="Poteau River: Arkansas, Oklahoma"
|
||||
@.10="Arkansas River: Arkansas, Oklahoma"
|
||||
@.11="Red River (Mississippi watershed): Arkansas, Oklahoma, Texas"
|
||||
@.12="Byram River: Connecticut, New York"
|
||||
@.13="Pawcatuck River: Connecticut, Rhode Island"
|
||||
@.14="Delaware River: Delaware, New Jersey, New York, Pennsylvania"
|
||||
@.15="Potomac River: District of Columbia, Maryland, Virginia, West Virginia"
|
||||
@.16="St. Marys River: Florida, Georgia"
|
||||
@.17="Chattooga River: Georgia, South Carolina"
|
||||
@.18="Tugaloo River: Georgia, South Carolina"
|
||||
@.19="Savannah River: Georgia, South Carolina"
|
||||
@.20="Snake River: Idaho, Oregon, Washington"
|
||||
@.21="Wabash River: Illinois, Indiana"
|
||||
@.22="Ohio River: Illinois, Indiana, Kentucky, Ohio, West Virginia"
|
||||
@.23="Great Miami River (mouth only): Indiana, Ohio"
|
||||
@.24="Des Moines River: Iowa, Missouri"
|
||||
@.25="Big Sioux River: Iowa, South Dakota"
|
||||
@.26="Missouri River: Kansas, Iowa, Missouri, Nebraska, South Dakota"
|
||||
@.27="Tug Fork River: Kentucky, Virginia, West Virginia"
|
||||
@.28="Big Sandy River: Kentucky, West Virginia"
|
||||
@.29="Pearl River: Louisiana, Mississippi"
|
||||
@.30="Sabine River: Louisiana, Texas"
|
||||
@.31="Monument Creek: Maine, New Brunswick (Canda)"
|
||||
@.32="St. Croix River: Maine, New Brunswick (Canda)"
|
||||
@.33="Piscataqua River: Maine, New Hampshire"
|
||||
@.34="St. Francis River: Maine, Quebec (Canada)"
|
||||
@.35="St. John River: Maine, Quebec (Canada)"
|
||||
@.36="Pocomoke River: Maryland, Virginia"
|
||||
@.37="Palmer River: Massachusetts, Rhode Island"
|
||||
@.38="Runnins River: Massachusetts, Rhode Island"
|
||||
@.39="Montreal River: Michigan (upper peninsula), Wisconsin"
|
||||
@.40="Detroit River: Michigan, Ontario (Canada)"
|
||||
@.41="St. Clair River: Michigan, Ontario (Canada)"
|
||||
@.42="St. Marys River: Michigan, Ontario (Canada)"
|
||||
@.43="Brule River: Michigan, Wisconsin"
|
||||
@.44="Menominee River: Michigan, Wisconsin"
|
||||
@.45="Red River of the North: Minnesota, North Dakota"
|
||||
@.46="Bois de Sioux River: Minnesota, North Dakota, South Dakota"
|
||||
@.47="Pigeon River: Minnesota, Ontario (Canada)"
|
||||
@.48="Rainy River: Minnesota, Ontario (Canada)"
|
||||
@.49="St. Croix River: Minnesota, Wisconsin"
|
||||
@.50="St. Louis River: Minnesota, Wisconsin"
|
||||
@.51="Halls Stream: New Hampshire, Canada"
|
||||
@.52="Salmon Falls River: New Hampshire, Maine"
|
||||
@.53="Connecticut River: New Hampshire, Vermont"
|
||||
@.54="Arthur Kill: New Jersey, New York (tidal strait)"
|
||||
@.55="Kill Van Kull: New Jersey, New York (tidal strait)"
|
||||
@.56="Hudson River (lower part only): New Jersey, New York"
|
||||
@.57="Rio Grande: New Mexico, Texas, Tamaulipas (Mexico), Nuevo Leon (Mexico), Coahuila De Zaragoza (Mexico), Chihuahua (Mexico)"
|
||||
@.58="Niagara River: New York, Ontario (Canada)"
|
||||
@.59="St. Lawrence River: New York, Ontario (Canada)"
|
||||
@.60="Poultney River: New York, Vermont"
|
||||
@.61="Catawba River: North Carolina, South Carolina"
|
||||
@.62="Blackwater River: North Carolina, Virginia"
|
||||
@.63="Columbia River: Oregon, Washington"
|
||||
@.1 = " Rivers that form part of a (USA) state's border " /*this value is adjusted later to include a prefix & suffix.*/
|
||||
@.2 = '=' /*this value is expanded later. */
|
||||
@.3 = "Perdido River Alabama, Florida"
|
||||
@.4 = "Chattahoochee River Alabama, Georgia"
|
||||
@.5 = "Tennessee River Alabama, Kentucky, Mississippi, Tennessee"
|
||||
@.6 = "Colorado River Arizona, California, Nevada, Baja California (Mexico)"
|
||||
@.7 = "Mississippi River Arkansas, Illinois, Iowa, Kentucky, Minnesota, Mississippi, Missouri, Tennessee, Louisiana, Wisconsin"
|
||||
@.8 = "St. Francis River Arkansas, Missouri"
|
||||
@.9 = "Poteau River Arkansas, Oklahoma"
|
||||
@.10 = "Arkansas River Arkansas, Oklahoma"
|
||||
@.11 = "Red River (Mississippi watershed) Arkansas, Oklahoma, Texas"
|
||||
@.12 = "Byram River Connecticut, New York"
|
||||
@.13 = "Pawcatuck River Connecticut, Rhode Island and Providence Plantations"
|
||||
@.14 = "Delaware River Delaware, New Jersey, New York, Pennsylvania"
|
||||
@.15 = "Potomac River District of Columbia, Maryland, Virginia, West Virginia"
|
||||
@.16 = "St. Marys River Florida, Georgia"
|
||||
@.17 = "Chattooga River Georgia, South Carolina"
|
||||
@.18 = "Tugaloo River Georgia, South Carolina"
|
||||
@.19 = "Savannah River Georgia, South Carolina"
|
||||
@.20 = "Snake River Idaho, Oregon, Washington"
|
||||
@.21 = "Wabash River Illinois, Indiana"
|
||||
@.22 = "Ohio River Illinois, Indiana, Kentucky, Ohio, West Virginia"
|
||||
@.23 = "Great Miami River (mouth only) Indiana, Ohio"
|
||||
@.24 = "Des Moines River Iowa, Missouri"
|
||||
@.25 = "Big Sioux River Iowa, South Dakota"
|
||||
@.26 = "Missouri River Kansas, Iowa, Missouri, Nebraska, South Dakota"
|
||||
@.27 = "Tug Fork River Kentucky, Virginia, West Virginia"
|
||||
@.28 = "Big Sandy River Kentucky, West Virginia"
|
||||
@.29 = "Pearl River Louisiana, Mississippi"
|
||||
@.30 = "Sabine River Louisiana, Texas"
|
||||
@.31 = "Monument Creek Maine, New Brunswick (Canada)"
|
||||
@.32 = "St. Croix River Maine, New Brunswick (Canada)"
|
||||
@.33 = "Piscataqua River Maine, New Hampshire"
|
||||
@.34 = "St. Francis River Maine, Quebec (Canada)"
|
||||
@.35 = "St. John River Maine, Quebec (Canada)"
|
||||
@.36 = "Pocomoke River Maryland, Virginia"
|
||||
@.37 = "Palmer River Massachusetts, Rhode Island and Providence Plantations"
|
||||
@.38 = "Runnins River Massachusetts, Rhode Island and Providence Plantations"
|
||||
@.39 = "Montreal River Michigan (upper peninsula), Wisconsin"
|
||||
@.40 = "Detroit River Michigan, Ontario (Canada)"
|
||||
@.41 = "St. Clair River Michigan, Ontario (Canada)"
|
||||
@.42 = "St. Marys River Michigan, Ontario (Canada)"
|
||||
@.43 = "Brule River Michigan, Wisconsin"
|
||||
@.44 = "Menominee River Michigan, Wisconsin"
|
||||
@.45 = "Red River of the North Minnesota, North Dakota"
|
||||
@.46 = "Bois de Sioux River Minnesota, North Dakota, South Dakota"
|
||||
@.47 = "Pigeon River Minnesota, Ontario (Canada)"
|
||||
@.48 = "Rainy River Minnesota, Ontario (Canada)"
|
||||
@.49 = "St. Croix River Minnesota, Wisconsin"
|
||||
@.50 = "St. Louis River Minnesota, Wisconsin"
|
||||
@.51 = "Halls Stream New Hampshire, Canada"
|
||||
@.52 = "Salmon Falls River New Hampshire, Maine"
|
||||
@.53 = "Connecticut River New Hampshire, Vermont"
|
||||
@.54 = "Arthur Kill New Jersey, New York (tidal strait)"
|
||||
@.55 = "Kill Van Kull New Jersey, New York (tidal strait)"
|
||||
@.56 = "Hudson River (lower part only) New Jersey, New York"
|
||||
@.57 = "Rio Grande New Mexico, Texas, Tamaulipas (Mexico), Nuevo Leon (Mexico), Coahuila de Zaragoza (Mexico), Chihuahua (Mexico)"
|
||||
@.58 = "Niagara River New York, Ontario (Canada)"
|
||||
@.59 = "St. Lawrence River New York, Ontario (Canada)"
|
||||
@.60 = "Poultney River New York, Vermont"
|
||||
@.61 = "Catawba River North Carolina, South Carolina"
|
||||
@.62 = "Blackwater River North Carolina, Virginia"
|
||||
@.63 = "Columbia River Oregon, Washington"
|
||||
|
||||
do highItem=1 while @.highItem\=='' /*find how many entries, and also*/
|
||||
maxL=max(maxL,length(@.highItem)) /* find the maximum width entry.*/
|
||||
end /*highItem*/
|
||||
|
||||
highItem=highItem-1 /*adjust highItem slightly. */
|
||||
@.1=centre(@.1,maxL,'-') /*adjust the header information. */
|
||||
@.2=copies(@.2,maxL) /*adjust the header separator. */
|
||||
do #=1 while @.#\=='' /*find how many entries, and also*/
|
||||
maxL=max(maxL, length(@.#)) /* find the maximum width entry.*/
|
||||
end /*#*/
|
||||
#=#-1 /*adjust the highest element #. */
|
||||
@.1=centre(@.1, maxL, '-') /*adjust the header information. */
|
||||
@.2=copies(@.2, maxL) /*adjust the header separator. */
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: widthH=length(highItem) /*maximum width of any line. */
|
||||
|
||||
do j=1 for highItem /*display each item in the array.*/
|
||||
say 'element' right(j,widthH) arg(1)':' @.j
|
||||
end /*j*/
|
||||
|
||||
say copies('█',maxL+widthH+22) /*display a separator line. */
|
||||
show@: widthH=length(#) /*maximum width of any line. */
|
||||
do j=1 for # /*display each item in the array.*/
|
||||
say 'element' right(j,widthH) arg(1)':' @.j
|
||||
end /*j*/
|
||||
say copies('▒', maxL + widthH + 22) /*display a separator line. */
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
#lang racket
|
||||
(define (quicksort a-list (compare <))
|
||||
(match a-list
|
||||
((list)
|
||||
(list))
|
||||
((cons x xs)
|
||||
(append (quicksort (filter (lambda (element) (compare element x)) xs) compare)
|
||||
(list x)
|
||||
(quicksort (filter (lambda (element) (not (compare element x))) xs) compare)))))
|
||||
(define (quicksort < l)
|
||||
(match l
|
||||
['() '()]
|
||||
[(cons x xs)
|
||||
(let-values ([(xs-gte xs-lt) (partition (curry < x) xs)])
|
||||
(append (quicksort < xs-lt)
|
||||
(list x)
|
||||
(quicksort < xs-gte)))]))
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
(quicksort '(8 7 5 6 4 3 2))
|
||||
(quicksort < '(8 7 3 6 4 5 2))
|
||||
;returns '(2 3 4 5 6 7 8)
|
||||
(quicksort '("Quicksort" "Mergesort" "Bubblesort") string<?)
|
||||
(quicksort string<? '("Mergesort" "Quicksort" "Bubblesort"))
|
||||
;returns '("Bubblesort" "Mergesort" "Quicksort")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
class Array
|
||||
def quick_sort
|
||||
h, *t = self
|
||||
h ? t.partition { |e| e < h }.inject { |l, r| l.quick_sort + [h] + r.quick_sort } : []
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
// We use in place quick sort
|
||||
// For details see http://en.wikipedia.org/wiki/Quicksort#In-place_version
|
||||
fn quick_sort<T: Ord>(v: &mut[T]) {
|
||||
let len = v.len();
|
||||
if len < 2 {
|
||||
return;
|
||||
}
|
||||
|
||||
let pivot_index = partition(v);
|
||||
|
||||
// Sort the left side
|
||||
quick_sort(v.mut_slice(0, pivot_index));
|
||||
|
||||
// Sort the right side
|
||||
quick_sort(v.mut_slice(pivot_index + 1, len));
|
||||
}
|
||||
|
||||
// Reorders the slice with values lower than the pivot at the left side,
|
||||
// and values bigger than it at the right side.
|
||||
// Also returns the store index.
|
||||
fn partition<T: Ord>(v: &mut [T]) -> uint {
|
||||
let len = v.len();
|
||||
let pivot_index = len / 2;
|
||||
|
||||
v.swap(pivot_index, len - 1);
|
||||
|
||||
let mut store_index = 0;
|
||||
for i in range(0, len - 1) {
|
||||
if v[i] <= v[len - 1] {
|
||||
v.swap(i, store_index);
|
||||
store_index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
v.swap(store_index, len - 1);
|
||||
store_index
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Sort numbers
|
||||
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
|
||||
println!("Before: {}", numbers.as_slice());
|
||||
|
||||
quick_sort(numbers);
|
||||
println!("After: {}", numbers.as_slice());
|
||||
|
||||
// Sort strings
|
||||
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
|
||||
println!("Before: {}", strings.as_slice());
|
||||
|
||||
quick_sort(strings);
|
||||
println!("After: {}", strings.as_slice());
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
def quicksortInt(list: List[Int]): List[Int] = list match {
|
||||
case List(head) => list
|
||||
case head :: tail =>
|
||||
val (smaller, bigger) = tail partition (_ < head)
|
||||
quicksortInt(smaller) ::: head :: quicksortInt(bigger)
|
||||
case list => list
|
||||
case _ => list
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue