Hoare's partitioning scheme golang

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

Hoare's partitioning scheme golang

问题

使用Hoare的划分方案实现快速排序,但是我无法弄清楚我做错了什么。它陷入了一个无限循环,总是内存不足

// Hoare的划分方案
func PartitionHoare(arr []int, low, high int) int {
    length := len(arr)

    if length == 0 {
        panic("数组大小为0")
    }

    pivot := arr[low]
    i := low - 1
    j := high + 1

    for {
        for {
            j--
            if arr[j] > pivot {
                break
            }
        }

        for {
            i++
            if arr[i] < pivot {
                break
            }
        }

        if i < j {
            Swap(&arr, i, j)
        } else {
            return j
        }
    }
}

// 排序
func SortHoare(arr []int, low, high int) {
    if low < high {
        p := PartitionHoare(arr, low, high)
        SortHoare(arr, low, p)
        SortHoare(arr, p+1, high)
    }
}


// 交换 i <--> j
func Swap(arr *[]int, i, j int) {
    (*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
}

尝试使用Hoare的划分方案实现快速排序,但是无法解决问题。它陷入了一个无限循环,并且总是内存不足

英文:

> Quicksort with Hoare's partitioning

// Hoare&#39;s partitioning scheme
func PartitionHoare(arr []int, low, high int) int {
	length := len(arr)

	if length == 0 {
		panic(&quot;Array size is 0&quot;)
	}

	pivot := arr[low]
	i := low - 1
	j := high + 1

	for {
		for {
			j--
			if arr[j] &gt; pivot {
				break
			}
		}

		for {
			i++
			if arr[i] &lt; pivot {
				break
			}
		}

		if i &lt; j {
			Swap(&amp;arr, i, j)
		} else {
			return j
		}
	}
}

// Sort
func SortHoare(arr []int, low, high int) {
	if low &lt; high {
		p := PartitionHoare(arr, low, high)
		SortHoare(arr, low, p)
		SortHoare(arr, p+1, high)
	}
}


// Swap i &lt;--&gt; j
func Swap(arr *[]int, i, j int) {
	(*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
}

Trying to implement Quicksort using Hoare's partitioning but can't figure out what am I doing wrong. It is stuck in an infinite loop, always runs out of memory

fatal error: runtime: out of memory

答案1

得分: 2

在查找i和j的位置进行交换时,应该使用非严格不等式。所以,不要使用:

        if arr[j] > pivot {
            break
        }

而应该使用:

        if arr[j] >= pivot {
            break
        }

对于i也是一样。不要使用:

        if arr[i] < pivot {
            break
        }

而应该使用:

        if arr[i] <= pivot {
            break
        }

此外,我不确定这是否是有意为之,但是目前你的算法是按降序排序的。如果你想按升序排序,请交换i和j之间的比较。所以:

    if arr[j] <= pivot {
        break
    }

    if arr[i] >= pivot {
        break
    }
英文:

You should use non strict inequalities while looking for position of i and j to do the swap. So instead of

        if arr[j] &gt; pivot {
            break
        }

you should have

        if arr[j] &gt;= pivot {
            break
        }

And the same for i. Instead of

        if arr[i] &lt; pivot {
            break
        }

use

        if arr[i] &lt;= pivot {
            break
        }

Also I'm not sure if it's intentional or not, but currently your algorithm sorts in descending order. If you want to sort in ascending order swap the comparitions between i and j. So:

    if arr[j] &lt;= pivot {
        break
    }

and

    if arr[i] &gt;= pivot {
        break
    }

huangapple
  • 本文由 发表于 2017年9月4日 00:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/46025760.html
匿名

发表评论

匿名网友

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

确定