How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Julia programming language
How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Julia programming language
Table of Contents
Problem Statement
The cocktail sort is an improvement on the Bubble Sort.
A cocktail sort is also known as:
The improvement is basically that values "bubble" (migrate) both directions through the array, because on each iteration the cocktail sort bubble sorts once forwards and once backwards. After ii passes, the first ii and the last ii elements in the array are in their correct positions, and don't have to be checked (again). By shortening the part of the array that is sorted each time, the number of comparisons can be halved.
Pseudocode for the 2nd algorithm (from Wikipedia) with an added comment and changed indentations: % indicates a comment, and deal indicates a swap.
Implement a cocktail sort and optionally show the sorted output here on this page. See the discussion page for some timing comparisons.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Julia programming language
The code snippet you provided implements the Cocktail Shaker Sort in Julia, a programming language for scientific computing.
The Cocktail Shaker Sort is a sorting algorithm that is similar to the Bubble Sort, but it scans the array in both directions, from left to right and right to left.
The code is divided into two modules, CocktailShakerSorts
and BenchmarkTools
.
The CocktailShakerSorts
module implements the sorting algorithm and exports the CocktailShakerSort
struct as the algorithm.
The BenchmarkTools
module is used to benchmark the sorting algorithm.
The sort!
function is the main function of the sorting algorithm.
It takes an AbstractVector A
, two indices lo
and hi
, an algorithm a
, and an Ordering ord
.
The lo
and hi
indices specify the range of the array to be sorted, and the a
algorithm specifies the sorting algorithm to use.
The ord
Ordering specifies the ordering of the array, either ForwardOrdering or ReverseOrdering.
The sort!
function first checks if the range of the array to be sorted is valid.
If the range is not valid, it returns the array.
If the range is valid, it sorts the array using the Cocktail Shaker Sort algorithm.
The Cocktail Shaker Sort algorithm works by scanning the array in both directions, from left to right and right to left. In each pass, the algorithm compares adjacent elements in the array and swaps them if they are out of order. The algorithm continues to scan the array in both directions until the array is sorted.
The BenchmarkTools
module is used to benchmark the sorting algorithm.
The @btime
macro is used to measure the time it takes to sort an array.
The sort!
function is called twice, once with the default sorting algorithm and once with the Cocktail Shaker Sort algorithm.
The time it takes to sort the array with each algorithm is printed to the console.
The output of the code snippet is as follows:
[5, 8, 2, 0, 6, 1, 9, 3, 4] => [0, 1, 2, 3, 4, 5, 6, 8, 9] => [9, 8, 6, 5, 4, 3, 2, 1, 0]
Using default sort, which is Quicksort with integers:
9.719 ns (99 allocations: 2.20 KiB)
Using CocktailShakerSort:
12.321 ns (13 allocations: 2.20 KiB)
As you can see, the Cocktail Shaker Sort algorithm is slightly slower than the default sorting algorithm, which is Quicksort. However, the Cocktail Shaker Sort algorithm is still a relatively efficient sorting algorithm, and it is easy to implement.
Source code in the julia programming language
module CocktailShakerSorts
using Base.Order, Base.Sort
import Base.Sort: sort!
export CocktailShakerSort
struct CocktailSortAlg <: Algorithm end
const CocktailShakerSort = CocktailSortAlg()
function sort!(A::AbstractVector, lo::Int, hi::Int, a::CocktailSortAlg, ord::Ordering)
if lo > 1 || hi < length(A)
return sort!(view(A, lo:hi), 1, length(A), a, ord)
end
forward, beginindex, endindex = ord isa ForwardOrdering, 1, length(A) - 1
while beginindex <= endindex
newbegin, newend = endindex, beginindex
for idx in beginindex:endindex
if (forward && A[idx] > A[idx + 1]) || (!forward && A[idx] < A[idx + 1])
A[idx + 1], A[idx] = A[idx], A[idx + 1]
newend = idx
end
end
# end has another in correct place, so narrow end by 1
endindex = newend - 1
for idx in endindex:-1:beginindex
if (forward && A[idx] > A[idx + 1]) || (!forward && A[idx] < A[idx + 1])
A[idx + 1], A[idx] = A[idx], A[idx + 1]
newbegin = idx
end
end
# beginning has another in correct place, so narrow beginning by 1
beginindex = newbegin + 1
end
A
end
end # module
using .CocktailShakerSorts
using BenchmarkTools
cocktailshakersort!(v) = sort!(v; alg=CocktailShakerSort)
arr = [5, 8, 2, 0, 6, 1, 9, 3, 4]
println(arr, " => ", sort(arr, alg=CocktailShakerSort), " => ",
sort(arr, alg=CocktailShakerSort, rev=true))
println("\nUsing default sort, which is Quicksort with integers:")
@btime sort!([5, 8, 2, 0, 6, 1, 9, 3, 4])
println("\nUsing CocktailShakerSort:")
@btime cocktailshakersort!([5, 8, 2, 0, 6, 1, 9, 3, 4])
You may also check:How to resolve the algorithm Heronian triangles step by step in the Elixir programming language
You may also check:How to resolve the algorithm Arrays step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the Quackery programming language
You may also check:How to resolve the algorithm URL decoding step by step in the UNIX Shell programming language