Skip to content

Commit

Permalink
Sleep Sort in Go (jainaman224#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-code12 authored May 31, 2020
1 parent 06ebcbe commit 880e8d6
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Sleep_Sort/Sleep_Sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import "fmt"
import "time"

func sleepNumber(number int, results chan<- int) {
time.Sleep(time.Duration(number) * time.Second)
results <- number
}

func sleepSort(data []int) {
results := make(chan int, len(data))
for _, v := range data {
go sleepNumber(v, results)
}

for i, _ := range data {
data[i] = <-results
}
close(results)
}

func main() {
var x int
fmt.Printf("Enter the size of the array : ")
fmt.Scan(&x)
fmt.Printf("Enter the elements of the array : ")
a := make([]int, x)
for i := 0; i < x; i++ {
fmt.Scan(&a[i])
}
data := a // A copy of the array 'a' is assigned to 'data'
sleepSort(data)
fmt.Println("After sorting", data)
}

/*
Sample Input :
Enter the size of the array : 5
Enter the elements of the array : 34 23 1 78 6
Sample Output :
After sorting [1 6 23 34 78]
*/

0 comments on commit 880e8d6

Please sign in to comment.