From d86292da29495d7a0a209d8d798bc87fa16cce50 Mon Sep 17 00:00:00 2001 From: clantonelli Date: Tue, 6 Aug 2024 13:32:58 +0200 Subject: [PATCH] Create circle_sort Implementing the Circle Sort algorithm --- sorts/circle_sort | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 sorts/circle_sort diff --git a/sorts/circle_sort b/sorts/circle_sort new file mode 100644 index 00000000..a7ac154f --- /dev/null +++ b/sorts/circle_sort @@ -0,0 +1,53 @@ +/** + * @function CircleSort + * @description Sort an Array by comparing the first element to the last element, then the second element to the second last element, etc. (drawing circle into the array) and then splitting the array. Execute recursively + * @param {number[]}array - The input array + * @return {number[]} - The sorted array. + * @example circleSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] + */ + +const doCircle = (array: number[], left: number, right: number): boolean => { + let swap = false; + + if (left == right) { + return false; + } + + let l = left + let r = right + + while (l < r) { + if (array[l] > array[r]) { + ;[array[l], array[r]] = [array[r], array[l]]; + swap = true; + } + l++; + r--; + } + + /** + * Odd size + */ + if (l == r) { + if (array[l] > array[r + 1]) { + [array[l], array[r+1]] = [array[r+1], array[l]]; + swap = true; + } + } + + let middle = Math.floor((right - left) / 2); + let firstHalf = doCircle(array, left, left + middle); + let secondHalf = doCircle(array, left + middle + 1, right); + + return swap || firstHalf || secondHalf; +} + +export const CircleSort = (array: number[]) => { + while (doCircle(array, 0, array.length-1)) { + /** + * Nothing + */ + } + + return array +}