Java中带ArrayList的基本冒泡排序

Java中带ArrayList的基本冒泡排序,java,sorting,arraylist,Java,Sorting,Arraylist,我正在实现一个比较器,但它不起作用,所以我想我应该编写一个基本的冒泡排序 int[] numbers = { 5, 8, 14, 1, 5678 }; int tempVar; for (int i = 0; i < numbers.length; i++) { for(int j = 0; j < numbers.length; j++) { if(numbers[i] > numbers[j + 1]) {

我正在实现一个比较器,但它不起作用,所以我想我应该编写一个基本的冒泡排序

int[] numbers = { 5, 8, 14, 1, 5678 };
int tempVar;
for (int i = 0; i < numbers.length; i++)
{
   for(int j = 0; j < numbers.length; j++)
   {
            if(numbers[i] > numbers[j + 1])
            {
                   tempVar = numbers [j + 1];
                   numbers [j + 1]= numbers [i];
                   numbers [i] = tempVar;
            }
   }
}
for (int i = 0; i < numbers.length; i++)
{
     System.out.println(numbers[i].toString());
}
int[]number={5,8,14,15678};
int-tempVar;
for(int i=0;i数字[j+1])
{
tempVar=数字[j+1];
数字[j+1]=数字[i];
数字[i]=tempVar;
}
}
}
for(int i=0;i
这个教程正确吗?

我遵循这个示例,将其应用于arraylist中的姓氏,但结果有点奇怪

String a;
String b;
Person c;
Person d;

for (int i=0; i< list.size(); i++){

    for(int j=0; j< list.size()-1; j++){

         a = list.get(i).getLastName();
         b = list.get(j+1).getLastName();
         c = list.get(i);
         d = list.get(j+1);

         if ( a.compareTo(b) < 0 )  {

             Person temp = d;
             list.set(j+1, c);        
             list.set(i, temp); 
         }
     }
 }
字符串a;
b串;
c人;
d人;
对于(int i=0;i

我真的很想掌握一些方法(比如找出我的比较器为什么不工作),但现在我只想让气泡排序正确工作。谢谢。

这是一个奇怪且低效的实现,您需要比较每个数字。这样做更直观(可以在性能方面有所改进,但这不是重点,您只需节省大量时间,而不会意外地在索引中出错。如果您真正关心性能而不是可读性,请使用mergesort或quicksort,就像Java一样[Java对基本类型使用快速排序,对对象使用合并排序,可能是因为对于基本类型,算法是否稳定并不重要]:

public void bubbleSort(int[]arr){
布尔变换;
做{
改变=错误;
对于(int i=0;iarr[i+1]){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=温度;
改变=正确;
}
}
}而(变化);
}
应用于您的代码(按升序排序):

布尔变化;
做{
改变=错误;
对于(int i=0;i0){
列表集(i,d);
列表集(i+1,c);
改变=正确;
}
}
}而(变化);
旁注:
s.toUpperCase()。如果
s
仅包含符号,则compareTo(s.toLowerCase())==0
将为
true

如果您编写

for(int j = 0; j < numbers.length; j++)
因为数组
numbers
具有长度
5
和最后一个索引
4
(索引从
0
开始)。因此,当
j=4
时,循环中断条件
j
4<5
true
,但访问
numbers[4+1]/code>索引时会出现异常

所以试试看

for(int j = 0; j < numbers.length -1; j++)
输出是一个
列表
,包含:

列表=[Wilker、Kruden、Ezen、Crocket、Allen]


在冒泡排序中,您只需要比较相邻的元素并交换它们(取决于条件)

如果按升序排列,则比较相邻元素并交换
If(arr[j]>arr[j+1])
。 这将在第一次迭代中将最大的元素移动到末尾。因此,在外循环中有
n-1
迭代来排序数组,其中n是数组的长度

首先阅读这篇文章,因为你提到的教程是完全错误的

修正码

for (int i = 0; i < numbers.length-1; i++)
{
   for(int j = 0; j < numbers.length-i-1; j++)
   {
            if(numbers[j] > numbers[j + 1])
            {
                   tempVar = numbers [j + 1];
                   numbers [j + 1]= numbers [j];
                   numbers [j] = tempVar;
            }
   }
}
for(int i=0;i数字[j+1])
{
tempVar=数字[j+1];
数字[j+1]=数字[j];
数字[j]=tempVar;
}
}
}

这是

您最初使用的排序程序有一个小问题

