合并按字母顺序排序的名称不起作用

时间:2018-09-25 11:06:06

标签: java sorting merge mergesort alphabetical

因此,我试图按字母顺序对文件中的名称进行排序,但它没有提供正确的输出。输入5个项目时,输出应为:

英属维尔京群岛,香港,卢森堡,马尔代夫,新西兰

相反,它给出了不正确的输出:

卢森堡,新西兰,马尔代夫,新西兰,卢森堡

注意:起始索引是索引1,而不是索引0

        import java.io.*;
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.util.Scanner;
        public class triall
        {
           public static void main(String[] args)
              {
              Scanner sc = new Scanner (System.in);
                Scanner in = new Scanner (System.in);
                System.out.print("Enter maximum number of items: ");
                int num = sc.nextInt();
                String [] arr1 = new String [num+1];
                //String [] arr2 = new String [num+1]; 
                String sLine = "";
                int i;

                try 
                { 
                    FileInputStream fstream = new FileInputStream("ioc.txt");
                    BufferedReader bin = new BufferedReader(new InputStreamReader(fstream));
                    System.out.format("%-10s%-30s%-30s", "Index", "Array 1", "Array 2");
                    System.out.println("");
                    for ( i = 1; sLine  != null && i <= num; i++)
                    {
                        sLine = bin.readLine();
                        arr1[i] = sLine;
                        arr2[i] = sLine;
                        if (sLine == null)
                            System.out.println("EOF");
                        else
                        {   System.out.format("%-10d", i);
                            System.out.format("%-30s", arr1[i]);
                            System.out.format("%-30s\n", arr2[i]);
                        }    
                    }

                    sort (arr1);

                } // end try

                catch (IOException e) 
                {
                    e.printStackTrace();
                }
           }


              public static void merge(String[] a, int from,  int mid, int to)
              {  int n1 = (mid - from + 1);
                 int n2 = to - mid;
                    // size of the range to be merged


                 String[] left = new String[n1 + 1];
                 String [] right = new String[n2 + 1];
                 int i = 1;
                 while ( i <= n1)
                 {
                     left [i] = a[from + i - 1];
                 }

                 int j = 1;
                 while (j <= n2)
                 {
                     right [j] = a[mid + j];
                 }

                 i = 1;
                 j = 1;
                 int k = from;

                 while (k <= to)
                 {
                     if (left[i].compareToIgnoreCase(right[j]) <= 0)
                     {
                         a[k] = left[i];
                         i++;
                     }
                     else
                     {
                         a[k] = right[j];
                         j++;
                     }
                 }

                 for (i=1; i<=to; i++)
                        System.out.println(a[i]);
              }


              public static void mergeSort(String [] a, int from, int to)
              {  if (from < to)
              {int mid = (int) (Math.floor((from + to) / 2));
                  // sort the first and the second half
                 mergeSort(a, from, mid);
                 mergeSort(a, mid + 1, to);
                 merge(a, from, mid, to);}
              }


              public static void sort(String[] a)
              {  mergeSort(a, 1, a.length - 1);
           }
        }

3 个答案:

答案 0 :(得分:0)

您可以尝试一下!

 import java.io.*;
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.util.Scanner;
        public class triall
        {
           public static void main(String[] args)
              {
              Scanner sc = new Scanner (System.in);
                Scanner in = new Scanner (System.in);
                System.out.print("Enter maximum number of items: ");
                int num = sc.nextInt();
                String [] arr1 = new String [num+1];
                String [] arr2 = new String [num+1]; 
                String sLine = "";
                int i;

                try 
                { 
                    FileInputStream fstream = new FileInputStream("ioc.txt");
                    BufferedReader bin = new BufferedReader(new InputStreamReader(fstream));
                    System.out.format("%-10s%-30s%-30s", "Index", "Array 1", "Array 2");
                    System.out.println("");
                    for ( i = 1; sLine  != null && i <= num; i++)
                    {
                        sLine = bin.readLine();
                        arr1[i] = sLine;
                        arr2[i] = sLine;
                        if (sLine == null)
                            System.out.println("EOF");
                        else
                        {   System.out.format("%-10d", i);
                            System.out.format("%-30s", arr1[i]);
                            System.out.format("%-30s\n", arr2[i]);
                        }    
                    }

                    sort (arr1);

                } // end try

                catch (IOException e) 
                {
                    e.printStackTrace();
                }
           }


                public static void mergeSort2(String[] names) {
                    if (names.length >= 2) {
                        String[] left = new String[names.length / 2];
                        String[] right = new String[names.length - names.length / 2];

                        for (int i = 0; i < left.length; i++) {
                            left[i] = names[i];
                        }

                        for (int i = 0; i < right.length; i++) {
                            right[i] = names[i + names.length / 2];
                        }

                        merge(names, left, right);
                    }
                }



            public static void merge(String[] names, String[] left, String[] right) {
                    int a = 0;
                    int b = 0;
                    for (int i = 0; i < names.length; i++) {
                        if (b >= right.length || (a < left.length && left[a].compareToIgnoreCase(right[b]) < 0)) {
                            names[i] = left[a];
                            a++;
                        } else {
                            names[i] = right[b];
                            b++;
                        }
                    }
                }
              public static void sort(String[] a)
              {  mergeSort2(a);
           }
        }

答案 1 :(得分:0)

您可以使用TreeSet避免重复并获得排序结果。

public static void main(String[] args) {

    Set<String> result = new TreeSet<>();

    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ioc.txt")))) {
        String line;
        while ((line = br.readLine()) != null) {
            result.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(result);
    // If you want result as String array 
    String[] array = result.toArray(new String[result.size()]);
    System.out.println(Arrays.toString(array));

}

答案 2 :(得分:-1)

我不知道您的问题的答案,但我编写的代码类似于您的代码。也许可以帮到您。

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    ArrayList<String> words = new ArrayList<String>();

    System.out.println("Enter the word or write STOP to exit:");
    while (in.hasNext()) {
        String inputLine = in.nextLine();
        if (inputLine.equalsIgnoreCase("STOP")) {
            break;
        }
        words.add(inputLine);
    }

    Collections.sort(words);

    System.out.println("The words sorted:");
    System.out.println(words);

    in.close();
}