Quicksort implementation in Go

huangapple go评论96阅读模式
英文:

Quicksort implementation in Go

问题

我正在尝试在Go语言中实现一个快速排序算法,只是为了学习目的。

到目前为止,我已经编写了以下代码:

package main

import (
	"fmt"
)

var arr = []int{20, 43, 52, -1, 43, 29, 34}

func main() {
	fmt.Println("Unsorted: ", arr)
	quick_sort(arr)
	fmt.Println("Sorted: ", quick_sort(arr))
}

func quick_sort(arr []int) []int {
	var recurse func(left int, right int)
	var swap func(i int, j int)
	var partition func(left int, right int, pivot int)

	swap = func(i int, j int) {
		var temp = arr[i]
		arr[i] = arr[j]
		arr[j] = temp
	}

	partition = func(left int, right int, pivot int) int {
		v := arr[pivot]
		right--
		swap(pivot, right)

		for i := left; i < right; i++ {
			// 这里的 arr[i] 似乎没有更新
			fmt.Println(arr, left, right, i, arr[i], v)
			if arr[i] <= v {
				left++
				swap(i, left)
			}
		}

		swap(left, right)
		return left
	}

	recurse = func(left int, right int) {
		if left < right {
			pivot := (right + left) / 2
			pivot = partition(left, right, pivot)
			recurse(left, pivot)
			recurse(pivot+1, right)
		}
	}

	recurse(0, len(arr))
	return arr
}

这是我之前用JavaScript编写的代码的直接翻译:

function quick_sort(arr) {

  function partition(left, right, pivot) {
    var v = arr[pivot];
    swap(pivot, --right);

    for (var i = left;  i < right; i ++) {
      console.log(arr, left, right, i, arr[i], v);
      if (arr[i] <= v) {
        swap(i, left++);
      }
    }
    swap(left, right);

    return left;
  }

  function swap(i, j) {
    var temp = arr[i];

    arr[i] = arr[j];
    arr[j] = temp;
  }

  function recurse(left, right) {
    if (left < right) {
      var pivot = ~~((left + right) / 2)
      pivot = partition(left, right, pivot);
      recurse(left, pivot);
      recurse(pivot + 1, right);
    }
  }

  recurse(0, arr.length)
  return arr;
}

var arr = [20, 43, 52, -1, 43, 29, 34];

console.log(quick_sort(arr));

在JavaScript中,它完美地工作,但是在Go中,我无法让它正常工作。由于某种原因,在我的分区函数中,在for循环中,arr[i]的值保持不变,即使i在变化。

我已经花了很多时间试图弄清楚我做错了什么,但是我无法找出原因。

有人看到我漏掉的东西吗?

英文:

I am trying to implement a quicksort algorithm in go just for learning purposes.

So far I have come up with the following code:

package main
import (
&quot;fmt&quot;
)
var arr = []int{20, 43, 52, -1, 43, 29, 34}
func main() {
fmt.Println(&quot;Unsorted: &quot;, arr)
quick_sort(arr)
fmt.Println(&quot;Sorted: &quot;, quick_sort(arr))
}
func quick_sort(arr []int) []int {
var recurse func(left int, right int)
var swap func(i int, j int)
var partition func(left int, right int, pivot int) int
swap = func(i int, j int) {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
partition = func(left int, right int, pivot int) int {
v := arr[pivot]
right--
swap(pivot, right)
for i := left; i &lt; right; i++ {
// arr[i] doesn&#39;t seem to be updating here
fmt.Println(arr, left, right, i, arr[i], v)
if arr[i] &lt;= v {
left++
swap(i, left)
}
}
swap(left, right)
return left
}
recurse = func(left int, right int) {
if left &lt; right {
pivot := (right + left) / 2
pivot = partition(left, right, pivot)
recurse(left, pivot)
recurse(pivot+1, right)
}
}
recurse(0, len(arr))
return arr
}

This is a direct translation of a code I had previously written in javascript:

  function quick_sort(arr) {
function partition(left, right, pivot) {
var v = arr[pivot];
swap(pivot, --right);
for (var i = left;  i &lt; right; i ++) {
console.log(arr, left, right, i, arr[i], v);
if (arr[i] &lt;= v) {
swap(i, left++);
}
}
swap(left, right);
return left;
}
function swap(i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function recurse(left, right) {
if (left &lt; right) {
var pivot = ~~((left + right) / 2)
pivot = partition(left, right, pivot);
recurse(left, pivot);
recurse(pivot + 1, right);
}
}
recurse(0, arr.length)
return arr;
}
var arr = [20, 43, 52, -1, 43, 29, 34];
console.log(quick_sort(arr));

It works like a charm in js, but somehow I cannot get it to work in go. For some reason, in my partition function, in my for loop, the value of arr[i] remains constant even when i is changing.

I have spent quite a lot of time trying to figure out what I am doing wrong but I couldn't figure it out.

Does anyone see something I'm missing?

答案1

得分: 6

left++ 应该在 swap() 函数之后,如下所示:

   if arr[i] <= v {         
swap(i, left)
left++
}

修复后的输出为:

未排序:[20 43 52 -1 43 29 34]
排序后:[-1 20 29 34 43 43 52]
英文:

left++ should be after the swap() function as follow

   if arr[i] &lt;= v {         
swap(i, left)
left++
}

After the fix, output is

Unsorted:  [20 43 52 -1 43 29 34]
Sorted:  [-1 20 29 34 43 43 52]

huangapple
  • 本文由 发表于 2014年12月27日 02:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/27660446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定