2017-01-10 53 views
0

我从用户扫描了字符串。下一步是按照文本的长度对数组进行排序,我不知道我在做什么错误,有时它正在工作。通过字符串的长度在数组中快速排序

public static void quickSort(String[] subtitles, int start, int end) { 
    int i = start; 
    int j = end; 
    if (j - i >= 1) { 
     String pivot = subtitles[i]; 
     while (j > 1) { 
      while (subtitles[i].compareTo(pivot) <= 0 && i < end && j > i) 
       i++; 

      while (subtitles[j].compareTo(pivot) >= 0 && j > start && j >= i) 
       j--; 

      if (j > i) 
       swap(subtitles, i, j); 
     } 

     swap(subtitles, start, j); 
     quickSort(subtitles, start, j - 1); 
     quickSort(subtitles, j + 1, end); 
    } else 
     return; 
} 

public static void swap(String[] a, int i, int j) { 
    String tmp = a[i]; 
    a[i] = a[j]; 
    a[j] = tmp; 
} 

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    int amountStrings = 3; 
    String[] subtitles = new String[amountStrings]; 
    System.out.println("insert "); 
    for (int i = 0; i < amountStrings; i++) { 
     subtitles[i] = scan.next(); 
    } 

    System.out.println("--------"); 

    quickSort(subtitles, 0, subtitles.length - 1); 

    for (int i = 0; i < subtitles.length; i++) { 
     System.out.print(subtitles[i] + " "); 
    } 

错误:

在: asdzxc ASD ZXC

日期: ASD asdzxc ZXC

正确:

在: SDF sdfsfwer小号 日期:

sdf sdfsfwer

+1

当您尝试调试时发生了什么? – shmosel

回答

1

好的,我回顾了你的代码,并提出了两种新的方法。按字母顺序对数组进行排序,并通过计算数组中每个字的字母数量来排序。什么方法适合你,取决于你。 测试和工作。

import java.util.Arrays; 
import java.util.Comparator; 
import java.util.Scanner; 

public class Subtitles { 

    public static void sortAlfabetical(String x[]) { 
     int j; 
     boolean found = true; // will determine when the sort is finished 
     String temp; 

     while (found) { 
      found = false; 
      for (j = 0; j < x.length - 1; j++) { 
       if (x[j].compareToIgnoreCase(x[j + 1]) > 0) { // ascending sort 
        temp = x[j]; 
        x[j] = x[j + 1]; // swap 
        x[j + 1] = temp; 
        found = true; 
       } 
      } 
     } 

     for (int i = 0; i < x.length; i++) { 
      System.out.print(x[i] + " "); 
     } 
    } 



    public static void compare(String[] arrayOne) { 

     Arrays.sort(arrayOne, new Comparator<String>() { 

      @Override 
      public int compare(String o1, String o2) { 
       return o1.length() - o2.length(); 
      } 
     }); 

     for (String s : arrayOne) { 
      System.out.print(s + " "); 
     } 

    } 




    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     int amountStrings = 3; 
     String[] subtitles = new String[amountStrings]; 
     System.out.println("insert "); 
     for (int i = 0; i < amountStrings; i++) { 
      subtitles[i] = scan.next(); 
     } 

     System.out.println("--------"); 


     System.out.print("Sorting alphabetical: "); 
     sortAlfabetical(subtitles); 
     System.out.println(); 


     System.out.println("==========================="); 
     System.out.print("Sorting by word length: "); 
     compare(subtitles); 


    } 
} 
+0

好吧,如果我写汽车,大象,男孩它的工作,但是当我改变大象,男孩,汽车它不工作 – llooll

+0

修改。现在检查。它会工作 – zypa