按字母顺序从数组中插入排序字符串
Insert sorting strings from an array in alphabetical order
我真的不是 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
java.util.Collections.sort(list);
这会将列表按字母顺序排列
编辑:Atish 有更完整的答案
String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};
使用java.util.Collections
方法
对列表list
进行排序
java.util.Collections.sort(list);
sort()
api 对数组进行排序 Natural Ordering (sort alphabetically)
.
我刚刚清理了您的代码,使其可以正常工作 - 给您:
我注意到的几件事 -- 当你初始化你的行星时 -- 确保你初始化它们,这样它们就不全是空的,例如 String Earth = "Earth",而不仅仅是 String Earth。当您在排序中比较字符串时 - 确保使用 .compareTo() 方法。您不能使用 < 或 > 来比较 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
试试下面的代码:
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);
}
}
我相信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 个代码即可!
我真的不是 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
java.util.Collections.sort(list);
这会将列表按字母顺序排列
编辑:Atish 有更完整的答案
String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};
使用java.util.Collections
方法
list
进行排序
java.util.Collections.sort(list);
sort()
api 对数组进行排序 Natural Ordering (sort alphabetically)
.
我刚刚清理了您的代码,使其可以正常工作 - 给您:
我注意到的几件事 -- 当你初始化你的行星时 -- 确保你初始化它们,这样它们就不全是空的,例如 String Earth = "Earth",而不仅仅是 String Earth。当您在排序中比较字符串时 - 确保使用 .compareTo() 方法。您不能使用 < 或 > 来比较 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
试试下面的代码:
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);
}
}
我相信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 个代码即可!