From 34dcd1869ed5fb6901ff66792856fdf2a9bfd34b Mon Sep 17 00:00:00 2001 From: ganeshvenkatasai Date: Tue, 23 Jul 2024 23:52:22 +0530 Subject: [PATCH 1/2] feat: add Circle Sort algorithm --- sort/circlesort.go | 44 +++++++++++++++++++++++++++++++++++++++++ sort/circlesort_test.go | 25 +++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 sort/circlesort.go create mode 100644 sort/circlesort_test.go diff --git a/sort/circlesort.go b/sort/circlesort.go new file mode 100644 index 000000000..c18b0d408 --- /dev/null +++ b/sort/circlesort.go @@ -0,0 +1,44 @@ +// Package sort implements various sorting algorithms. +package sort + +import "github.com/TheAlgorithms/Go/constraints" + +// Circle sorts an array using the circle sort algorithm. +func Circle[T constraints.Ordered](arr []T) []T { + if len(arr) == 0 { + return arr + } + for doSort(arr, 0, len(arr)-1) { + } + return arr +} + +// doSort is the recursive function that implements the circle sort algorithm. +func doSort[T constraints.Ordered](arr []T, left, right int) bool { + if left == right { + return false + } + swapped := false + low := left + high := right + + for low < high { + if arr[low] > arr[high] { + arr[low], arr[high] = arr[high], arr[low] + swapped = true + } + low++ + high-- + } + + if low == high && arr[low] > arr[high+1] { + arr[low], arr[high+1] = arr[high+1], arr[low] + swapped = true + } + + mid := left + (right-left)/2 + leftHalf := doSort(arr, left, mid) + rightHalf := doSort(arr, mid+1, right) + + return swapped || leftHalf || rightHalf +} diff --git a/sort/circlesort_test.go b/sort/circlesort_test.go new file mode 100644 index 000000000..55cd4920f --- /dev/null +++ b/sort/circlesort_test.go @@ -0,0 +1,25 @@ +package sort + +import ( + "reflect" + "testing" +) + +func TestCircle(t *testing.T) { + testCases := []struct { + input []int + expected []int + }{ + {input: []int{3, 1, 2}, expected: []int{1, 2, 3}}, + {input: []int{5, 3, 4, 1, 2}, expected: []int{1, 2, 3, 4, 5}}, + {input: []int{1, 2, 3, 4, 5}, expected: []int{1, 2, 3, 4, 5}}, + {input: []int{}, expected: []int{}}, + } + + for _, tc := range testCases { + result := Circle(tc.input) + if !reflect.DeepEqual(result, tc.expected) { + t.Errorf("expected %v, got %v", tc.expected, result) + } + } +} From 67c6df98673685e486ab7387ce85cd4f2629bb25 Mon Sep 17 00:00:00 2001 From: ganeshvenkatasai Date: Wed, 24 Jul 2024 00:35:43 +0530 Subject: [PATCH 2/2] Add test and benchmark for Circle sort algorithm --- sort/circlesort_test.go | 25 ------------------------- sort/sorts_test.go | 8 ++++++++ 2 files changed, 8 insertions(+), 25 deletions(-) delete mode 100644 sort/circlesort_test.go diff --git a/sort/circlesort_test.go b/sort/circlesort_test.go deleted file mode 100644 index 55cd4920f..000000000 --- a/sort/circlesort_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package sort - -import ( - "reflect" - "testing" -) - -func TestCircle(t *testing.T) { - testCases := []struct { - input []int - expected []int - }{ - {input: []int{3, 1, 2}, expected: []int{1, 2, 3}}, - {input: []int{5, 3, 4, 1, 2}, expected: []int{1, 2, 3, 4, 5}}, - {input: []int{1, 2, 3, 4, 5}, expected: []int{1, 2, 3, 4, 5}}, - {input: []int{}, expected: []int{}}, - } - - for _, tc := range testCases { - result := Circle(tc.input) - if !reflect.DeepEqual(result, tc.expected) { - t.Errorf("expected %v, got %v", tc.expected, result) - } - } -} diff --git a/sort/sorts_test.go b/sort/sorts_test.go index 63ca010e0..213b17e56 100644 --- a/sort/sorts_test.go +++ b/sort/sorts_test.go @@ -190,6 +190,10 @@ func TestTimsort(t *testing.T) { testFramework(t, sort.Timsort[int]) } +func TestCircle(t *testing.T) { + testFramework(t, sort.Circle[int]) +} + // END TESTS func benchmarkFramework(b *testing.B, f func(arr []int) []int) { @@ -328,3 +332,7 @@ func BenchmarkCycle(b *testing.B) { func BenchmarkTimsort(b *testing.B) { benchmarkFramework(b, sort.Timsort[int]) } + +func BenchmarkCircle(b *testing.B) { + benchmarkFramework(b, sort.Circle[int]) +}