2017-01-10 7 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

正しい: 自衛隊sdfsfwerの アウト:で

sdf sdfsfwer

+1

デバッグを試したときに何が起こりましたか? – shmosel

答えて

1

あなたのコードを見直し、2つの新しいメソッドを作成しました。 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