File tree Expand file tree Collapse file tree 3 files changed +101
-0
lines changed
Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Original file line number Diff line number Diff line change 1+ // oddevensort.go
2+ // Implementation of Odd-Even Sort (Brick Sort)
3+ // Reference: https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
4+
5+ package sort
6+
7+ import "github.com/TheAlgorithms/Go/constraints"
8+
9+ // OddEvenSort performs the odd-even sort algorithm on the given array.
10+ // It is a variation of bubble sort that compares adjacent pairs, alternating
11+ // between odd and even indexed elements in each pass until the array is sorted.
12+ func OddEvenSort [T constraints.Ordered ](arr []T ) []T {
13+ if len (arr ) == 0 { // handle empty array
14+ return arr
15+ }
16+
17+ swapped := true
18+ for swapped {
19+ swapped = false
20+
21+ // Perform "odd" indexed pass
22+ for i := 1 ; i < len (arr )- 1 ; i += 2 {
23+ if arr [i ] > arr [i + 1 ] {
24+ arr [i ], arr [i + 1 ] = arr [i + 1 ], arr [i ]
25+ swapped = true
26+ }
27+ }
28+
29+ // Perform "even" indexed pass
30+ for i := 0 ; i < len (arr )- 1 ; i += 2 {
31+ if arr [i ] > arr [i + 1 ] {
32+ arr [i ], arr [i + 1 ] = arr [i + 1 ], arr [i ]
33+ swapped = true
34+ }
35+ }
36+ }
37+
38+ return arr
39+ }
Original file line number Diff line number Diff line change 1+ // sleepsort.go
2+ // Implementation of Sleep Sort algorithm (mostly as a fun experiment)
3+ // Note: This is not a practical sorting algorithm and relies on goroutines and time.Sleep
4+ // Reference: https://en.wikipedia.org/wiki/Sorting_algorithm#Sleep_sort
5+
6+ package sort
7+
8+ import (
9+ "sync"
10+ "time"
11+ )
12+
13+ // SleepSort performs the sleep sort algorithm on the given array.
14+ // It uses goroutines to "sort" by sleeping for a duration proportional to each element's value.
15+ // WARNING: Not suitable for production or large numbers, as it is inefficient and can cause timing issues.
16+ func SleepSort (arr []int ) []int {
17+ if len (arr ) == 0 { // handle empty array
18+ return arr
19+ }
20+
21+ var wg sync.WaitGroup
22+ resultChan := make (chan int , len (arr ))
23+
24+ for _ , num := range arr {
25+ wg .Add (1 )
26+ go func (n int ) {
27+ defer wg .Done ()
28+ time .Sleep (time .Duration (n ) * time .Millisecond ) // sleep for n milliseconds
29+ resultChan <- n
30+ }(num )
31+ }
32+
33+ // Wait for all goroutines to finish
34+ go func () {
35+ wg .Wait ()
36+ close (resultChan )
37+ }()
38+
39+ // Collect sorted results
40+ var result []int
41+ for num := range resultChan {
42+ result = append (result , num )
43+ }
44+
45+ return result
46+ }
47+
48+ // Example usage:
49+ // func main() {
50+ // arr := []int{3, 1, 4, 2}
51+ // sortedArr := SleepSort(arr)
52+ // fmt.Println(sortedArr) // Output may vary based on timing accuracy
53+ // }
Original file line number Diff line number Diff line change @@ -194,6 +194,15 @@ func TestCircle(t *testing.T) {
194194 testFramework (t , sort .Circle [int ])
195195}
196196
197+ func TestSleepSort (t * testing.T ) {
198+ t .Skip ("Skipping test for Sleep Sort, as it is not a normal sort algorithm and it doesn't support negative values." )
199+ //testFramework(t, sort.SleepSort)
200+ }
201+
202+ func TestOddEvenSort (t * testing.T ) {
203+ testFramework (t , sort .OddEvenSort [int ])
204+ }
205+
197206// END TESTS
198207
199208func benchmarkFramework (b * testing.B , f func (arr []int ) []int ) {
You can’t perform that action at this time.
0 commit comments