PDA

View Full Version : Help to fix this please


adil_chaudhry
Apr 11, 2013, 08:35 PM
This is what I have so far for quick sort but its keep giving me error. Can someone please help me Thanks



public class Sorting{

private static void swap (Comparable[] data, int index1, int index2)
{
Comparable temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}

public static void insertionSort (Comparable[] data)
{
for (int index = 1; index < data.length; index++) {
Comparable key = data[index];
int position = index;

while (position > 0 && data[position-1].compareTo(key) > 0) {
data[position] = data[position-1];
position--;
}

data[position] = key;
}
}

public static void quickSort (Comparable[] data, int min, int max)
{
int pivot;

if (min < max)
{
pivot = partition (data, min, max); // make partitions
quickSort(data, min, pivot-1); // sort left partition
quickSort(data, pivot+1, max); // sort right partition
}
}
//-----------------------------------------------------------------
// Creates the partitions needed for quick sort.
//-----------------------------------------------------------------
private static int partition (Comparable[] data, int min, int max)
{
// Use first element as the partition value
Comparable partitionValue = data[min];

int left = min;
int right = max;

while (left < right)
{
// Search for an element that is > the partition element
while (data[left].compareTo(partitionValue) <= 0 && left < right)
left++;

// Search for an element that is < the partitionelement
while (data[right].compareTo(partitionValue) > 0)
right--;

if (left < right)
swap(data, left, right);
}

// Move the partition element to its final position
swap (data, min, right);

return right;
}
}