冒泡排序中交换的正确终止条件是什么?

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

What is the correct terminating condition for swapping in bubble sort?

问题

我正在学习golang,并尝试编写冒泡排序算法并使用指针。以下是目前的代码示例:

package main

import (
	"fmt"
	"math/rand"
)

func main() {

	testTwo := make([]int, 10)
	for i := 0; i <= len(testTwo)-1; i++ {
		fmt.Print("\n")
		testTwo[i] = rand.Intn(10)
	}

	for i := 0; i <= len(testTwo)-1; i++ {
		for j := i + 1; j <= len(testTwo)-1; j++ {

			testTwo[i], testTwo[i+1] = swap(testTwo[i], testTwo[i+1])
		}
	}
}

/*
Swaps the pointers of two adjacent elements in an array
*/
func swap(valOne, valTwo int) (int, int) {

	valAddress := &valOne
	valAddressTwo := &valTwo

	if valOne <= valTwo {
		temp_address := *valAddressTwo
		*valAddressTwo = valOne
		*valAddress = temp_address

	} else {
		temp_address := *valAddress
		*valAddress = valTwo
		*valAddressTwo = temp_address
	}

	return valOne, valTwo
}

这是目前的代码示例。输入的切片可能是 [4 1 2 9 8 4 1 5 7 6],但排序过程中停止了。[1 4 9 2 4 8 5 1 6 7]。我是否需要添加其他条件,或者在交换函数中的指针使用是否有问题?

英文:

I am learning golang, and I am trying to work through writing bubblesort and working with pointers.

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
)

func main() {

	testTwo := make([]int, 10)
     	for i := 0; i &lt;= len(testTwo)-1; i++ {
		fmt.Print(&quot;\n&quot;)
		testTwo[i] = rand.Intn(10)
	}

	for i := 0; i &lt;= len(testTwo)-1; i++ {
		for j := i + 1; j &lt;= len(testTwo)-1; j++ {

			testTwo[i], testTwo[i+1] = swap(testTwo[i], testTwo[i+1])
		}
	}
}


/*
Swaps the pointers of two adjacent elements in an array
*/
func swap(valOne, valTwo int) (int, int) {

	valAddress := &amp;valOne
	valAddressTwo := &amp;valTwo

	if valOne &lt;= valTwo {
		temp_address := *valAddressTwo
		*valAddressTwo = valOne
		*valAddress = temp_address

	} else {
		temp_address := *valAddress
		*valAddress = valTwo
		*valAddressTwo = temp_address
	}

	return valOne, valTwo
}

This is an example of what is being done so far. The input slice might be
[4 1 2 9 8 4 1 5 7 6]. But it stops short of sorting everything.[1 4 9 2 4 8 5 1 6 7]. Is there another condition I should add or is there something wrong with how I am using the pointers in the swap function?

答案1

得分: 1

package main

import (
	"fmt"
	"math/rand"
)

func main() {

	testTwo := make([]int, 10)
	for i := 0; i <= len(testTwo)-1; i++ {
		fmt.Print("\n")
		testTwo[i] = rand.Intn(10)
	}

	for i := 0; i <= len(testTwo)-1; i++ {
		for j := i + 1; j <= len(testTwo)-1; j++ {
			if testTwo[i] > testTwo[j] {
                // 这里我们交换指针
				testTwo[i], testTwo[j] = swap(testTwo[i], testTwo[j])
			}
		}
	}

	fmt.Print(testTwo)
}

/*
GO 总是按值传递参数。除非我们使用 *int,否则无法在此处交换指针
*/
func swap(valOne, valTwo int) (int, int) {
	if valOne <= valTwo {
		return valOne, valTwo

	} else {
		return valTwo, valOne
	}
}
英文:
package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
)

func main() {

	testTwo := make([]int, 10)
	for i := 0; i &lt;= len(testTwo)-1; i++ {
		fmt.Print(&quot;\n&quot;)
		testTwo[i] = rand.Intn(10)
	}

	for i := 0; i &lt;= len(testTwo)-1; i++ {
		for j := i + 1; j &lt;= len(testTwo)-1; j++ {
			if testTwo[i] &gt; testTwo[j] {
                // here we swap pointers
				testTwo[i], testTwo[j] = swap(testTwo[i], testTwo[j])
			}
		}
	}

	fmt.Print(testTwo)
}

/*
GO always sends arguments by value. Pointers cannot be swapped here, unless we use *int
*/
func swap(valOne, valTwo int) (int, int) {
	if valOne &lt;= valTwo {
		return valOne, valTwo

	} else {
		return valTwo, valOne
	}
}

答案2

得分: -1

你忘记比较两个变量,并且在使用i+1而不是j时出错。

for i := 0; i <= len(testTwo)-1; i++ {
    for j := i + 1; j <= len(testTwo)-1; j++ {
        if testTwo[i] < testTwo[j] {
            testTwo[i], testTwo[j] = swap(testTwo[i], testTwo[j])
        }
    }
}
英文:

You forgot to compare 2 variables and you have mistaken using i+1 instead of j.

for i := 0; i &lt;= len(testTwo)-1; i++ {
	for j := i + 1; j &lt;= len(testTwo)-1; j++ {
		if testTwo[i] &lt; testTwo[j] {
			testTwo[i], testTwo[j] = swap(testTwo[i], testTwo[j])
		}
	}
}

huangapple
  • 本文由 发表于 2022年5月31日 03:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/72439128.html
匿名

发表评论

匿名网友

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

确定