JortSort: Difference between revisions
Added Easylang
m (→{{header|J}}: clarify the possibilities a little) |
(Added Easylang) |
||
(8 intermediate revisions by 7 users not shown) | |||
Line 23:
{{trans|Python}}
<
R Array(sequence) == sorted(sequence)
Line 33:
print_for_seq([‘a’, ‘c’])
print_for_seq(‘CVGH’)
print_for_seq(‘PQRST’)</
{{out}}
Line 46:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program jortSort64.s */
Line 179:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
INT i
Line 252:
Test(d,1)
Test(d,0)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/JortSort.png Screenshot from Atari 8-bit computer]
Line 265:
=={{header|Ada}}==
<
procedure Jortsort is
Line 282:
Put_Line("""abbigail"" sorted: " & Boolean'Image(Jort_Sort("abbigail")));
Put_Line("""abbey"" sorted: " & Boolean'Image(Jort_Sort("abbey")));
end Jortsort;</
{{out}}
Line 290:
=={{header|AppleScript}}==
<
use framework "Foundation"
use scripting additions
Line 346:
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort</
{{Out}}
<pre>{false, true}</pre>
Line 352:
=={{header|Arturo}}==
<
a = sort a
Line 364:
test ["a" "b" "c"]
test ["c" "a" "b"]</
{{out}}
Line 375:
=={{header|AutoHotkey}}==
<
sorted:=[]
for index, val in Array
Line 383:
return 0
return 1
}</
Examples:<
Array2 := ["a", "b", "c" , "d"]
MsgBox % JortSort(Array1) "`n" JortSort(Array2)
return</
Outputs:<pre>0
1</pre>
Line 394:
BQN has a really simple way to represent JortSort:
<
JSort 3‿2‿1
0</
Which means "Does the array match itself sorted?"
Line 408:
As others did in this page, this example doesn't follow the request to sort the input array and then compare the sorted version to the original one to check if it was already sorted. It only checks if the input is sorted and behaves accordingly.
I've tested this code only with gcc but should work with any C compiler.
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 464:
}
</syntaxhighlight>
=={{header|C sharp|C#}}==
{{trans|JavaScript}}
<
class Program
Line 489:
return true;
}
}</
=={{header|C++}}==
<
#include <algorithm>
#include <string>
Line 555:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 566:
=={{header|Clojure}}==
<
=={{header|Common Lisp}}==
<
(defun jort-sort (x)
(equalp x (sort (copy-seq x) #'<)))
</syntaxhighlight>
=={{header|Crystal}}==
<
array == array.sort
end</
=={{header|D}}==
<syntaxhighlight lang="d">
module jortsort;
Line 597:
assert(!jortSort(["two", "one", "three"]));
}
</syntaxhighlight>
=={{header|Delphi}}==
{{Trans|JavaScript}}
<syntaxhighlight lang="delphi">
program JortSort;
Line 648:
Readln;
end.
</syntaxhighlight>
=={{header|EasyLang}}==
<syntaxhighlight>
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
func jortsort arr[] .
orig[] = arr[]
sort arr[]
return if orig[] = arr[]
.
print jortsort [ 2 4 6 ]
print jortsort [ 4 2 6 ]
</syntaxhighlight>
{{out}}
<pre>
1
0
</pre>
=={{header|Elixir}}==
<
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(2)> jortsort.([1,2,3,4])
true
iex(3)> jortsort.([1,2,5,4])
false</
=={{header|F_Sharp|F#}}==
<
let jortSort n=n=Array.sort n
printfn "%A %A" (jortSort [|1;23;42|]) (jortSort [|23;42;1|])
</syntaxhighlight>
{{out}}
<pre>
Line 669 ⟶ 694:
</pre>
=={{header|Factor}}==
<
: jortsort ( seq -- ? ) dup natural-sort = ;</
=={{header|FreeBASIC}}==
<
' Although it's possible to create generic sorting routines using macros in FreeBASIC
Line 734 ⟶ 759:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 744 ⟶ 769:
=={{header|Go}}==
<syntaxhighlight lang="go">
package main
Line 773 ⟶ 798:
}
</syntaxhighlight>
=={{header|Haskell}}==
For lists:
<
jortSort :: (Ord a) => [a] -> Bool
jortSort list = list == sort list</
or in applicative terms:
<
jortSort
Line 793 ⟶ 818:
--------------------------- TEST ---------------------------
main :: IO ()
main = print $ jortSort <$> [[4, 5, 1, 3, 2], [1, 2, 3, 4, 5]]</
{{Out}}
<pre>[False,True]</pre>
Line 801 ⟶ 826:
'''Solution'''
<
More in line with the spirit of the task would be:
<
Of course, ideally, assert would be replaced with something more assertive. Perhaps deleting all storage? But even better would be to send email to your boss and leadership explaining (at great length) exactly why they are all idiots. Do enough of this and you will never have to sort again...
'''Example Usage'''
<
0
jortsort 'sux'
1
jortsort&> 1 2 4 3;14 6 8;1 3 8 19;'ac';'sux';'CVGH';'PQRST'
0 0 1 1 1 0 1</
Using jortSort in place of jortsort would throw an error on all but the first of those examples. It would return an empty result for that first example. (We have no way of representing the consequences of the more inane proposals presented here.)
=={{header|Janet}}==
<
(deep= xs (sorted xs)))
(print (jortsort @[1 2 3 4 5])) # true
(print (jortsort @[2 1 3 4 5])) # false</
=={{header|Java}}==
Optimized version of JortSort. Even less funny. Doesn't bother with sorting, but simply returns ''true''. Very fast. Use only when you're ''absolutely sure'' that the input is already sorted. You may have to use an unoptimized version of JortSort to ascertain this.
<
public static void main(String[] args) {
System.out.println(jortSort(new int[]{1, 2, 3}));
Line 836 ⟶ 861:
return true;
}
}</
<pre>true</pre>
Line 843 ⟶ 868:
The original JavaScript implementation courtesy of the author, [https://github.com/jennschiffer/jortsort Jenn "Moneydollars" Schiffer].
<
// sort the array
Line 855 ⟶ 880:
return true;
};</
=={{header|jq}}==
<
'''Example''':
<
{{Out}}
<syntaxhighlight lang
=={{header|Jsish}}==
Based on the Javascript satire.
<
var jortSort = function(arr:array):boolean {
// make a copy
Line 894 ⟶ 919:
jortSort(['snort', 'sort', 1, [1,2], {1:2}]) ==> false
=!EXPECTEND!=
*/</
{{out}}
Line 901 ⟶ 926:
=={{header|Julia}}==
<
jortsort(A) = sort(A) == A
</syntaxhighlight>
{{out}}
Line 922 ⟶ 947:
=={{header|K}}==
<syntaxhighlight lang="text">jortsort:{x~x@<x}</
'''Example''':
<syntaxhighlight lang="text">jortsort 1 2 3</
<pre>1
</pre>
=={{header|Kotlin}}==
<
fun <T> jortSort(a: Array<T>): Boolean {
Line 953 ⟶ 978:
val d = arrayOf('C', 'D', 'A', 'E', 'B')
printResults(d)
}</
{{out}}
Line 965 ⟶ 990:
=={{header|Lua}}==
<
local new = {}
for k, v in pairs(t) do new[k] = v end
Line 978 ⟶ 1,003:
end
return true
end</
=={{header|Maple}}==
<
local copy:
copy := sort(Array([seq(arr[i], i=1..numelems(arr))])):
Line 989 ⟶ 1,014:
jortSort(Array([5,6,7,2,1]));
jortSort(Array([-5,0,7,12,21]));
jortSort(Array(StringTools:-Explode("abcdefg")));</
{{Out|Output}}
<pre>false
Line 996 ⟶ 1,021:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Print[jortSort[Range[100]]];
Print[jortSort[RandomSample[Range[100]]]];</
{{out}}
<pre>True
Line 1,004 ⟶ 1,029:
=={{header|Nim}}==
<
func jortSort[T](a: openArray[T]): bool =
Line 1,016 ⟶ 1,041:
echo ""
test(['a', 'b', 'c'])
test(['c', 'a', 'b'])</
{{out}}
Line 1,026 ⟶ 1,051:
=={{header|Objeck}}==
<
sorted := CompareVector->New(elems);
sorted->Sort();
Line 1,037 ⟶ 1,062:
return true;
}</
=={{header|OCaml}}==
For lists:
<
lst = List.sort compare lst</
For arrays:
<
let originalArray = Array.copy ary in
Array.sort compare ary;
originalArray = ary</
=={{header|Oforth}}==
<
{{out}}
Line 1,061 ⟶ 1,086:
=={{header|ooRexx}}==
{{trans|REXX}}
<
/*---------------------------------------------------------------------
* Determine if list is sorted
Line 1,072 ⟶ 1,097:
End
Return (i=words(list)+1)|(list='')
</syntaxhighlight>
=={{header|PARI/GP}}==
<
=={{header|Perl}}==
<
my @s=sort @_; # Default standard string comparison
for (0..$#s) {
Line 1,084 ⟶ 1,109:
}
1;
}</
The task wants us to sort, but we could implement this by just using <tt>cmp</tt> on the input array elements, which would be faster (especially with unsorted input).
=={{header|Phix}}==
<!--<
<span style="color: #008080;">type</span> <span style="color: #000000;">JortSort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">type</span>
<!--</
Then any variable or constant delared as type JortSort raises an error if used incorrectly, eg
<!--<
<span style="color: #000000;">JortSort</span> <span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">JortSort</span> <span style="color: #000000;">bad</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}</span>
<!--</
{{out}}
Note that while the above is fine under pwa/p2js and can be invoked explicitly, it does not automatically trigger the error.
Line 1,105 ⟶ 1,130:
</pre>
Amusingly the compiler itself uses a variant of jortsort, in that pttree.e declares a whole bunch of ternary tree node constants for all the language keywords and builtins such as
<!--<
<span style="color: #008080;">global</span> <span style="color: #008080;">constant</span> <span style="color: #000000;">T_while</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">336</span> <span style="color: #000000;">tt_stringF</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"while"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">T_while</span><span style="color: #0000FF;">)</span>
<!--</
and if you change that to 338 and try to recompile the compiler, you'll immediately get:
<pre>
Line 1,114 ⟶ 1,139:
It does that because at the lowest level a cmp imm is at least twice as fast as a cmp [mem], and the only other way it could know these constants at compile-time would be to (re)build a 5000-node ternary tree, though I will concede that any sane person would have written a program to write an include file rather than hacking these things by hand.<br>
There is a similar thing in pwa\src\p2js_keywords.e, though it automatically checks, prompts, and auto-rebuilds that for you.
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
List = [
[1,2,3,4,5],
[2,3,4,5,1],
[2],
"jortsort",
"jortsort".sort()
],
foreach(L in List)
printf("%w: ", L),
if not jortsort(L) then
print("not ")
end,
println("sorted")
end,
nl.
jortsort(X) => X == X.sort().</syntaxhighlight>
{{out}}
<pre>[1,2,3,4,5]: sorted
[2,3,4,5,1]: not sorted
[2]: sorted
jortsort: not sorted
joorrstt: sorted</pre>
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
(de jortSort (L) (= L (sort L)))
(jortSort (1 2 3))
</syntaxhighlight>
{{out}}
T
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function jortsort($a) { -not (Compare-Object $a ($a | sort) -SyncWindow 0)}
jortsort @(1,2,3)
jortsort @(2,3,1)
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 1,136 ⟶ 1,188:
=={{header|PureBasic}}==
<
If OpenConsole()
Print("[ ") : ForEach liste : Print(liste+Space(1)) : Next : Print("] = ")
Line 1,155 ⟶ 1,207:
For i=1 To 10 : AddElement(l1()) : l1()=Chr(Random(90,65)) : Next
isSort(l1()) : SortList(l1(),#PB_Sort_Ascending) : isSort(l1())
Input()</
{{out}}
<pre>[ A Z Q G B N E B G Y ] = False
Line 1,161 ⟶ 1,213:
=={{header|Python}}==
<
return list(sequence) == sorted(sequence)
>>> for data in [(1,2,4,3), (14,6,8), ['a', 'c'], ['s', 'u', 'x'], 'CVGH', 'PQRST']:
Line 1,171 ⟶ 1,223:
jortsort('CVGH') is False
jortsort('PQRST') is True
>>> </
=={{header|Quackery}}==
Line 1,179 ⟶ 1,231:
<code>jortsortwith</code> returns <code>true</code> if the nest is sorted, sort order is specified after <code>jortsortwith</code>, so <code>jortsortwith ></code> returns <code>true</code> if a nest of numbers is sorted in ascending numerical order, and <code>jortsortwith $<</code> returns <code>true</code> if a nest of strings is sorted in descending lexicographical order.
<
' [ sortwith ]
]'[ nested join
do = ] is jortsortwith ( [ --> b )</
===The Non-satirical version===
Line 1,188 ⟶ 1,240:
Jortsort, as specified in this task, is a sensible function ("is this nest sorted?) done inefficiently. This is the efficient version, with a more sensible name.
<
]'[ temp put
dup [] != if
Line 1,195 ⟶ 1,247:
[ dip not conclude ] ] ]
drop
temp release ] is sortedwith ( [ --> b )</
=={{header|Racket}}==
<
(define (jort-sort l [<? <])
(equal? l (sort l <?)))</
Racket's <tt>sort</tt> function is efficient in that it starts by
checking the input, so the above could be made more efficient with a
pointer equality test:
<
(define (jort-sort l [<? <])
(eq? l (sort l <?)))</
And an explicit implementation that checks the order (note that Racket's
<tt>sort</tt> expects a “smaller-than” comparator):
<
(define (jort-sort l [<? <])
(or (null? l)
(for/and ([x (in-list l)] [y (in-list (cdr l))])
(not (<? y x))))) ; same as (<= x y) but using only <?</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
Actually, there's a better internal sort that seems to work best for lists that are already completely sorted, but tends to fails for any other list. The name of this sort, <tt>[!after]</tt>, is completely opaque, so we're pretty much forced to hide it inside a subroutine to prevent widespread panic.
<syntaxhighlight lang="raku"
However, since Perl 6 has a really good inliner, there's really little point anyway in using the <tt>[!after]</tt> reduction operator directly, and <tt>jort-sort-more-better-sorta</tt> is really much more self-documenting, so please don't use the reduction operator if you can. For example:
{{out}}
Line 1,236 ⟶ 1,288:
The array elements (items) may be any form of number that REXX supports, and/or they can be alphabetic characters.
===using sort===
<
parse arg $ /*obtain the list of numbers from C.L. */
if $='' then $=1 2 4 3 /*Not specified? Then use the default.*/
Line 1,261 ⟶ 1,313:
if !.k\==@.k then return 0 /*the array isn't sorted. */
end /*k*/
return 1 /*the array is sorted. */</
'''output''' when using the default input: <tt> 1 2 4 3 </tt>
<pre>
Line 1,283 ⟶ 1,335:
Nothing is mentioned how it does this, and it certainly doesn't say that it sorts the input to verify if it's in order.
<
parse arg $ /*obtain the list of numbers from C.L. */
if $='' then $=1 2 4 3 /*Not specified? Then use the default.*/
Line 1,297 ⟶ 1,349:
p=_
end /*j*/
return 1 /*array is sorted.*/</
'''output''' is the same as the 1<sup>st</sup> REXX version. <br><br>
=={{header|Ring}}==
<
aList = [4,2,3,1]
see jortSort(aList) + nl
Line 1,312 ⟶ 1,364:
next
return true
</syntaxhighlight>
=={{header|Ruby}}==
<
array == array.sort
end</
{{trans|JavaScript}}
<
# sort the array
original_array = array.dup
Line 1,331 ⟶ 1,383:
true
end</
=={{header|Rust}}==
{{trans|JavaScript}}
<
fn jort_sort<T: Ord + Eq + Clone>(array: Vec<T>) -> bool {
Line 1,350 ⟶ 1,402:
return true;
}</
'''Using iterators:'''
<
fn jort_sort<T>(slice: &[T]) -> bool
where
Line 1,366 ⟶ 1,418:
.zip(sorted.iter())
.all(|(orig, sorted)| orig == sorted)
}</
'''Idiomatic:'''
<
fn jort_sort<T>(slice: &[T]) -> bool
where
Line 1,379 ⟶ 1,431:
slice == sorted
}</
=={{header|Scala}}==
Line 1,385 ⟶ 1,437:
The Java deepEquals() method will work as a replacement.
<
def jortSort[K:Ordering]( a:Array[K] ) = deepEquals(a.sorted, a)</
=={{header|Sidef}}==
<
=={{header|SSEM}}==
Line 1,398 ⟶ 1,450:
There are a couple of limitations that make the program less useful than it would otherwise be. Firstly, it is essentially a single-shot application: if you want to test a second array, you will need to manually reset storage address 0 to 16411 and storage address 26 to 0. Secondly, the SSEM's modest storage capacity means that the largest array you can sort (or not sort) using this program consists of (the terminating zero, and) four integers. Subject to those provisos, however, the program should be found to meet the specification satisfactorily.
<
00000000000000110000000000000000 1. Test
11101000000000000000000000000000 2. 23 to CI
Line 1,422 ⟶ 1,474:
11111111111111111111111111111111 22. -1
00001000000000000000000000000000 23. 16
01001000000000000000000000000000 24. 18</
=={{header|Swift}}==
<
return array == sorted(array)
}</
{{trans|JavaScript}}
<
// sort the array
Line 1,442 ⟶ 1,494:
return true
}</
=={{header|Tcl}}==
<
proc jortsort {args} {
set list [lindex $args end]
Line 1,453 ⟶ 1,505:
expr {[lsort {*}$options $list] eq $list}
}
</syntaxhighlight>
This supports all of the options known to the native '''lsort''' command, making it quite natural to use. The commented line ensures it will do the right thing for any list, even if it has funny formatting because it's embedded in source:
<pre>
Line 1,469 ⟶ 1,521:
{{works with|Bourne Again SHell}}
<syntaxhighlight lang="sh">
JortSort() {
cmp -s <(printf “%s\n” “$@“) <(printf “%s\n” “$@“ | sort)
Line 1,486 ⟶ 1,538:
JortSortVerbose a b c
JortSortVerbose c a b
</syntaxhighlight>
{{Out}}
<pre>True
Line 1,494 ⟶ 1,546:
=={{header|VBScript}}==
<
JortSort = True
arrPreSort = Split(s,",")
Line 1,518 ⟶ 1,570:
WScript.StdOut.Write JortSort("a,b,c")
WScript.StdOut.WriteLine
WScript.StdOut.Write JortSort("a,c,b")</
{{out}}
<pre>True
Line 1,524 ⟶ 1,576:
True
False</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">
fn main() {
println(jort_sort([1, 2, 1, 11, 213, 2, 4])) //false
println(jort_sort([0, 1, 0, 0, 0, 0])) //false
println(jort_sort([1, 2, 4, 11, 22, 22])) //true
println(jort_sort([0, 0, 0, 1, 2, 2])) //true
}
fn jort_sort(a []int) bool {
mut c := a.clone()
c.sort()
for k, v in c {
if v == a[k] {
continue
} else {
return false
}
}
return true
}</syntaxhighlight>
{{out}}
<pre>
false
false
true
true
</pre>
=={{header|Wren}}==
{{libheader|Wren-sort}}
<
var jortSort = Fn.new { |a|
Line 1,538 ⟶ 1,621:
var tests = [ [1, 2, 3, 4, 5], [2, 1, 3, 4, 5] ]
for (test in tests) System.print("%(test) -> %(jortSort.call(test) ? "sorted" : "not sorted")")</
{{out}}
<pre>
[1, 2, 3, 4, 5] -> sorted
[2, 1, 3, 4, 5] -> not sorted
</pre>
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib; \for Sort
func JortSort(A, N);
int A, N, B, I;
def SizeOfInt = 4;
[B:= Reserve(N*SizeOfInt);
for I:= 0 to N-1 do B(I):= A(I);
Sort(B, N);
for I:= 0 to N-1 do
if B(I) # A(I) then return false;
return true;
];
int Tests, Test, I;
def Size = 5;
[Tests:= [ [1, 2, 3, 4, 5], [2, 1, 3, 4, 5] ];
for Test:= 0 to 2-1 do
[ChOut(0, ^[);
for I:= 0 to Size-2 do
[IntOut(0, Tests(Test,I)); Text(0, ", ")];
IntOut(0, Tests(Test,I));
Text(0, "] -> ");
Text(0, if JortSort(Tests(Test), Size) then "sorted" else "not sorted");
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
Line 1,548 ⟶ 1,665:
=={{header|zkl}}==
Two "solutions", a linear one and one that actually sorts.
<
<
{{out}}
<pre>
|