按字母顺序从数组中插入排序字符串

时间:2015-01-09 17:07:01

标签: java arrays sorting

我真的不是100%确定如何从数组中按字母顺序插入排序。这是我到目前为止所得到的任何帮助。

我正在尝试使用插入排序算法在此项目中按字母顺序排序。

我在排序和运行时错误方面遇到了一些错误。

谢谢!

// The "Insertion_Sort_Example" class.
import java.awt.*;
import java.io.*;
import java.util.*;

public class SortPlanets
{





    public static void main (int [] args)
    {
    String Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto ;
    String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};   // Array holding contents

    System.out.println ("Array contents before sorting...");  // simple print statements to show proof of before sort
    for(int i = 0; i < 5; i++) {
        System.out.println(list[i]);
    }

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

    insertSort (list);   // call to insert function

    System.out.println ("************************************");   // insert after
    System.out.println ("Array contents after sorting...");
    for(int i = 0; i < 5; i++) {
        System.out.println(list[i]);
    }

    // Place your program here.  'c' is the output console
    } // main method


    public static void insertSort (String [] list)  // sort function
    {
    for (int top = 1 ; top < list.length ; top++)
    {
        int item = list [top];
        int i = top;
        while (i > 0 && item < list [i - 1])
        {
          list [i] = list [i - 1];
          i--;
        }
        list [i] = item;
        System.out.print (list [0]);
        System.out.print (" ");
        System.out.print (list [1]);
        System.out.print (" ");
        System.out.print (list [2]);
        System.out.print (" ");
        System.out.print (list [3]);
        System.out.print (" ");
        System.out.print (list [4]);
        System.out.println ();
    }
    }
} // Insertion_Sort_Example class

5 个答案:

答案 0 :(得分:1)

java.util.Collections.sort(list);

这将按字母顺序排列列表

编辑:Atish有更完整的答案

答案 1 :(得分:1)

String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};

使用list方法

对列表java.util.Collections进行排序
java.util.Collections.sort(list);

sort() api对Natural Ordering (sort alphabetically)中的数组进行排序。

答案 2 :(得分:1)

我刚刚清理了你的代码,以便它可以运行 - 你走了:

我注意到的一些事情 - 当你初始化你的行星时 - 确保你初始化它们所以它们并非都是空的,例如String Earth =&#34; Earth&#34 ;,而不仅仅是String Earth。比较排序中的字符串时 - 请确保使用.compareTo()方法。你不能使用&lt;或者&gt;比较String对象。除此之外,你有点亲近。 GJ队友。

package com.cudp.cuprodigy.utils;

import java.awt.*;
import java.io.*;
import java.util.*;

public class SortPlanets
{





    public static void main (String [] args)
    {
        String Mercury="Mercury", Venus="Venus", Earth="Earth", Mars="Mars", Jupiter="Jupiter", Saturn="Saturn", Uranus="Uranus", Neptune="Neptune", Pluto="Pluto" ;
        String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};

        System.out.println ("Array contents before sorting...");
        System.out.print (list [0]);
        System.out.print (" ");
        System.out.print (list [1]);
        System.out.print (" ");
        System.out.print (list [2]);
        System.out.print (" ");
        System.out.print (list [3]);
        System.out.print (" ");
        System.out.print (list [4]);
        System.out.println ("");
        System.out.println ("************************************");
        System.out.println ("PLEASE. NOT THE ASS.");

        SortPlanets.insertSort (list);

        System.out.println ("************************************");
        System.out.println ("Array contents after sorting...");
        System.out.print (list [0]);
        System.out.print (" ");
        System.out.print (list [1]);
        System.out.print (" ");
        System.out.print (list [2]);
        System.out.print (" ");
        System.out.print (list [3]);
        System.out.print (" ");
        System.out.print (list [4]);





        // Place your program here.  'c' is the output console
    } // main method


    public static void insertSort (String [] list)
    {
        for (int top = 1 ; top < list.length ; top++)
        {
            String item = list [top];
            int i = top;
            while (i > 0 && item.compareTo(list [i - 1]) < 0)
            {
                list [i] = list [i - 1];
                i--;
            }
            list [i] = item;
            System.out.print (list [0]);
            System.out.print (" ");
            System.out.print (list [1]);
            System.out.print (" ");
            System.out.print (list [2]);
            System.out.print (" ");
            System.out.print (list [3]);
            System.out.print (" ");
            System.out.print (list [4]);
            System.out.println ();
        }
    }
} // Insertion_Sort_Example class

答案 3 :(得分:1)

尝试以下代码:

import java.util.Arrays;
    public class Mainclass {        
        public static void main(String[] args) {
            String list [] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
            Arrays.sort(list);
            for(String str:list)
                System.out.println(str);    

        }   
    }

答案 4 :(得分:1)

我相信atish的答案很好且易于理解。但是,如果您可以考虑使用Java库进行排序,还有另一种选择。

Java 8引入了具有自然内置排序的流API。可以调用sorted - 方法,如下所示,也可以提供自定义Comparator

// Array of planet names (is Pluto technically a planet?)
String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};

// Java 8 Streams are used here to do the sorting and iterations
String[] sortedPlanets = Arrays.stream(planets) // Create the stream
                               .sorted() // Sort the stream
                               .toArray(String[]::new); // Convert back to String[]

System.out.println(Arrays.toString(planets)); // order from the sun
System.out.println(Arrays.toString(sortedPlanets)); order alphabetically

只需几行流式Java 8代码即可实现!