StackOverFlow error for QuickSort algorithm

I get a StackOverflowError for this code. Lines 184/185 are written in it, where I initialize the split position (see below) and are called by the first recursive quickSort method. I see that the code is having problems exiting recursion, but I'm not sure where this is going. Each time I call quickSort, it is on a smaller section.

import java.util.*;

public class java2{

public static int MAXINT = 10000;
public static int[] intArray = new int[MAXINT];
public static int index;
public static long comparisons;

public static void main(String[] args)
{       
    System.out.println("SORTING ALGORITHM: Quicksort");
    // Create a random array of integers and sort using the CombSort algorithm
    // Print the number of items and comparisions

    for(index = 10; index <= 10000; index = index * 10)
    {
        if (index == 10)
            for(int i = 0; i < index; i++)
                System.out.print(intArray[i] + " ");
        comparisons = 0;
        generate(intArray, index);
        quickSort(intArray, 0, index - 1);
        output(comparisons);
    }
}

// Generate an array of random values between 0 and 10000
public static void generate(int[] valueArray, int count)
{
    Random generator = new Random();

    for(int temp = 0; temp < count; temp++)
    {
        valueArray[temp] = generator.nextInt(MAXINT) + 1;
    }
}

// Print the number of values in the array and the number of comparisons
public static void output(long count)
{

    System.out.println("Number of values in array: " + index);
    System.out.println("Number of comparisons required: " + count);
    System.out.println();
}

//Swap the given values and then assign them to the correct place in the array 
public static void swap(int[] value, int i, int j)
{
    int temp = value[i];
    value[i] = value[j];
    value[j] = temp;        
}

//Implement Quicksort algorithm
public static void quickSort(int[] value, int startIndex, int endIndex)
{
    int r = endIndex;
    int l = startIndex;
    int s;

    if (l < r)
    {
        s = partition(intArray, l, r);
        quickSort(intArray, l, s - 1); // StackOverflowError here
        quickSort(intArray, s + 1, r);
    }
}

//Partition an array into two parts
public static int partition(int[] value, int startIndex, int endIndex)
{       
    int r = endIndex;
    int l = startIndex;
    int p = value[l];
    int i = l;
    int j = r + 1;

    while(i < j)
    {
        while(value[i] < p)
        {
            i++;
            comparisons++;
        }

        while(value[j] > p)
        {
            j--;
            comparisons++;
        }

        swap(value, i, j);
    }

    swap(value, i, j);
    swap(value, l, j);

    return j;
}
} // end main
+4
source share
2 answers

Restructuring the partitioning method, the problem has been fixed:

    public static int partition(int[] value, int p, int r)
    {
            int x = value[p];
            int i = p - 1;
            int j = r + 1 ;

            while (true)
            {
                    do
                    {
                        j--;
                        comparisons++;
                    }
                    while (value[j] > x);

                    do
                    {
                        i++;
                        comparisons++;
                    }
                    while (value[i] < x);

                    if (i < j)
                    {
                        swap(value, i, j);
                    }
                    else
                            return j;
            }
    }
0
source

Here are a few things you can start with debugging.

swap, . , void swap(int, int, int, int), , value. - :

public static void swap(int[] value, int i, int j) {
    int temp = value[i];
    value[i] = value[j];
    value[j] = temp;
}

:

swap(value, i, j);

= 10. , , . , .

, , !

+5

Source: https://habr.com/ru/post/1615347/


All Articles