Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,74 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Show_Cocktail_Sort is
|
||||
generic
|
||||
type T is private;
|
||||
procedure Swap (A, B : in out T);
|
||||
|
||||
procedure Swap (A, B : in out T) is
|
||||
Tmp : constant T := A;
|
||||
begin
|
||||
A := B;
|
||||
B := Tmp;
|
||||
end Swap;
|
||||
|
||||
generic
|
||||
type T is private;
|
||||
type Index is (<>);
|
||||
type T_Array_Type is array (Index range <>) of T;
|
||||
with function ">" (Left, Right : T) return Boolean;
|
||||
procedure Cocktail_Sort (T_Array : in out T_Array_Type);
|
||||
|
||||
procedure Cocktail_Sort (T_Array : in out T_Array_Type) is
|
||||
procedure Swap_T is new Swap (T => T);
|
||||
Low_Index : Index := T_Array'First;
|
||||
High_Index : Index := Index'Pred (T_Array'Last);
|
||||
New_Low_Index : Index;
|
||||
New_High_Index : Index;
|
||||
begin
|
||||
while Low_Index <= High_Index loop
|
||||
New_Low_Index := High_Index;
|
||||
New_High_Index := Low_Index;
|
||||
for I in Low_Index .. High_Index loop
|
||||
if T_Array (I) > T_Array (Index'Succ (I)) then
|
||||
Swap_T (T_Array (I), T_Array (Index'Succ (I)));
|
||||
New_High_Index := I;
|
||||
end if;
|
||||
end loop;
|
||||
High_Index := Index'Pred (New_High_Index);
|
||||
for I in reverse Low_Index .. High_Index loop
|
||||
if T_Array (I) > T_Array (Index'Succ (I)) then
|
||||
Swap_T (T_Array (I), T_Array (Index'Succ (I)));
|
||||
New_Low_Index := I;
|
||||
end if;
|
||||
end loop;
|
||||
Low_Index := Index'Succ (New_Low_Index);
|
||||
end loop;
|
||||
end Cocktail_Sort;
|
||||
|
||||
subtype Index is Positive range 1 .. 10;
|
||||
type Positive_Array_Type is array (Positive range <>) of Positive;
|
||||
Positive_Array : Positive_Array_Type (Index) :=
|
||||
(6, 10, 3, 1, 5, 8, 4, 9, 2, 7);
|
||||
|
||||
procedure Print_Positive_Array is
|
||||
begin
|
||||
Put ("Example array:");
|
||||
for I in Index loop
|
||||
Put (Positive'Image (Positive_Array (I)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end Print_Positive_Array;
|
||||
|
||||
procedure Sort_Positive_Array is new
|
||||
Cocktail_Sort
|
||||
(T => Positive,
|
||||
Index => Positive,
|
||||
T_Array_Type => Positive_Array_Type,
|
||||
">" => Standard.">");
|
||||
begin
|
||||
Print_Positive_Array;
|
||||
Sort_Positive_Array (Positive_Array);
|
||||
Put_Line ("Sorted");
|
||||
Print_Positive_Array;
|
||||
end Show_Cocktail_Sort;
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
proc cktlShakerSort &a[] .
|
||||
a = 1
|
||||
b = len a[] - 1
|
||||
while a <= b
|
||||
an = b
|
||||
bn = a
|
||||
for i = a to b
|
||||
if a[i] > a[i + 1]
|
||||
swap a[i] a[i + 1]
|
||||
bn = i
|
||||
.
|
||||
.
|
||||
b = bn - 1
|
||||
for i = b downto a
|
||||
if a[i] > a[i + 1]
|
||||
swap a[i] a[i + 1]
|
||||
an = i
|
||||
.
|
||||
.
|
||||
a = an + 1
|
||||
.
|
||||
.
|
||||
a[] = [ 4 38 100 1 25 69 69 16 8 59 71 53 33 ]
|
||||
cktlShakerSort a[]
|
||||
print a[]
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
program cocktail_sort_demo
|
||||
implicit none
|
||||
integer, parameter :: n = 12
|
||||
integer :: arr(n)
|
||||
integer :: i
|
||||
!
|
||||
call system_clock(count=i) ! Get a value we can use as a seed
|
||||
call srand(i) ! Set random number seed
|
||||
do i = 1,n
|
||||
arr(i) = irand(0) ! Fill with random integers
|
||||
end do
|
||||
print *, "Original array:"
|
||||
print *, arr
|
||||
|
||||
call cocktail_sort(arr, n)
|
||||
|
||||
print *, "Sorted array:"
|
||||
print *, arr
|
||||
|
||||
contains
|
||||
|
||||
!===========================================================
|
||||
! Subroutine: cocktail_sort
|
||||
! Purpose : Sort an integer array using the Cocktail Sort
|
||||
! algorithm (also known as bidirectional bubble sort).
|
||||
! This algorithm repeatedly traverses the array in
|
||||
! both directions, bubbling the largest element to
|
||||
! the end and the smallest element to the beginning.
|
||||
!===========================================================
|
||||
subroutine cocktail_sort(A, n)
|
||||
! Input: n = number of elements in the array
|
||||
! InOut: A = integer array to be sorted in ascending order
|
||||
|
||||
integer, intent(in) :: n ! size of the array
|
||||
integer, intent(inout) :: A(n) ! array to be sorted
|
||||
integer :: beginIdx ! current left boundary of unsorted region
|
||||
integer :: endIdx ! current right boundary of unsorted region
|
||||
integer :: newBeginIdx ! updated left boundary after backward pass
|
||||
integer :: newEndIdx ! updated right boundary after forward pass
|
||||
integer :: i ! loop index
|
||||
integer :: temp ! temporary variable for swapping
|
||||
|
||||
! Initialize boundaries:
|
||||
! beginIdx starts at the first element (index 1 in Fortran arrays).
|
||||
! endIdx starts at the last element (index n-1, since we compare A(i) with A(i+1)).
|
||||
beginIdx = 1
|
||||
endIdx = n - 1
|
||||
|
||||
! Outer loop: continue until the left boundary crosses the right boundary.
|
||||
! Each iteration performs one forward pass and one backward pass.
|
||||
do while (beginIdx <= endIdx)
|
||||
|
||||
! Reset new boundaries for this iteration.
|
||||
! These will track the last swap positions to shrink the unsorted region.
|
||||
newBeginIdx = endIdx
|
||||
newEndIdx = beginIdx
|
||||
|
||||
!---------------------------------------------------
|
||||
! Forward pass: move left to right.
|
||||
! Compare each pair A(i), A(i+1) and swap if out of order.
|
||||
! This bubbles the largest element toward the right end.
|
||||
!---------------------------------------------------
|
||||
do i = beginIdx, endIdx
|
||||
if (A(i) > A(i+1)) then
|
||||
! Swap elements A(i) and A(i+1)
|
||||
temp = A(i)
|
||||
A(i) = A(i+1)
|
||||
A(i+1) = temp
|
||||
|
||||
! Track the last index where a swap occurred.
|
||||
! This helps reduce the range for the next pass.
|
||||
newEndIdx = i
|
||||
end if
|
||||
end do
|
||||
|
||||
! After forward pass, update right boundary.
|
||||
! Elements beyond newEndIdx are already in correct position.
|
||||
endIdx = newEndIdx - 1
|
||||
|
||||
!---------------------------------------------------
|
||||
! Backward pass: move right to left.
|
||||
! Compare each pair A(i), A(i+1) and swap if out of order.
|
||||
! This bubbles the smallest element toward the left end.
|
||||
!---------------------------------------------------
|
||||
do i = endIdx, beginIdx, -1
|
||||
if (A(i) > A(i+1)) then
|
||||
! Swap elements A(i) and A(i+1)
|
||||
temp = A(i)
|
||||
A(i) = A(i+1)
|
||||
A(i+1) = temp
|
||||
|
||||
! Track the last index where a swap occurred.
|
||||
! This helps reduce the range for the next pass.
|
||||
newBeginIdx = i
|
||||
end if
|
||||
end do
|
||||
|
||||
! After backward pass, update left boundary.
|
||||
! Elements before newBeginIdx are already in correct position.
|
||||
beginIdx = newBeginIdx + 1
|
||||
|
||||
end do
|
||||
end subroutine cocktail_sort
|
||||
end program cocktail_sort_demo
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
-- Translation of pseudo-code.
|
||||
local function cocktail_shaker_sort(a)
|
||||
local start = 1
|
||||
local finish = #a - 1
|
||||
while start <= finish do
|
||||
local newstart = finish
|
||||
local newfinish = start
|
||||
for i = start, finish do
|
||||
if a[i] > a[i+1] then
|
||||
a[i], a[i + 1] = a[i + 1], a[i]
|
||||
newfinish = i
|
||||
end
|
||||
end
|
||||
finish = newfinish - 1
|
||||
if finish >= start then
|
||||
for i = finish, start, -1 do
|
||||
if a[i] > a[i+1] then
|
||||
a[i], a[i + 1] = a[i + 1], a[i]
|
||||
newstart = i
|
||||
end
|
||||
end
|
||||
end
|
||||
start = newstart + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- From the RC Cocktail sort task (no optimizations).
|
||||
local function cocktail_sort(a)
|
||||
local last = #a
|
||||
while true do
|
||||
local swapped = false
|
||||
for i = 1, last - 1 do
|
||||
if a[i] > a[i + 1] then
|
||||
a[i], a[i + 1] = a[i + 1], a[i]
|
||||
swapped = true
|
||||
end
|
||||
end
|
||||
if !swapped then return end
|
||||
swapped = false
|
||||
if last >= 1 then
|
||||
for i = last - 1, 1, -1 do
|
||||
if a[i] > a[i + 1] then
|
||||
a[i], a[i + 1] = a[i + 1], a[i]
|
||||
swapped = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if !swapped then return end
|
||||
end
|
||||
end
|
||||
|
||||
-- First make sure the routines are working correctly.
|
||||
local a = {21, 4, -9, 62, -7, 107, -62, 4, 0, -170}
|
||||
fmt.print("Original array: %,s", a)
|
||||
local b = a:clone() -- make copy as sorts mutate array in place
|
||||
cocktail_sort(a)
|
||||
fmt.print("Cocktail sort : %,s", a)
|
||||
cocktail_shaker_sort(b)
|
||||
fmt.print("C/Shaker sort : %,s", b)
|
||||
|
||||
-- Timing comparison code.
|
||||
print("\nRelative speed of the two sorts")
|
||||
print(" N x faster (CSS v CS)")
|
||||
print("----- -------------------")
|
||||
local runs = 5 -- average over 5 runs say
|
||||
for {1000, 2000, 4000, 8000, 10000, 20000} as n do
|
||||
local sum = 0
|
||||
for i = 1, runs do
|
||||
-- Get 'n' random numbers in range [0, 99,999]
|
||||
-- with every other number being negated.
|
||||
local nums = table.create(n)
|
||||
for j = 1, n do
|
||||
local rn = math.random(1, 99_999)
|
||||
if j % 2 == 0 then rn = -rn end
|
||||
nums[j] = rn
|
||||
end
|
||||
-- Copy the array
|
||||
local nums2 = nums:clone()
|
||||
|
||||
local start = os.clock()
|
||||
cocktail_sort(nums)
|
||||
local elapsed = os.clock() - start
|
||||
local start2 = os.clock()
|
||||
cocktail_shaker_sort(nums2)
|
||||
local elapsed2 = os.clock() - start2
|
||||
sum += elapsed/elapsed2
|
||||
end
|
||||
fmt.print(" %2dk %0.3f", n // 1000, sum / runs)
|
||||
end
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
' Sorting algorithms/Cocktail sort with shifting bounds - VBScript
|
||||
|
||||
Function cocktailShakerSort(ByVal A)
|
||||
beginIdx = Lbound(A)
|
||||
endIdx = Ubound(A)-1
|
||||
Do While beginIdx <= endIdx
|
||||
newBeginIdx = endIdx
|
||||
newEndIdx = beginIdx
|
||||
For ii = beginIdx To endIdx
|
||||
If A(ii) > A(ii+1) Then
|
||||
tmp=A(ii) : A(ii)=A(ii+1) : A(ii+1)=tmp
|
||||
newEndIdx = ii
|
||||
End If
|
||||
Next
|
||||
endIdx = newEndIdx - 1
|
||||
For ii = endIdx To beginIdx Step -1
|
||||
If A(ii) > A(ii+1) Then
|
||||
tmp=A(ii) : A(ii)=A(ii+1) : A(ii+1)=tmp
|
||||
newBeginIdx = ii
|
||||
End If
|
||||
Next
|
||||
beginIdx = newBeginIdx+1
|
||||
Loop
|
||||
cocktailShakerSort=A
|
||||
End Function 'cocktailShakerSort
|
||||
|
||||
Dim B(20)
|
||||
For i=Lbound(B) To Ubound(B)
|
||||
B(i)=Int(Rnd()*100)
|
||||
Next
|
||||
Wscript.Echo Join(cocktailShakerSort(B)," ")
|
||||
Loading…
Add table
Add a link
Reference in a new issue