gpt4 book ai didi

java - 在java中使用冒泡排序对字符串进行排序

转载 作者:行者123 更新时间:2023-12-04 06:03:13 24 4
gpt4 key购买 nike

我尝试使用冒泡排序对字符串进行排序,但我不知道这是否有意义,但是有人可以帮我弄清楚如何对字符串进行排序并让我知道我的逻辑是否正确吗?我已将字符串转换为字符只是为了检查它们是否按字母顺序排列。

import java.io.*;
import java.util.*;
class sort
{
public static void main(String args[])throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("enter the strings");
String str=br.readLine();
StringTokenizer st=new StringTokenizer(str,",");
String s1=st.nextToken();
String s2=st.nextToken();
String s3=st.nextToken();
char ch1[]=s1.toCharArray();
char ch2[]=s2.toCharArray();
char ch3[]=s3.toCharArray();

if ((ch1[0]<ch2[0])&&(ch1[0]<ch3[0])&&(ch2[0]<ch3[0]))
for(int i=0;i<4;i++)
{
System.out.println(+ch1[i]);
System.out.println(+ch2[i]);
System.out.println(+ch3[i]);

}
else if((ch2[0]<ch1[0]&&ch2[0]<ch3[0]&&ch1[0]<ch3[0]) )
for(int i=0;i<4;i++)
{
System.out.println(+ch2[i]);
System.out.println(+ch1[i]);
System.out.println(+ch3[i]);
}
else if((ch3[0]<ch1[0])&&(ch3[0]<ch2[0])&&ch1[0]<ch2[0])
for(int i=0;i<4;i++)
{
System.out.println(+ch3[i]);
System.out.println(+ch1[i]);
System.out.println(+ch2[i]);
}
}
}

最佳答案

Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better. Wikipedia



以下是执行此操作的排序方式。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

final public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the strings:->");
String str=br.readLine();

String strArr[]=str.split(" ");//your sentence will be split into words.
Arrays.sort(strArr);

for(int i=0;i<strArr.length;i++)
{
System.out.println(strArr[i]);
}
}
}

如果您愿意,您可以按如下方式应用您自己的逻辑。
final public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the strings:->");
String str=br.readLine();

String strArr[]=str.split(" ");

String temp;

for(int i = 0; i<strArr.length - 1; i++)
{
for(int j = 0; j<strArr.length - 1; j++)
{
if(strArr[j].compareTo(strArr[j+1]) > 0)
{
temp = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = temp;
}
}
}

for(int i=0;i<strArr.length;i++)
{
System.out.println(strArr[i]);
}
}
}

在这两种情况下,我都假设 空格 作为单词分隔符而不是 逗号 ,你在你的例子中使用的。

关于java - 在java中使用冒泡排序对字符串进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8688413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com