From 58e8ec469b2326c98ea2a5970f5155b92881b8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?leotyliu=28=E5=88=98=E5=A4=A9=E4=B8=80=29?= Date: Wed, 17 Oct 2018 15:56:58 +0800 Subject: [PATCH] 12_sort --- go/12_sorts/MergeSort.go | 47 +++++++++++++++++++++++++++++++++++ go/12_sorts/MergeSort_test.go | 12 +++++++++ go/12_sorts/QuickSort.go | 37 +++++++++++++++++++++++++++ go/12_sorts/QuickSort_test.go | 12 +++++++++ 4 files changed, 108 insertions(+) diff --git a/go/12_sorts/MergeSort.go b/go/12_sorts/MergeSort.go index 5c77083..99babd4 100644 --- a/go/12_sorts/MergeSort.go +++ b/go/12_sorts/MergeSort.go @@ -1 +1,48 @@ package _2_sorts + +func MergeSort(arr []int) { + arrLen := len(arr) + if arrLen <= 1 { + return + } + + mergeSort(arr, 0, arrLen-1) +} + +func mergeSort(arr []int, start, end int) { + if start >= end { + return + } + + mid := (start + end) / 2 + mergeSort(arr, start, mid) + mergeSort(arr, mid+1, end) + merge(arr, start, mid, end) +} + +func merge(arr []int, start, mid, end int) { + tmpArr := make([]int, end-start+1) + + i := start + j := mid + 1 + k := 0 + for ; i <= mid && j <= end; k++ { + if arr[i] < arr[j] { + tmpArr[k] = arr[i] + i++ + } else { + tmpArr[k] = arr[j] + j++ + } + } + + for ; i <= mid; i++ { + tmpArr[k] = arr[i] + k++ + } + for ; j <= end; j++ { + tmpArr[k] = arr[j] + k++ + } + copy(arr[start:end+1], tmpArr) +} diff --git a/go/12_sorts/MergeSort_test.go b/go/12_sorts/MergeSort_test.go index 5c77083..693bab1 100644 --- a/go/12_sorts/MergeSort_test.go +++ b/go/12_sorts/MergeSort_test.go @@ -1 +1,13 @@ package _2_sorts + +import "testing" + +func TestMergeSort(t *testing.T) { + arr := []int{5, 4} + MergeSort(arr) + t.Log(arr) + + arr = []int{5, 4, 3, 2, 1} + MergeSort(arr) + t.Log(arr) +} diff --git a/go/12_sorts/QuickSort.go b/go/12_sorts/QuickSort.go index 5c77083..427d4e5 100644 --- a/go/12_sorts/QuickSort.go +++ b/go/12_sorts/QuickSort.go @@ -1 +1,38 @@ package _2_sorts + +func QuickSort(arr []int) { + arrLen := len(arr) + if arrLen <= 1 { + return + } + quickSort(arr, 0, arrLen-1) +} + +func quickSort(arr []int, start, end int) { + if start >= end { + return + } + + pivot := partition(arr, start, end) + quickSort(arr, start, pivot) + quickSort(arr, pivot+1, end) +} + +func partition(arr []int, low, high int) int { + pivotV := arr[low] + for low < high { + for low < high && arr[high] > pivotV { //指针从右边开始向右找到一个比pivot小的数 + high-- + } + arr[low] = arr[high] //将这个数放到low位,注意第一次这个位置放的是pivot值,所以不会丢 + + for low < high && arr[low] < pivotV { //指针从左边开始向右找到第一个比pivot大的数 + low++ + } + arr[high] = arr[low] //将这个数赋值给之前的high指针,因为之前high指针指向的数已经被一定,所以不会丢 + } + + //最后将pivot的值放入合适位置,此时low与high相等 + arr[low] = pivotV + return low +} diff --git a/go/12_sorts/QuickSort_test.go b/go/12_sorts/QuickSort_test.go index 5c77083..4e0ba08 100644 --- a/go/12_sorts/QuickSort_test.go +++ b/go/12_sorts/QuickSort_test.go @@ -1 +1,13 @@ package _2_sorts + +import "testing" + +func TestQuickSort(t *testing.T) { + arr := []int{5, 4} + QuickSort(arr) + t.Log(arr) + + arr = []int{5, 4, 3, 2, 1} + QuickSort(arr) + t.Log(arr) +}