int j=0
应该是

int j=i
另外,您并没有完全将其替换为字符串排序

a.compareTo(b) < 0
选中此项:

import java.util.*;

public class HelloWorld{
public static void main(String[] args){

   ArrayList<Person> list = new ArrayList<Person>();
   list.add(new Person("xyz"));
   list.add(new Person("abc"));
   list.add(new Person("pqr"));
   list.add(new Person("lmn"));

   String a;
   String b;
   Person c;
   Person d;
   for (int i=0; i< list.size(); i++){
      for(int j=i; j< list.size()-1; j++){
         a = list.get(i).getLastName();
         b = list.get(j+1).getLastName();
         c = list.get(i);
         d = list.get(j+1);

         if ( a.compareTo(b) > 0 )  {

             Person temp = d;
             list.set(j+1, c);        
             list.set(i, temp); 
        }
     }
  }
  for(Person person: list){
      System.out.println(person.lastName);
  }
}
}
class Person{

   String lastName;
   Person(String str){
      lastName = str;
   }
   public String getLastName(){
      return lastName;
   }
}
import java.util.*;
公共类HelloWorld{
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
添加(新人员(“xyz”);
列表。添加(新人员(“abc”);
列表。添加(新人员(“pqr”);
列表。添加(新人员(“lmn”);
字符串a;
b串;
c人;
d人;
对于(int i=0;i0){
人员温度=d;
列表集(j+1,c);
列表设置(i,温度);
}
}
}
用于(个人:列表){
System.out.println(person.lastName);
}
}
}
班主任{
字符串lastName;
人(字符串str){
lastName=str;
}
公共字符串getLastName(){
返回姓氏;
}
}

感谢大家为我指明了正确的方向。 一个问题是我忘记了.trim(),因此compareTo不起作用,也没有与charAt(0)进行比较

此外,我还发现了一个更好的冒泡排序循环实现

这就是现在的效果:

    String a;
    String b;
    Person c;
    Person d;

    for (int i= 0; i< list.size() ; i++){

        for(int j=0; j< list.size() - i-1; j++){

             a = list.get(j).getLastName().toUpperCase().trim();
             b = list.get(j+1).getLastName().toUpperCase().trim();

             c = list.get(j);
             d = list.get(j+1);

             if ( a.compareTo(b) > 0)  {

                 Person temp = d;  
                 list.set(j+1, c);
                 list.set(j, temp);

             } 
        }
字符串a;
b串;
c人;
d人;
对于(int i=0;iint j=0
int j=i
a.compareTo(b) < 0
a.compareTo(b) > 0
import java.util.*;

public class HelloWorld{
public static void main(String[] args){

   ArrayList<Person> list = new ArrayList<Person>();
   list.add(new Person("xyz"));
   list.add(new Person("abc"));
   list.add(new Person("pqr"));
   list.add(new Person("lmn"));

   String a;
   String b;
   Person c;
   Person d;
   for (int i=0; i< list.size(); i++){
      for(int j=i; j< list.size()-1; j++){
         a = list.get(i).getLastName();
         b = list.get(j+1).getLastName();
         c = list.get(i);
         d = list.get(j+1);

         if ( a.compareTo(b) > 0 )  {

             Person temp = d;
             list.set(j+1, c);        
             list.set(i, temp); 
        }
     }
  }
  for(Person person: list){
      System.out.println(person.lastName);
  }
}
}
class Person{

   String lastName;
   Person(String str){
      lastName = str;
   }
   public String getLastName(){
      return lastName;
   }
}
    String a;
    String b;
    Person c;
    Person d;

    for (int i= 0; i< list.size() ; i++){

        for(int j=0; j< list.size() - i-1; j++){

             a = list.get(j).getLastName().toUpperCase().trim();
             b = list.get(j+1).getLastName().toUpperCase().trim();

             c = list.get(j);
             d = list.get(j+1);

             if ( a.compareTo(b) > 0)  {

                 Person temp = d;  
                 list.set(j+1, c);
                 list.set(j, temp);

             } 
        }
Bubble Sort Swap Printer in JAVA:
static void countSwaps(int[] a) {
    int swaps = 0;
    for(int i=0; i<a.length-1; i++){
        for(int j=0; j<a.length-i-1; j++){
            if (a[j] > a[j+1]){
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
                swaps++;
            }
        }
    }
    System.out.println("Array is sorted in " + swaps +" swaps.");
}