Last active
March 1, 2023 12:08
-
-
Save shyamvlmna/0ffcd8642463a4c52d38a98930f5ffa9 to your computer and use it in GitHub Desktop.
Sorting algorithms in Go (Selection sort, Insertion sort, Bubble sort, Merge sort, Quick sort)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
a := make([]int, rand.Intn(20)) | |
for i := range a { | |
rand.Int() | |
a[i] = rand.Intn(999) - rand.Intn(555) | |
} | |
a = append(a, 0) | |
fmt.Println(SelectionSort(a)) | |
fmt.Println(InsertionSort(a)) | |
fmt.Println(BubbleSort(a)) | |
fmt.Println(MergeSort(a)) | |
fmt.Println(QuickSort(a)) | |
} | |
// selectionsort | |
func SelectionSort(arr []int) []int { | |
for i := 0; i < len(arr)-1; i++ { | |
for j := i + 1; j < len(arr); j++ { | |
if arr[j] < arr[i] { | |
arr[i], arr[j] = arr[j], arr[i] | |
} | |
} | |
} | |
return arr | |
} | |
// insertionsort | |
func InsertionSort(arr []int) []int { | |
for i := 1; i < len(arr); i++ { | |
current := arr[i] | |
j := i - 1 | |
for j >= 0 && arr[j] > current { | |
arr[j], arr[j+1] = arr[j+1], arr[j] | |
j-- | |
} | |
} | |
return arr | |
} | |
// bubblesort | |
func BubbleSort(arr []int) []int { | |
for i := 1; i < len(arr); i++ { | |
for j := 0; j < len(arr)-i; j++ { | |
if arr[j] > arr[j+1] { | |
arr[j], arr[j+1] = arr[j+1], arr[j] | |
} | |
} | |
} | |
return arr | |
} | |
// MergeSort | |
func MergeSort(arr []int) []int { | |
l := len(arr) | |
if l < 2 { | |
return arr | |
} | |
return merge(MergeSort(arr[:l/2]), MergeSort(arr[l/2:])) | |
} | |
func merge(left, right []int) []int { | |
i, j := 0, 0 | |
res := []int{} | |
for i < len(left) && j < len(right) { | |
if left[i] < right[j] { | |
res = append(res, left[i]) | |
i++ | |
} else { | |
res = append(res, right[j]) | |
j++ | |
} | |
} | |
for i < len(left) { | |
res = append(res, left[i]) | |
i++ | |
} | |
for j < len(right) { | |
res = append(res, right[j]) | |
j++ | |
} | |
return res | |
} | |
// QuickSort | |
func QuickSort(arr []int) []int { | |
quickSortHelper(arr, 0, len(arr)-1) | |
return arr | |
} | |
func quickSortHelper(arr []int, strIdx, endIdx int) { | |
if strIdx >= endIdx { | |
return | |
} | |
pivot := strIdx | |
left := strIdx + 1 | |
right := endIdx | |
//partition algorithm | |
for left <= right { | |
if arr[left] > arr[pivot] && arr[right] < arr[pivot] { | |
arr[left], arr[right] = arr[right], arr[left] | |
left++ | |
right-- | |
} | |
if arr[left] <= arr[pivot] { | |
left++ | |
} else if arr[right] >= arr[pivot] { | |
right-- | |
} | |
} | |
arr[right], arr[pivot] = arr[pivot], arr[right] | |
quickSortHelper(arr, strIdx, right-1) | |
quickSortHelper(arr, right+1, endIdx) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment