given algorithm doesn’t sort the given array:
while (i < nums.length) {
int correct = nums[i] - 1;
if (nums[i] != nums[correct]) {
swap(nums, i, correct);
} else {
i = i + 1;
}
}
solution comes out correct regardless but shouldn’t we be worried that the cyclic sort is not implemented correctly?
Input array ->[4, 3, 4, 5, 1]
output of sort algo ->[1,4,3,4,5]
Course: Grokking Coding Interview Patterns in Java - Learn Interactively
Lesson: Solution: Find the Corrupt Pair
Course: Grokking Coding Interview Patterns in Java - Learn Interactively
Lesson: Solution: Find the Corrupt Pair