8000 . · Pull Request #241 · codezoned/ScriptsDump · GitHub
[go: up one dir, main page]

Skip to content

. #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed

. #241

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Go implementation of Cycle Sort
  • Loading branch information
hrishikeshcs authored Oct 6, 2020
commit 99cc8a239ebbe0f3c3d85848990ae7112599c79c
74 changes: 74 additions & 0 deletions Arrays-Sorting/src/CycleSort.go
C58C
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import "fmt"

func sort(arr []int, n int) int {
var writes int

for cycleStart:=0; cycleStart<n-1; cycleStart++ {
item := arr[cycleStart]
pos := cycleStart

for i:=cycleStart+1; i<n; i++ {
if(arr[i] < item) {
pos++
}
}

if(pos == cycleStart) {
continue
}

for item == arr[pos] {
pos++
}

if(item != arr[pos]) {
item, arr[pos] = arr[pos], item

writes++
}

for pos!=cycleStart {
pos = cycleStart

for i:=cycleStart+1; i<n; i++ {
if(arr[i] < item) {
pos++
}
}

for item == arr[pos] {
pos++
}

if(item != arr[pos]) {
item, arr[pos] = arr[pos], item

writes++
}
}
}

return writes
}

func main() {
var n int

fmt.Printf("Enter the number of elements: ")
fmt.Scan(&n)
arr := make([]int, n)

fmt.Printf("Enter the %d elements: ", n)
for i:=0; i<n; i++ {
fmt.Scan(&arr[i])
}

sort(arr, n)
fmt.Println("Sorted Array:")
for _, x := range arr {
fmt.Printf("%d ", x)
}
fmt.Println()
}
0