0
0
DSA Goprogramming~10 mins

Quick Sort Partition Lomuto and Hoare in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap two elements in the array.

DSA Go
func swap(arr []int, i, j int) {
    temp := arr[[1]]
    arr[i] = arr[j]
    arr[j] = temp
}
Drag options to blanks, or click blank then click option'
Aj
Bi
C0
Dlen(arr)-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[j] instead of arr[i] for temp causes incorrect swapping.
Forgetting to store the value before overwriting.
2fill in blank
medium

Complete the code to select the pivot as the last element in Lomuto partition.

DSA Go
func lomutoPartition(arr []int, low, high int) int {
    pivot := arr[[1]]
    i := low - 1
    for j := low; j < high; j++ {
        if arr[j] <= pivot {
            i++
            swap(arr, i, j)
        }
    }
    swap(arr, i+1, high)
    return i + 1
}
Drag options to blanks, or click blank then click option'
Alow
Blen(arr)-1
C0
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing pivot as low or 0 index causes incorrect partitioning.
Using len(arr)-1 instead of high when partitioning subarrays.
3fill in blank
hard

Fix the error in the Hoare partition loop condition.

DSA Go
func hoarePartition(arr []int, low, high int) int {
    pivot := arr[low]
    i := low - 1
    j := high + 1
    for {
        for {
            i++
            if arr[i] [1] pivot {
                break
            }
        }
        for {
            j--
            if arr[j] [1] pivot {
                break
            }
        }
        if i >= j {
            return j
        }
        swap(arr, i, j)
    }
}
Drag options to blanks, or click blank then click option'
A>
B>=
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= causes infinite loops or wrong partition.
Mixing comparison directions for i and j.
4fill in blank
hard

Fill both blanks to complete the Lomuto partition swap and return statement.

DSA Go
func lomutoPartition(arr []int, low, high int) int {
    pivot := arr[high]
    i := low - 1
    for j := low; j < high; j++ {
        if arr[j] <= pivot {
            i++
            swap(arr, [1], j)
        }
    }
    swap(arr, [2], high)
    return i + 1
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping wrong indices causes incorrect partition.
Using j instead of i+1 in the final swap.
5fill in blank
hard

Fill all three blanks to complete the Hoare partition loop increments and swap.

DSA Go
func hoarePartition(arr []int, low, high int) int {
    pivot := arr[low]
    i := low - 1
    j := high + 1
    for {
        for {
            i++
            if arr[i] [1] pivot {
                break
            }
        }
        for {
            j--
            if arr[j] [2] pivot {
                break
            }
        }
        if i >= j {
            return j
        }
        swap(arr, [3], j)
    }
}
Drag options to blanks, or click blank then click option'
A<
B>
Ci
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping arr[j] and arr[j] instead of arr[i] and arr[j].
Using wrong comparison operators causing infinite loops.