Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions QuickSort/QuickSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package QuickSort

import (
"math/rand"
)

func QuickSort(a []int) []int {
if len(a) < 2 {
return a
}
left, right := 0, len(a)-1
pivot := rand.Int() % len(a)
a[pivot], a[right] = a[right], a[pivot]
for i, _ := range a {
if a[i] < a[right] {
a[left], a[i] = a[i], a[left]
left++
}
}
a[left], a[right] = a[right], a[left]
QuickSort(a[:left])
QuickSort(a[left+1:])
return a
}
24 changes: 24 additions & 0 deletions QuickSort/QuickSort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package QuickSort
import (
"math/rand"
"sort"
"testing"
"time"
)

func TestSelectionSort(t *testing.T) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
array1 := make([]int, random.Intn(100-10)+10)
for i := range array1 {
array1[i] = random.Intn(100)
}
array2 := make(sort.IntSlice, len(array1))
copy(array2, array1)
QuickSort(array1)
array2.Sort()
for i := range array1 {
if array1[i] != array2[i] {
t.Fail()
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ There are several data structures and algorithms implemented in this project. Th
- Cocktail Sort
- Gnome Sort
- Merge Sort
- Quick Sort

## Usage

Expand Down