What is the relation between "sorting an array with only two distinct elements" and quicksort
I'm learning quicksort from Sedgwick's book. One of his exercise problems is
Write a program th开发者_如何学Goat sorts an array that is known to contain just two distinct key values.
View Solution
I think I understand the solution. It runs in O(n). I don't however understand the relation between this and quicksort? I fail to see any sort of partitioning like in quick sort. All that is done here is deposit lesser and greater elements beyond respective boundaries pointed to by lt
and gt
.
Solution is just like quicksort whit single pass.
Quick sort pseudo code:
1. Check stopping condition
2. Pick one number called pivot
3. Move smaller or equal elements than pivot on left part of array
4. Move greater elements than pivot on right part of array
5. Quicksort left part
6. Quicksort right part
Where soultion you looking for is same as quicsort exept 1,5,6
1. Pick one number called pivot
2. Move smaller or equal elements than pivot on left part of array
3. Move greater elements than pivot on right part of array
精彩评论