Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,45 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
local function flip(a, r)
|
||||
local l = 1
|
||||
while l <= r do
|
||||
a[r], a[l] = a[l], a[r]
|
||||
l += 1
|
||||
r -= 1
|
||||
end
|
||||
end
|
||||
|
||||
local function pancake_sort(a)
|
||||
for uns = #a, 2, -1 do
|
||||
local lg = a:slice(1, uns):max()
|
||||
local lx = a:findindex(|e| -> e == lg)
|
||||
flip(a, lx)
|
||||
flip(a, uns)
|
||||
end
|
||||
end
|
||||
|
||||
local a = {31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
|
||||
print($"Before: \{{a:concat(", ")}}")
|
||||
pancake_sort(a)
|
||||
print($"After : \{{a:concat(", ")}}")
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
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