Skip to content

Commit

Permalink
added mergesort.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AshiSinha18 committed Oct 27, 2017
1 parent 4848ddd commit 2412b44
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions mergesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import "fmt"

func mergeSort(a []int) []int {

if len(a) <= 1 {
return a
}

left := make([]int, 0)
right := make([]int, 0)
m := len(a) / 2

for i, x := range a {
switch {
case i < m:
left = append(left, x)
case i >= m:
right = append(right, x)
}
}

left = mergeSort(left)
right = mergeSort(right)

return merge(left, right)
}

func merge(left, right []int) []int {

results := make([]int, 0)

for len(left) > 0 || len(right) > 0 {
if len(left) > 0 && len(right) > 0 {
if left[0] <= right[0] {
results = append(results, left[0])
left = left[1:len(left)]
} else {
results = append(results, right[0])
right = right[1:len(right)]
}
} else if len(left) > 0 {
results = append(results, left[0])
left = left[1:len(left)]
} else if len(right) > 0 {
results = append(results, right[0])
right = right[1:len(right)]
}
}

return results
}

func main() {

arrayzor := []int{1, 6, 2, 4, 9, 0, 5, 3, 7, 8}
fmt.Println("Unsorted array: ", arrayzor)
arrayzor = mergeSort(arrayzor)
fmt.Println("Sorted array: ", arrayzor)
}

0 comments on commit 2412b44

Please sign in to comment.