簡體   English   中英

Java 使用字符串的 ArrayLists 進行插入排序

[英]Java Insertion Sorting with ArrayLists of Strings

我有一個關於排序的任務,要求我通過將以相同字母開頭的字母放在組/區域中並按字母順序對該組進行排序來對隨機單詞列表進行排序。 我的代碼對單詞進行了排序,但我的問題是某些單詞已經改變。 例如,而不是將 output 作為

  • 安吉拉
  • 一個蘋果
  • 一個蘋果
  • 一個蘋果
  • 狒狒
  • 全部
  • C
  • c
  • 粉墨
  • 粉筆
  • 史蒂夫

我會有一個 output :

  • 蘋果
  • 蘋果
  • 蘋果
  • 蘋果
  • 粉色的
  • 粉色的
  • 史蒂夫

正如你所看到的,一些單詞已經改變,在某些情況下,大寫字母的單詞變成了小寫字母,比如“cat”,我似乎找不到我的錯誤在哪里。

這是我的排序代碼; 我的驅動程序 class 只接受隨機單詞列表:

import java.util.ArrayList;
import java.util.Collections;

public class ZoneSort 
{


ArrayList[] arrayOfZones;
ArrayList<String> words; 

public ZoneSort(ArrayList<String> words)
{
    arrayOfZones = new ArrayList [ 26 ];
    
    for(int index = 0; index < 26;index++)
        arrayOfZones [ index ] = new ArrayList();
    
    this.words = words; 
    
    putWordsIntoZones();
}

private  void putWordsIntoZones()
{
    for(String word: words)
    {
        int index = Character.toLowerCase(word.charAt(0)) - 97; 
        ArrayList<String> zoneAtIndex = arrayOfZones[index];
        zoneAtIndex.add(word);
    }
}

public  void sortTheArrayOfZones() 
    {
        for(ArrayList<String> zone : arrayOfZones )
        {
            sortZone(zone);
        }
    }


private  void sortZone(ArrayList<String> zone)
{
        for(int i = 1; i < zone.size(); i++)
        {
            String key = zone.get(i);
            int j = i-1;
            while(j>=0 && key.compareTo(zone.get(j)) > 0)
            {
                String x = zone.get(j+1);
                zone.set(j, x);
                j--;
            }
            
            String x = zone.get(j+1);
            x = key;
        }
}   

public   void printArrayOfZones()
{
    System.out.println("The sorted words are");
    for(ArrayList<String> zone:arrayOfZones)
    {
        for(String word: zone)
        {
            
            System.out.println(word);
        }
    }
}

閱讀您的代碼並查看您的結果,您的代碼似乎覆蓋了這些值而不是交換它們。 要解決此問題,您需要查看 function 排序。 我已經修改了您的代碼,以便您交換兩個元素,而不是覆蓋:

private  void sortZone(ArrayList<String> zone){
    for(int i = 1; i < zone.size(); i++){
        String key = zone.get(i);
        int j = i-1;
        while(j>=0 && key.compareTo(zone.get(j)) > 0){
            String x = zone.get(j+1);
            zone.set(j+1,zone.get(j)); // line added
            zone.set(j, x);
            j--;
        }
        String x = zone.get(j+1);
        x = key;
    }
}

我希望這解決了你的問題。

如果我將您的 sortZone 實現與參考插入排序實現(例如https://www.baeldung.com/java-insertion-sort )進行比較,我會看到以下差異 - 請參閱內聯注釋

        for(int i = 1; i < zone.size(); i++)
        {
            String key = zone.get(i);
            int j = i-1;

// The sort order is reversed.
// You've used "key > zone[j]" when it should be "key < zone[j]"

            while(j>=0 && key.compareTo(zone.get(j)) < 0)
            {
// This is copying items backwards, towards the beginning of the array. 
//                String x = zone.get(j+1);
//                zone.set(j, x);
// It should be copying items forwards, towards the end, to make room for "key"
// Like this:
                String x = zone.get(j);
                zone.set(j+1, x);
                j--;
            }
            
// You should be setting zone[j+1] = "key" - this does not do it: 
//            String x = zone.get(j+1);
//            x = key;
// This is how you set a value in a list:
            zone.set(j+1, key);
          
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM