Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,45 +0,0 @@
|
|||
with Ada.Text_IO;
|
||||
procedure Pancake_Sort is
|
||||
generic
|
||||
type Element_Type is private;
|
||||
type Index_Type is range <>;
|
||||
type Array_Type is array (Index_Type range <>) of Element_Type;
|
||||
with function ">" (Left, Right : Element_Type) return Boolean is <>;
|
||||
procedure Pancake_Sort (Data: in out Array_Type);
|
||||
|
||||
procedure Pancake_Sort (Data: in out Array_Type) is
|
||||
procedure Flip (Up_To : in Index_Type) is
|
||||
Temp : constant Array_Type := Data (Data'First .. Up_To);
|
||||
begin
|
||||
for I in Temp'Range loop
|
||||
Data (I) := Temp (Temp'First + Up_To - I);
|
||||
end loop;
|
||||
end Flip;
|
||||
Max_Index : Index_Type;
|
||||
begin
|
||||
for I in reverse Data'First + 1 .. Data'Last loop
|
||||
Max_Index := Data'First;
|
||||
for A in Data'First + 1 .. I loop
|
||||
if Data(A) > Data (Max_Index) then
|
||||
Max_Index := A;
|
||||
end if;
|
||||
end loop;
|
||||
if Max_Index /= I then
|
||||
if Max_Index > Data'First then
|
||||
Flip (Max_Index);
|
||||
end if;
|
||||
Flip (I);
|
||||
end if;
|
||||
end loop;
|
||||
end Pancake_Sort;
|
||||
|
||||
type Integer_Array is array (Positive range <>) of Integer;
|
||||
procedure Int_Pancake_Sort is new Pancake_Sort (Integer, Positive, Integer_Array);
|
||||
Test_Array : Integer_Array := (3, 14, 1, 5, 9, 2, 6, 3);
|
||||
begin
|
||||
Int_Pancake_Sort (Test_Array);
|
||||
for I in Test_Array'Range loop
|
||||
Ada.Text_IO.Put (Integer'Image (Test_Array (I)));
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end Pancake_Sort;
|
||||
|
|
@ -58,10 +58,10 @@ extension op
|
|||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
public Program()
|
||||
{
|
||||
var list := new int[]{6, 7, 8, 9, 2, 5, 3, 4, 1};
|
||||
|
||||
console.printLine("before:", list.asEnumerable());
|
||||
console.printLine("after :", list.pancakeSort().asEnumerable())
|
||||
Console.printLine("before:", list.asEnumerable());
|
||||
Console.printLine("after :", list.pancakeSort().asEnumerable())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
function flip(sequence s, integer n)
|
||||
object temp
|
||||
for i = 1 to n/2 do
|
||||
temp = s[i]
|
||||
s[i] = s[n-i+1]
|
||||
s[n-i+1] = temp
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
|
||||
function pancake_sort(sequence s)
|
||||
integer m
|
||||
for i = length(s) to 2 by -1 do
|
||||
m = 1
|
||||
for j = 2 to i do
|
||||
if compare(s[j], s[m]) > 0 then
|
||||
m = j
|
||||
end if
|
||||
end for
|
||||
|
||||
if m < i then
|
||||
if m > 1 then
|
||||
s = flip(s,m)
|
||||
end if
|
||||
s = flip(s,i)
|
||||
end if
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
|
||||
constant s = rand(repeat(100,10))
|
||||
|
||||
? s
|
||||
? pancake_sort(s)
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
class PancakeSort {
|
||||
@:generic
|
||||
inline private static function flip<T>(arr:Array<T>, num:Int) {
|
||||
var i = 0;
|
||||
while (i < --num) {
|
||||
var temp = arr[i];
|
||||
arr[i++] = arr[num];
|
||||
arr[num] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
@:generic
|
||||
public static function sort<T>(arr:Array<T>) {
|
||||
if (arr.length < 2) return;
|
||||
|
||||
var i = arr.length;
|
||||
while (i > 1) {
|
||||
var maxNumPos = 0;
|
||||
for (a in 0...i) {
|
||||
if (Reflect.compare(arr[a], arr[maxNumPos]) > 0)
|
||||
maxNumPos = a;
|
||||
}
|
||||
if (maxNumPos == i - 1) i--;
|
||||
if (maxNumPos > 0) flip(arr, maxNumPos + 1);
|
||||
flip(arr, i--);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
static function main() {
|
||||
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
|
||||
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
|
||||
3.5, 0.0, -4.1, -9.5];
|
||||
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
|
||||
'be', 'self-evident', 'that', 'all',
|
||||
'men', 'are', 'created', 'equal'];
|
||||
Sys.println('Unsorted Integers: ' + integerArray);
|
||||
PancakeSort.sort(integerArray);
|
||||
Sys.println('Sorted Integers: ' + integerArray);
|
||||
Sys.println('Unsorted Floats: ' + floatArray);
|
||||
PancakeSort.sort(floatArray);
|
||||
Sys.println('Sorted Floats: ' + floatArray);
|
||||
Sys.println('Unsorted Strings: ' + stringArray);
|
||||
PancakeSort.sort(stringArray);
|
||||
Sys.println('Sorted Strings: ' + stringArray);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +1,19 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
with javascript_semantics
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">pancake_sort</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: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">largest</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;"><</span><span style="color: #000000;">i</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function pancake_sort(sequence s)
|
||||
s = deep_copy(s)
|
||||
for i=length(s) to 2 by -1 do
|
||||
integer m = largest(s[1..i],true)
|
||||
if m<i then
|
||||
if m>1 then
|
||||
s[1..m] = reverse(s[1..m])
|
||||
end if
|
||||
s[1..i] = reverse(s[1..i])
|
||||
end if
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">s</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">pancake_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
constant s = shuffle(tagset(10))
|
||||
? s
|
||||
? pancake_sort(s)
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
Function FlipPancake( [Object[]] $indata, $index = 1 )
|
||||
{
|
||||
$data=$indata.Clone()
|
||||
$datal = $data.length - 1
|
||||
if( $index -gt 0 )
|
||||
{
|
||||
if( $datal -gt $index )
|
||||
{
|
||||
$first = $data[ $index..0 ]
|
||||
$last = $data[ ( $index + 1 )..$datal ]
|
||||
$data = $first + $last
|
||||
} else {
|
||||
$data = $data[ $index..0 ]
|
||||
}
|
||||
}
|
||||
$data
|
||||
}
|
||||
|
||||
Function MaxIdx( [Object[]] $data )
|
||||
{
|
||||
$data | ForEach-Object { $max = $data[ 0 ]; $i = 0; $maxi = 0 } { if( $_ -gt $max ) { $max = $_; $maxi = $i }; $i++ } { $maxi }
|
||||
}
|
||||
|
||||
Function PancakeSort( [Object[]] $data, $index = 0 )
|
||||
{
|
||||
"unsorted - $data"
|
||||
$datal = $data.length - 1
|
||||
if( $datal -gt 0 )
|
||||
{
|
||||
for( $i = $datal; $i -gt 0; $i-- )
|
||||
{
|
||||
$data = FlipPancake ( FlipPancake $data ( MaxIdx $data[ 0..$i ] ) ) $i
|
||||
}
|
||||
}
|
||||
"sorted - $data"
|
||||
}
|
||||
|
||||
$l = 100; PancakeSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } )
|
||||
Loading…
Add table
Add a link
Reference in a new issue