简体   繁体   English

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

[英]Java Insertion Sorting with ArrayLists of Strings

I have an assignment on sorting which requires me to sort a list of random words by putting letters that start with the same letter in a group/zones and sorting that group alphabetically.我有一个关于排序的任务,要求我通过将以相同字母开头的字母放在组/区域中并按字母顺序对该组进行排序来对随机单词列表进行排序。 My code sorts the words but my problem is that some of the words have changed.我的代码对单词进行了排序,但我的问题是某些单词已经改变。 For example instead of having an output as例如,而不是将 output 作为

  • a ngela安吉拉
  • A PPLE一个苹果
  • A pple一个苹果
  • a pple一个苹果
  • B aboon狒狒
  • B all全部
  • C at C
  • c at c
  • P INK粉墨
  • P ink粉笔
  • S teve史蒂夫

I would have an output of:我会有一个 output :

  • apple苹果
  • apple苹果
  • apple苹果
  • apple苹果
  • Ball
  • Ball
  • cat
  • cat
  • Pink粉色的
  • PINK粉色的
  • Steve史蒂夫

As you can see, some of the words have been changed and in some cases, words with a capital letter are turned into lower cases like "cat" and I can't seem to find where my mistake is.正如你所看到的,一些单词已经改变,在某些情况下,大写字母的单词变成了小写字母,比如“cat”,我似乎找不到我的错误在哪里。

This is my sorting code;这是我的排序代码; my driver class just takes in the list of random words:我的驱动程序 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);
        }
    }
}

Reading your code and viewing your results, it seems that your code overwrites the values instead of swapping them.阅读您的代码并查看您的结果,您的代码似乎覆盖了这些值而不是交换它们。 To fix this you need to take a look at the function sort.要解决此问题,您需要查看 function 排序。 I have modified your code so that instead of overwriting, you swap the two elements:我已经修改了您的代码,以便您交换两个元素,而不是覆盖:

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;
    }
}

I hope this fixed your problem.我希望这解决了你的问题。

If I compare your sortZone implementation with a reference insertion sort implementation such as https://www.baeldung.com/java-insertion-sort I see the following differences - see inline comments如果我将您的 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