arr[0], arr[i] = arr[i], arr[0]
26 }
27
28 for i := n - 1; i >= 0; i-- {
29 tmp := arr[0]30 arr[0] = arr[i]
31 arr[i] = tmp
32 heapify(arr, 0, i)
arr[i], arr[max] = arr[max], arr[i]
45 max = r
46 }
47 if i != max {
48 tmp := arr[i]49 arr[i] = arr[max]
50 arr[max] = tmp
51 heapify(arr, max, n)
arr[j], arr[j+1] = arr[j+1], arr[j]
25 flag := false
26 for j := 0; j < n - i - 1; j++ {
27 if arr[j] > arr[j + 1] {
28 tmp := arr[j]29 arr[j] = arr[j + 1];
30 arr[j + 1] = tmp
31 flag = true
tosort[i], tosort[i-1] = tosort[i-1], tosort[i]
37
38 for i := end; i > 0; i-- {
39 if tosort[i] < tosort[i-1] {
40 temp := tosort[i]41 tosort[i] = tosort[i-1]
42 tosort[i-1] = temp
43 flag = 1
tosort[i], tosort[i+1] = tosort[i+1], tosort[i]
23 for i := 0; i <end; i++ {
24 // fmt.Println(tosort[i])
25 if tosort[i] > tosort[i+1] {
26 temp := tosort[i]27 tosort[i] = tosort[i+1]
28 tosort[i+1] = temp
29 flag = 1
It is legal and idiomatic to swap values of two variables using "tuple assignment" instead of swapping them in multiple statements.
For Python, "tuple assignment" is a feature that allows a tuple of variables on the left of an assignment to be assigned values from a tuple on the right of the assignment. A similar assignment approach works for Go as well.
tmp := *x
*x = *y
*y = tmp
*x, *y = *y, *x