Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Help to fix this please (https://www.askmehelpdesk.com/showthread.php?t=743881)

  • Apr 11, 2013, 08:35 PM
    adil_chaudhry
    help to fix this please
    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;
    }
    }

  • All times are GMT -7. The time now is 12:43 PM.