简体   繁体   中英

Lexicographic quicksort algorithm

My professor gave me a code for the methods to be used in sorting an array of names lexicographically, but I have no idea how what to write inside the main class to show that the program works. I am very new to java, so please if you know how to do this could you write it as simple as possible for me to understand it. Thanks in advance.

This is are the classes

public class quicksort_class {

int[] array1 = new int[11];


public quicksort_class(int[] w)
{
    array1 = w;
}

private static void sort(String[] string, int leftlimit, int rightlimit) {

    if (rightlimit > leftlimit) 
    {
    int midpoint = partitionstep(string, leftlimit, rightlimit);

    sort(string, leftlimit, midpoint - 1);

    sort(string, midpoint, rightlimit);
    }
}

public static int partitionstep(String[] string, int leftlimit, int rightlimit)
{   
    String midpoint = string[rightlimit];

    int lpointer = leftlimit;

    int rpointer = rightlimit;

    String temp = "";

    while(string[lpointer].compareTo(midpoint) <= 0)
    {
        lpointer = lpointer ++;
    }
    while(string[rpointer].compareTo(midpoint) > 0)
    {
        rpointer = rpointer --;
    }

    if(lpointer > rpointer)
    {
        temp = string[lpointer];

        string[lpointer] = string[rightlimit];

        string[rpointer] = temp;

        System.out.println(string);
    }

    while(lpointer < rpointer)
    {
        temp = string[lpointer];

        string[lpointer] = string[rightlimit];

        string[rightlimit] = temp;
    }

    return lpointer;
   }
}

This is the main class (as you can see I have no idea what to write)

package quicksort;

public class Quicksort {

public static void main(String[] args) {


 }
}

Write something that sets up an array of strings and calls sort against it, then prints out the results or checks them against a known good result.

Ideally, write something which does this repeatedly, with particular emphasis on checking unusual combinations (already sorted or sorted in reverse, null in the array, same value appearing several times or all values being identical...)

If you want to go beyond that, you need to dig into the code to understand its edge cases and specifically test those, and/or do a "code coverage" analysis (there are tools to help with that) to make sure all parts of the code have been exercised.

Assume the algorithm of sort method is correct: 1. If the main method is within the body of quicksort_class, you can directly call the sort method as sort(arrayToBeSorted, 0 , arrayToBeSorted.length-1). And the arrayToBeSorted should ordered lexicographically after your call. You can check that to confirm. 2. If the main method is in other class, as your main method currently, you need to at least change the private prefix of sort method to public, and call quicksort_class.sort(arrayToBeSorted, 0 , arrayToBeSorted.length-1).

Some tips: 1. Private prefix of method definition means this method can only be called inside current class body. 2. Static prefix of method definition means you should call this method via class name directly, instead of via a instance of class.

By the way, can you provide what the array1 attribute stands for? I don't get why it's there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM