简体   繁体   中英

Sorting an array of string data using the insertion sort method

I'm having trouble using insertion to sort an array of strings.

When I compile the following code:

public class Project1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String names[]=new String[5];
        int size=names.length;
        System.out.println("Enter the 5 car manufacturers: ");
        //Load Array
        for (int i = 0; i < 5; i++) {
            names[i] = input.nextLine();            
        }

        //Print descending order list
        String[] descSort;
        descSort=bubbleSortDesc(names);
        System.out.println("Car manufacturers listed sorted in descending order (via BubbleSort): ");
        for (int x=0; x < names.length; x++) {
            System.out.println(names[x]);
        }

        //Print ascending order list
        insertionSortAsc(names, size);
        System.out.println("Car manufacturers listed sorted in ascending order (via InsertionSort): ");
        for (int z=0; z < names.length; z++) {
            System.out.println(names[z]);
        }
    }¨

    public static String[] bubbleSortDesc(String[] names) {
        String temp;
        int passNum, i, result;
        for (passNum=1; passNum <= 4; passNum++) {
            for (i = 0; i<=(4-passNum); i++) {
                result=names[i].compareToIgnoreCase(names[i+1]);
                if(result<0) {
                    temp=names[i];
                    names[i]=names[i+1];
                    names[i+1]=temp;
                }
            }
        }
        return names;
    }

    public static void insertionSortAsc(String[] names, int i) {
        String temp = names[i];
        int j = i-1;
        while (j >= 0 && names[j].compareToIgnoreCase(temp) > 0) {
            names[j+1]=names[j];
            j--;
        }
        names[j+1]=temp; 
    }

    public static void insertionSort(String[] names, int n) {
        for(int i = 1; i<n; i++) {
            insertionSortAsc(names, i);
        }
    }
}

It gives me the error:

cannot find symbol- method insert(java.lang.String[], int)

I suspect it has something to do with the fact that we were told to use our book as reference for the code, yet the book only deals with sorting data of type int and there are no examples for sorting string data.

Any help is appreciated.

Edit: After fixing the error, the program compiles and executes but after inputting the data it crashes and gives me the following error

java.lang.ArrayIndexOutofBoundsException:
5

This error highlights the line String temp = names[i]

您尚未定义名为 insert 的方法。

This will work the way you intend:

public static void insertionSortAsc(String[] names, int n)
{
    for(int i = 1; i<n; i++)
    {
        insert(names, i);
    }
}

public static void insert(String[] names, int i)
{
    String temp = names[i];
    int j = i - 1;

    while (j >= 0 && names[j].compareToIgnoreCase(temp)  > 0)
    {
        names[j + 1]= names[j];
        j--;
    }
    names[j + 1] = temp;
}
public static void insertionSort(int... arr) {
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] >= arr[i - 1])
            continue;

        int j = i - 1;

        for (; j >= 0; j--)
            if (arr[j] < arr[i])
                break;

        int tmp = arr[i];
        System.arraycopy(arr, j + 1, arr, j + 2, i - j - 1);
        arr[j + 1] = tmp;
    }
}

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