Sorting algorithms/Tree sort on a linked list: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 19: Line 19:
=={{header|ATS}}==
=={{header|ATS}}==


<syntaxhighlight lang="ats">(*
<lang ATS>(*


Tree sort based on the algorithm at http://archive.today/WM83M
Tree sort based on the algorithm at http://archive.today/WM83M
Line 560: Line 560:
println! (dllist2list<int> lst)
println! (dllist2list<int> lst)
end
end
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 576: Line 576:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
Line 686: Line 686:
list_destroy(&list);
list_destroy(&list);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 696: Line 696:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
{{trans|Yabasic}}
<lang freebasic>#define key 0
<syntaxhighlight lang="freebasic">#define key 0
#define izda 1
#define izda 1
#define dcha 2
#define dcha 2
Line 762: Line 762:
showTree(1)
showTree(1)
Print
Print
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>eight five four nine one seven six ten three two</pre>
<pre>eight five four nine one seven six ten three two</pre>
Line 768: Line 768:
=={{header|Go}}==
=={{header|Go}}==
This is based on the Kotlin entry but has been adjusted to satisfy the revised task description.
This is based on the Kotlin entry but has been adjusted to satisfy the revised task description.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 842: Line 842:
lls2 := treeSort(ll2)
lls2 := treeSort(ll2)
printLinkedList(lls2, "%c", true)
printLinkedList(lls2, "%c", true)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 857: Line 857:
Implementation of doubly-linked list is given here [[Doubly-linked_list/Traversal#Haskell]]
Implementation of doubly-linked list is given here [[Doubly-linked_list/Traversal#Haskell]]


<lang haskell>{-# language DeriveFoldable #-}
<syntaxhighlight lang="haskell">{-# language DeriveFoldable #-}
import Data.Foldable
import Data.Foldable


Line 895: Line 895:


treeSort :: (Foldable t, Ord a) => t a -> [a]
treeSort :: (Foldable t, Ord a) => t a -> [a]
treeSort = toList . mkTree</lang>
treeSort = toList . mkTree</syntaxhighlight>


<pre>λ> let l = mkDList [2,4,3,5,7,6,2,9]
<pre>λ> let l = mkDList [2,4,3,5,7,6,2,9]
Line 932: Line 932:
With these choices, the task becomes:
With these choices, the task becomes:


<lang J> finn=: fread '~user/temp/wake/finneganswake.txt'
<syntaxhighlight lang="j"> finn=: fread '~user/temp/wake/finneganswake.txt'
sentences=: (<;.2~ '. '&E.@rplc&(LF,' !.?.')) finn
sentences=: (<;.2~ '. '&E.@rplc&(LF,' !.?.')) finn
#sentences
#sentences
14961
14961
+/<:#@>C./:sentences
+/<:#@>C./:sentences
14945</lang>
14945</syntaxhighlight>


We have to swap almost every sentence, but 16 of them can be sorted "for free" with the swaps of the other sentences.
We have to swap almost every sentence, but 16 of them can be sorted "for free" with the swaps of the other sentences.
Line 943: Line 943:
For that matter, inspecting the lengths of the cycles formed by the minimal arrangements of swaps...
For that matter, inspecting the lengths of the cycles formed by the minimal arrangements of swaps...


<lang J> /:~ #@>C./:sentences
<syntaxhighlight lang="j"> /:~ #@>C./:sentences
1 1 2 2 4 9 12 25 32 154 177 570 846 935 1314 10877</lang>
1 1 2 2 4 9 12 25 32 154 177 570 846 935 1314 10877</syntaxhighlight>


... we can see that two of the sentences were fine right where they start out. Let's see what they are:
... we can see that two of the sentences were fine right where they start out. Let's see what they are:


<lang J> ;:inv (#~(= /:~))sentences
<syntaxhighlight lang="j"> ;:inv (#~(= /:~))sentences
Very, all
Very, all
fourlike tellt. What tyronte power!</lang>
fourlike tellt. What tyronte power!</syntaxhighlight>


So now you know.
So now you know.
Line 960: Line 960:
Anyways, here we go:
Anyways, here we go:


<lang J>left=: i.0
<syntaxhighlight lang="j">left=: i.0
right=: i.0
right=: i.0
data=: i.0
data=: i.0
Line 992: Line 992:
if. y>:#data do.'' return. end.
if. y>:#data do.'' return. end.
(extract y{left),(y{data),extract y{right
(extract y{left),(y{data),extract y{right
)</lang>
)</syntaxhighlight>


This could be wrapped differently, but it's adequate for this task.
This could be wrapped differently, but it's adequate for this task.


Example use would be something like:
Example use would be something like:
<lang j> insert sentences
<syntaxhighlight lang="j"> insert sentences
extract''</lang>
extract''</syntaxhighlight>


But task's the current url for Finnegan's Wake does not point at flat text and constructing such a thing would be a different task...
But task's the current url for Finnegan's Wake does not point at flat text and constructing such a thing would be a different task...


=={{header|Java}}==
=={{header|Java}}==
<lang java>// TreeSortTest.java
<syntaxhighlight lang="java">// TreeSortTest.java
import java.util.*;
import java.util.*;


Line 1,035: Line 1,035:
System.out.println(" after sort: " + list);
System.out.println(" after sort: " + list);
}
}
}</lang>
}</syntaxhighlight>


<lang java>// LinkedList.java
<syntaxhighlight lang="java">// LinkedList.java


// Java provides a doubly-linked list implementation but it doesn't permit
// Java provides a doubly-linked list implementation but it doesn't permit
Line 1,133: Line 1,133:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,145: Line 1,145:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>mutable struct BTree{T}
<syntaxhighlight lang="julia">mutable struct BTree{T}
data::T
data::T
left::Union{BTree, Nothing}
left::Union{BTree, Nothing}
Line 1,188: Line 1,188:


testtreesort(rand(1:99, 12))
testtreesort(rand(1:99, 12))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Unsorted: [1, 12, 15, 22, 28, 26, 69, 22, 1, 62, 73, 95]
Unsorted: [1, 12, 15, 22, 28, 26, 69, 22, 1, 62, 73, 95]
Line 1,196: Line 1,196:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
As I can't be bothered to download Finnegan's Wake and deal with the ensuing uncertainties, I've contented myself by following a similar approach to the Racket and Scheme entries:
As I can't be bothered to download Finnegan's Wake and deal with the ensuing uncertainties, I've contented myself by following a similar approach to the Racket and Scheme entries:
<lang scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


import java.util.LinkedList
import java.util.LinkedList
Line 1,240: Line 1,240:
val ll2 = LinkedList(listOf('d', 'c', 'e', 'b' , 'a'))
val ll2 = LinkedList(listOf('d', 'c', 'e', 'b' , 'a'))
ll2.treeSort()
ll2.treeSort()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,253: Line 1,253:
As Nim standard library provides a doubly linked list implementation which allows to access to the "prev" and "next" fields, we used it. So, we had only to write the transformation from list to tree and conversely.
As Nim standard library provides a doubly linked list implementation which allows to access to the "prev" and "next" fields, we used it. So, we had only to write the transformation from list to tree and conversely.


<lang Nim>import lists, random
<syntaxhighlight lang="nim">import lists, random




Line 1,297: Line 1,297:
list2.append(s)
list2.append(s)
echo "Before sort: ", list2
echo "Before sort: ", list2
echo "After sort: ", list2.treeSort()</lang>
echo "After sort: ", list2.treeSort()</syntaxhighlight>


{{out}}
{{out}}
Line 1,309: Line 1,309:
Ol has builtin sorted key-value trees named "ff". We converting list into ff and back again as already sorted list. Only values (small integers, constants) and symbols are allowed.
Ol has builtin sorted key-value trees named "ff". We converting list into ff and back again as already sorted list. Only values (small integers, constants) and symbols are allowed.


<lang scheme>
<syntaxhighlight lang="scheme">
(define (tree-sort l)
(define (tree-sort l)
(map car (ff->list
(map car (ff->list
Line 1,317: Line 1,317:


(print (tree-sort '(5 3 7 9 1)))
(print (tree-sort '(5 3 7 9 1)))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,326: Line 1,326:
=== version 1 ===
=== version 1 ===
{{trans|Kotlin}}
{{trans|Kotlin}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,362: Line 1,362:
<span style="color: #000000;">treeSort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">treeSort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">treeSort</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dceba"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">treeSort</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dceba"</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,371: Line 1,371:
=== version 2 ===
=== version 2 ===
Following my idea of a revised task description, see talk page.
Following my idea of a revised task description, see talk page.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,466: Line 1,466:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dceba"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dceba"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"d"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"c"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"e"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"b"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"d"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"c"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"e"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"b"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,483: Line 1,483:
{{trans|Scheme}} -- this implementation illustrates differences in identifiers and syntaxes of Scheme and Racket's <code>match-lambda</code> family. [http://docs.racket-lang.org/reference/match.html <code>racket/match</code> documented here].
{{trans|Scheme}} -- this implementation illustrates differences in identifiers and syntaxes of Scheme and Racket's <code>match-lambda</code> family. [http://docs.racket-lang.org/reference/match.html <code>racket/match</code> documented here].


<lang racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base
(require racket/match)
(require racket/match)


Line 1,512: Line 1,512:
(tree-sort-itr '(() () ()) lst))
(tree-sort-itr '(() () ()) lst))


(tree-sort '(5 3 7 9 1))</lang>
(tree-sort '(5 3 7 9 1))</syntaxhighlight>


{{out}}
{{out}}
Line 1,521: Line 1,521:
{{libheader|Matchable}}
{{libheader|Matchable}}
{{works with|Chicken Scheme}}
{{works with|Chicken Scheme}}
<lang Scheme>(use matchable)
<syntaxhighlight lang="scheme">(use matchable)


(define insert
(define insert
Line 1,551: Line 1,551:
[(x (a . b)) (tree-sort-itr (insert a x) b)]
[(x (a . b)) (tree-sort-itr (insert a x) b)]
[_ "incorrect arguments or broken tree" ]))
[_ "incorrect arguments or broken tree" ]))
(tree-sort-itr '(() () ()) lst))</lang>
(tree-sort-itr '(() () ()) lst))</syntaxhighlight>
Usage: <lang Scheme> #;2> (tree-sort '(5 3 7 9 1))
Usage: <syntaxhighlight lang="scheme"> #;2> (tree-sort '(5 3 7 9 1))
(1 3 5 7 9)</lang>
(1 3 5 7 9)</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
Line 1,559: Line 1,559:
{{libheader|Wren-llist}}
{{libheader|Wren-llist}}
{{libheader|wren-sort}}
{{libheader|wren-sort}}
<lang ecmascript>import "/llist" for DLinkedList
<syntaxhighlight lang="ecmascript">import "/llist" for DLinkedList
import "/sort" for Cmp
import "/sort" for Cmp


Line 1,603: Line 1,603:
treeSort.call(ll)
treeSort.call(ll)
var ll2 = DLinkedList.new(["d", "c", "e", "b", "a"])
var ll2 = DLinkedList.new(["d", "c", "e", "b", "a"])
treeSort.call(ll2)</lang>
treeSort.call(ll2)</syntaxhighlight>


{{out}}
{{out}}
Line 1,612: Line 1,612:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Tree_sort_on_a_linked_list
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Tree_sort_on_a_linked_list
// by Galileo, 04/2022
// by Galileo, 04/2022


Line 1,685: Line 1,685:
makeTree("one two three four five six seven eight nine ten")
makeTree("one two three four five six seven eight nine ten")
showTree(1)
showTree(1)
print</lang>
print</syntaxhighlight>
{{out}}
{{out}}
<pre>eight five four nine one seven six ten three two
<pre>eight five four nine one seven six ten three two
Line 1,692: Line 1,692:
=={{header|zkl}}==
=={{header|zkl}}==
This code reads a file [of source code] line by line, and builds a binary tree of the first word of each line. Then prints the sorted list.
This code reads a file [of source code] line by line, and builds a binary tree of the first word of each line. Then prints the sorted list.
<lang zkl>class Node{
<syntaxhighlight lang="zkl">class Node{
var left,right,value;
var left,right,value;
fcn init(value){ self.value=value; }
fcn init(value){ self.value=value; }
Line 1,718: Line 1,718:
}
}
}
}
}</lang>
}</syntaxhighlight>
<lang zkl>tree:=Tree();
<syntaxhighlight lang="zkl">tree:=Tree();
File("bbb.zkl").pump(tree.add,fcn(line){ // 5,000 lines to 660 words
File("bbb.zkl").pump(tree.add,fcn(line){ // 5,000 lines to 660 words
line.split(" ")[0].strip(); // take first word
line.split(" ")[0].strip(); // take first word
});
});


foreach word in (tree){ println(word) }</lang>
foreach word in (tree){ println(word) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>