Jump to content

External sort: Difference between revisions

m
syntax highlighting fixup automation
(fix lang name)
m (syntax highlighting fixup automation)
Line 21:
A "half chunk" of integers at a time is read to each of two buffer lists covering different sections of the file range being partitioned. Only those integers needing to be swapped are written back to the file and each list is replaced as it's used up. When the converging sections eventually overlap, a single list is used instead which is updated in parallel with the file to ensure that the partitioning repeat stops in the right place. Partitions less than a "chunk" in length are sorted in memory with a heap sort. (The Foundation framework has a built-in NSMutableArray sort which is faster than a vanilla heap sort — even with the necessary derivation of NSMutableArrays from the lists and lists from the sorted arrays — but I don't know how well this fits the task's "low memory" conceit.)
 
<langsyntaxhighlight lang="applescript">(*
Quicksort algorithm: S.A.R. (Tony) Hoare, 1960.
Optimisations by Robert Sedgewick and others at various times.
Line 345:
 
set theFile to (path to desktop as text) & "Test.dat"
set sortedFile to externalSort(theFile)</langsyntaxhighlight>
 
=={{header|C++}}==
Line 369:
All sorted streams are merged in this way out to an external output file ''merged.txt''.
<langsyntaxhighlight lang="cpp">
/* ExternalSort.cpp */
 
Line 626:
 
/* inputfile integers -- one per line for simplicity */
</syntaxhighlight>
</lang>
 
{{out}}
Line 650:
 
A small test file consisting of random integers has been generated and sorted to demonstrate that the approach works.
<langsyntaxhighlight lang="go">package main
 
import (
Line 855:
check(err)
}
}</langsyntaxhighlight>
 
{{out}}
Line 869:
=={{header|J}}==
Untested on a memory mapped file.
<syntaxhighlight lang="j">
<lang J>
NB. Apply an in-place sorting algorithm to a memory mapped file
NB. in-place sort is translation of in-place python quicksort.
Line 907:
i. 0 0 NB. verbs return the final noun
)
</syntaxhighlight>
</lang>
 
Demonstration the sorting works:
Line 917:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">intfile = open("/tmp/mmap.bin", "r+")
 
arr = Mmap.mmap(intfile, Vector{Int64}, (div(stat(intfile).size, 8))) # Int64 is 8 bytes
 
sort!(arr)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Phix}}
 
<langsyntaxhighlight Nimlang="nim">import algorithm, heapqueue, os, random, sequtils, strformat, strutils
 
 
Line 983:
 
for filename in filenames:
removeFile(filename)</langsyntaxhighlight>
 
{{out}}
Line 1,006:
=={{header|Perl}}==
Simulate task by reading from 'DATA' handle and using tiny record limit. As written, works for any numeric input, but could define any kind of customized sorting.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,051:
654
789
234</langsyntaxhighlight>
{{out}}
<pre>123
Line 1,070:
=={{header|Phix}}==
Slight variation on [[Stream_Merge#Phix|Stream_Merge]]
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">pqueue</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,131:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,151:
=={{header|Python}}==
A technique demonstrated with a short string character data.
<langsyntaxhighlight lang="python">
#! /usr/bin/python3
 
Line 1,235:
example = main
example()
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Borrowing from [http://rosettacode.org/wiki/Stream_Merge Stream_Merge] here. Temporary files are automatically deleted when program is done, so no explicit clean-up required.
<syntaxhighlight lang="raku" perl6line>use File::Temp;
 
sub merge_streams ( @streams ) {
Line 1,267:
@files.push: store(@chunk) if @chunk;
 
say join ' ', merge_streams @files».&open;</langsyntaxhighlight>
{{out}}
<pre>-11 -9 -2 0 2 3 4 15 32 34 42 43 45 45 55 64 66 76 78 87 92 123</pre>
Line 1,277:
 
This particular example uses the DOS &nbsp; '''SORT''' &nbsp; and &nbsp; '''ERASE''' &nbsp; commands.
<langsyntaxhighlight lang="rexx">/*REXX pgm reads a file, splits into smaller files, sorts 'em, combines into sorted file*/
parse arg FID n lim seed . /*obtain optional arguments from the CL*/
if FID=='' | FID=="," then FID= 'SORT_EXT.OUT' /*name of the output (sorted) file. */
Line 1,328:
/*──────────────────────────────────────────────────────────────────────────────────────*/
srt: procedure expose sWork; parse arg #
do j=1 for #; fn= sWORK || j; 'SORT' fn "/O" fn; end /*j*/; return</langsyntaxhighlight><br><br>
 
=={{header|Wren}}==
Line 1,336:
{{libheader|Wren-str}}
A bit simpler than the Go version as we use fixed length integers which (together with a following space) can be sorted as strings.
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "random" for Random
import "/dynamic" for Struct
Line 1,455:
var fileName = "es%(i)"
File.delete(fileName)
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.