0
0
DSA Goprogramming~20 mins

Quick Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Quick Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Quick Sort Partition Step
What is the output of the array after the partition step in Quick Sort using the last element as pivot?
DSA Go
func partition(arr []int, low, high int) int {
    pivot := arr[high]
    i := low - 1
    for j := low; j < high; j++ {
        if arr[j] < pivot {
            i++
            arr[i], arr[j] = arr[j], arr[i]
        }
    }
    arr[i+1], arr[high] = arr[high], arr[i+1]
    return i + 1
}

arr := []int{10, 7, 8, 9, 1, 5}
pi := partition(arr, 0, len(arr)-1)
fmt.Println(arr)
A[1 5 7 8 9 5]
B[1 5 7 9 8 10]
C[1 5 7 8 9 10]
D[10 7 8 9 1 5]
Attempts:
2 left
💡 Hint
Focus on how elements smaller than the pivot are moved before it.
🧠 Conceptual
intermediate
1:00remaining
Quick Sort Average Time Complexity
What is the average time complexity of the Quick Sort algorithm?
AO(n^2)
BO(n log n)
CO(log n)
DO(n)
Attempts:
2 left
💡 Hint
Think about how the array is divided and conquered.
Predict Output
advanced
2:00remaining
Final Sorted Array After Quick Sort
What is the final sorted array after applying Quick Sort on the given array?
DSA Go
func quickSort(arr []int, low, high int) {
    if low < high {
        pi := partition(arr, low, high)
        quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)
    }
}

func partition(arr []int, low, high int) int {
    pivot := arr[high]
    i := low - 1
    for j := low; j < high; j++ {
        if arr[j] < pivot {
            i++
            arr[i], arr[j] = arr[j], arr[i]
        }
    }
    arr[i+1], arr[high] = arr[high], arr[i+1]
    return i + 1
}

arr := []int{4, 2, 6, 9, 3}
quickSort(arr, 0, len(arr)-1)
fmt.Println(arr)
A[2 3 4 6 9]
B[4 2 3 6 9]
C[9 6 4 3 2]
D[2 4 3 6 9]
Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Quick Sort Partition
What error will this Quick Sort partition code cause when run?
DSA Go
func partition(arr []int, low, high int) int {
    pivot := arr[high]
    i := low
    for j := low; j < high; j++ {
        if arr[j] <= pivot {
            arr[i], arr[j] = arr[j], arr[i]
            i++
        }
    }
    arr[i], arr[high] = arr[high], arr[i]
    return i
}
AIncorrect partition index causing wrong sort
BIndex out of range error
CInfinite loop
DNo error, works correctly
Attempts:
2 left
💡 Hint
Check how the initial value of i affects the partitioning.
🚀 Application
expert
1:30remaining
Quick Sort Stability Check
Given the array of structs [{value: 3, id: 'a'}, {value: 3, id: 'b'}, {value: 1, id: 'c'}], which statement about Quick Sort is true?
AQuick Sort is stable and keeps the order of equal elements
BQuick Sort cannot sort structs
CQuick Sort always sorts in descending order
DQuick Sort is unstable and may change the order of equal elements
Attempts:
2 left
💡 Hint
Think about how Quick Sort swaps elements during partition.