Java - 数组中的冒泡排序对象

时间:2014-05-09 13:07:58

标签: java arrays sorting object

已解决 帖子末尾的固定代码以及进展

如果我的问题不清楚或者我遗漏了什么,我会提前道歉。我是编程和java的新手,所以我可能不知道如何恰当地说出我的问题。我要设置你应该知道的东西,以便帮助我解决问题,然后问问题。如果我能让事情变得更清楚,我会继续编辑这篇文章。

我有一个数组,arrStudents,(7)学生对象:(firstName,lastName,grade)(String,String,int)

getGrade()返回成绩

我想创建一个冒泡排序数组并打印的方法,例如,这是排序前的输出:

John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65

这是排序输出

Sue Taylor 55
George Bush 58
John Miller 65
Ann Miller 75
Al Clark 80
John Smith 90
Barack Obama 95

我有一些代码可供使用,但它并没有让我在任何地方。以下给出了冒泡排序的例子:

import java.util.Scanner;

class array {

public static void main (String[] args) 
{

Scanner scan = new Scanner(System.in);

int [] a = new int [100];
int i, n = 0, min, max;
float  total = 0;

System.out.println ("Enter integers separated by blanks (<Enter> <Ctrl-Z> to end):");

while (scan.hasNext()) {
   a[n] = scan.nextInt();
   n = n + 1;
}

min = a[0];
max = a[0];
for (i = 0; i < n; i++) {
  if (max < a[i]) max = a[i];
  if (min > a[i]) min = a[i];
  total = total + a[i];
}

System.out.print ("You entered " + n + " numbers: ");
System.out.print ("Min = " + min + ", Max = " + max + ", Average = " + total/n);

int t, swap = 0;
do { 
    swap = 0; 
    for (i=0; i<n-1; i++) 
       if (a[i]>a[i+1]) {
           t=a[i]; 
           a[i]=a[i+1]; 
           a[i+1]=t; 
           swap++;
       }
} while (swap>0);

System.out.print ("\nSorted: ");
for (i=0; i<n; i++) System.out.print (a[i] + " ");
System.out.println ();

 }
}

我无法使用&gt;对象数组运算符,所以我尝试使用 arrStudents [i] .getGrade(),但我不确定这是否正确,我无法得到它给我正确的输出。

以下是我一直在玩的代码:

public static void bubbleSort(Student [] arrStudents) {
int t, swap = 0;

do {
    swap = 0;
    for (i=0; i<arrStudents.length-1; i++){
    int grade = arrStudents[i].getGrade();
    int gradePlus = arrStudents[i+1].getGrade();
       if (grade>grade+1) {
           t=grade;
           grade=gradePlus;
           gradePlus=t;
           swap++;              
       }
    }
} while (swap>0); 
}

编辑:修改后的代码(仍需要修复)

public static void bubbleSort(Student [] arrStudents) {
int swap = 0;

do {
    swap = 0;
    for (i=0; i<arrStudents.length-1; i++){
    int grade = arrStudents[i].getGrade();
    int gradePlus = arrStudents[i+1].getGrade();
       if (grade>gradePlus) {
        Student tmp = arrStudents[i];
        arrStudents[i] = arrStudents[i+1];
        arrStudents[i+1]=tmp;
        swap++;
        System.out.println(tmp);
       }          
    }
} while (swap>0);
}

修改后的代码输出(降序很好):

Barack Obama 95
Barack Obama 95
Barack Obama 95
Barack Obama 95
Barack Obama 95
John Smith 90
John Smith 90
John Smith 90
John Smith 90
John Smith 90
Al Clark 80
Al Clark 80
Al Clark 80
Al Clark 80
Ann Miller 75
Ann Miller 75 

固定代码(带升序输出) - 我上次尝试打印时只是愚蠢

public static void bubbleSort(Student [] arrStudents) {
int swap = 0;    
do {
    swap = 0;
    for (i=0; i<arrStudents.length-1; i++){
      Student tmp = arrStudents[i];  
      int grade = arrStudents[i].getGrade();
      int gradePlus = arrStudents[i+1].getGrade();
       if (grade>gradePlus) {
        arrStudents[i] = arrStudents[i+1];
        arrStudents[i+1]=tmp;
        swap++;
       }          
    }
} while (swap>0);    
System.out.print ("\nSorted: ");
for (i=0; i<arrStudents.length; i++) 
System.out.print ("\n" + arrStudents[i]);
}

输出

Sorted:
Sue Taylor 55
George Bush 58
John Miller 65
Ann Miller 75
Al Clark 80
John Smith 90
Barack Obama 95

在这一次被遗忘了一段时间,所以任何帮助都会非常感激!

tl; dr帮我修复最近的气泡排序代码

编辑:还要注意可能有更好的排序方法,但对于这个程序,我需要使用冒泡排序。

2 个答案:

答案 0 :(得分:1)

int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
   if (grade>grade+1) {

在这段代码中,你说等级大于等级+ 1,这永远不会是真的,你想写下面我认为

int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
   if (grade>gradePlus ) {

答案 1 :(得分:1)

此类中的实际排序发生在swap部分。如果您排序的数组必须移动以进行排序以执行任何操作,则可以使用这些元素。

您必须交换arrStudents[i]arrStudents[i+1],因为它是您要排序的arrStudents

Student tmp = arrStudents[i];
arrStudents[i] = arrStudents[i+1];
arrStudents[i + 1] = tmp;

然后(也由@maczikasz指出),你测试条件是错误的。使用:

if (grade > gradePlus) {
    // Do the swap as above, increment the swap counter
}