简体   繁体   English

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

[英]Java - Bubble Sort Objects in Array

SOLVED Fixed code at the end of post along with progression 已解决 在帖子结尾处的固定代码以及进展

I apologize in advance if my question seems unclear or if I leave anything out. 如果我的问题不清楚,或者我遗漏了什么,我会提前道歉。 I'm new to programming and java, so I may not know how to phrase my questions appropriately yet. 我是编程和java的新手,所以我可能不知道如何恰当地说出我的问题。 I'm going to set up what you should know in order to help me with my question, and then ask it. 我将设置您应该知道的内容,以便帮助我解决问题,然后再问一下。 I'll continue editing this post if I can make things more clear. 如果我能让事情变得更清楚,我会继续编辑这篇文章。

I have an array, arrStudents, of (7) Student objects: (firstName, lastName, grade) (String, String, int) 我有一个数组,arrStudents,(7)学生对象:(firstName,lastName,grade)(String,String,int)

getGrade() returns the grade getGrade()返回成绩

I want to create a method that bubble sorts the array and prints , so for example, this is the output prior to sort: 我想创建一个bubble对数组进行排序和打印的方法,例如,这是sort之前的输出:

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

and here is the sorted output 这是排序的输出

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

I have some code to work from, but it hasn't gotten me anywhere. 我有一些代码可供使用,但它没有让我到任何地方。 Here's the example I was given with bubble sort: 这是我给出冒泡排序的例子:

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 ();

 }
}

I'm unable to use the array of objects with the > operator, so II tried using arrStudents[i].getGrade() , but I'm not sure if that was correct, I couldn't get it to give me the correct output. 我无法使用>运算符使用对象数组,所以尝试使用arrStudents [i] .getGrade() ,但我不确定这是否正确,我无法让它给我正确的输出。

Here's the code I've been playing with: 这是我一直在玩的代码:

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); 
}

Edit: Revised Code (Still needs fixing) 编辑:修改代码(仍需要修复)

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);
}

Output of revised code (Descending order is fine): 修改后的代码输出(降序很好):

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 

Fixed Code (with Ascending Output) - I was just stupid with the print from my previous attempt 固定代码(具有升序输出) - 我之前尝试的打印件只是愚蠢的

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]);
}

Output 产量

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

Been stumped for awhile on this one, so any help would be much appreciated! 在这一次被遗忘了一段时间,所以任何帮助将不胜感激!

tl;dr Help me fix the closest above bubble sort code tl; dr帮我修复最近的气泡排序代码

Edit: Also aware that there may be better ways to sort, but for this program, I need to use bubble sort. 编辑:还意识到可能有更好的排序方法,但对于这个程序,我需要使用冒泡排序。

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

In this code you're saying that grade is greater than grade+1 which will never be true, you wanted to write the following I think 在这段代码中,你说等级大于等级+ 1,这永远不会是真的,你想写下面我认为

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

The actual sorting in this sort happens in the swap part. 这种实际排序发生在swap部分。 The elements if the array you sort have to be moved for the sort to do anything. 如果您排序的数组必须移动以进行排序以执行任何操作。

You have to swap arrStudents[i] and arrStudents[i+1] , since it is the arrStudents that you are sorting: 您必须交换arrStudents[i]arrStudents[i+1] ,因为它是您正在排序的arrStudents

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

Then (also pointed out by @maczikasz), you test condition is wrong. 然后(也由@maczikasz指出),你测试条件是错误的。 Use: 采用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM