Amazon Interview Question
SDE-2sCountry: United States
Interview Type: Phone Interview
private static int partition(int[] array, int start, int end) {
int pivot = array[end];
int partitionIndex = start;
for(int i = start; i < end; i++)
{
if(array[i] <= pivot)
{
int temp = array[i];
array[i] = array[partitionIndex];
array[partitionIndex] = temp;
partitionIndex++;
}
}
int temp = array[partitionIndex];
array[partitionIndex] = array[end];
array[end] = temp;
return partitionIndex;
}
Am I right that it should be something like that? I assumed that the pivot element is the first element of the array
- Mike L November 06, 2016