How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the CoffeeScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the CoffeeScript programming language

Table of Contents

Problem Statement

Sort an array of integers (of any convenient size) into ascending order using Circlesort. In short, compare the first element to the last element, then the second element to the second last element, etc. Then split the array in two and recurse until there is only one single element in the array, like this: Repeat this procedure until quiescence (i.e. until there are no swaps). Show both the initial, unsorted list and the final sorted list. (Intermediate steps during sorting are optional.) Optimizations (like doing 0.5 log2(n) iterations and then continue with an Insertion sort) are optional. Pseudo code:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the CoffeeScript programming language

Source code in the coffeescript programming language

circlesort = (arr, lo, hi, swaps) ->
  if lo == hi
     return (swaps)

  high = hi
  low  = lo
  mid = Math.floor((hi-lo)/2)

  while lo < hi
    if arr[lo] > arr[hi]
       t = arr[lo]
       arr[lo] = arr[hi]
       arr[hi] = t
       swaps++
    lo++
    hi--

  if lo == hi
     if arr[lo] > arr[hi+1]
        t = arr[lo]
        arr[lo] = arr[hi+1]
        arr[hi+1] = t
        swaps++

  swaps = circlesort(arr,low,low+mid,swaps)
  swaps = circlesort(arr,low+mid+1,high,swaps)

  return(swaps)

VA = [2,14,4,6,8,1,3,5,7,9,10,11,0,13,12,-1]

while circlesort(VA,0,VA.length-1,0)
   console.log VA


  

You may also check:How to resolve the algorithm Even or odd step by step in the Elixir programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the F# programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the PHP programming language