Results 1 to 4 of 4
  1. #1
    lowpro is offline Member
    Join Date
    Nov 2007
    Location
    boston
    Posts
    10
    Rep Power
    0

    Unhappy need help with bubble sort

    // hello everyone my code is not doing the sort please help big thnks adv
    import java.util.Scanner;

    public class C6E19{
    public static void main(String [] args){
    int numberOfStudents;
    Scanner input = new Scanner(System.in);

    System.out.println(" please enter # of student");
    numberOfStudents = input.nextInt();

    String[] studentNames = new String[numberOfStudents];

    int[] studentScores = new int[numberOfStudents];

    for(int i = 0; i < numberOfStudents; ++i)
    {
    System.out.println("enter student name");
    studentNames[i] = input.next();
    System.out.println("enter student score");
    studentScores[i] = input.nextInt();

    } /// print arrray////
    printArray(studentNames, studentScores, numberOfStudents);


    }//end main
    /////call array///
    public static void printArray(String[] namesPassed, int[] scoresPassed,
    int numberOfStudentsPassed)
    {
    for(int i = 0 ; i < numberOfStudentsPassed; ++i){
    System.out.println(namesPassed[i] + " " + scoresPassed[i]);
    }// end for

    } //end method printArray

    /////////sort///////////
    public static void bubbleSortInt(int[] scoresPassed)
    {

    boolean changed = true;
    do{
    changed = false;

    for(int i = 0; i < scoresPassed.length - 1; ++i)
    {
    if(scoresPassed[i] < scoresPassed[i + 1])
    {
    int tempScore = scoresPassed[i];
    scoresPassed[i] = scoresPassed[i + 1];
    scoresPassed[i + 1] = tempScore;
    changed = true;
    }// end if
    }

    }while (changed);

    } // bubblesort

    }//end class

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Scanner;
    
    public class C6E19 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            System.out.println(" please enter # of student");
            int numberOfStudents = input.nextInt();
    
            String[] studentNames = new String[numberOfStudents];
    
            int[] studentScores = new int[numberOfStudents];
    
            for(int i = 0; i < numberOfStudents; ++i) {
                System.out.println("enter student name");
                studentNames[i] = input.next();
                System.out.println("enter student score");
                studentScores[i] = input.nextInt();
            }
    
            printArray(studentNames, studentScores);
            bubbleSortInt(studentScores, studentNames);
            printArray(studentNames, studentScores);
        }
    
        public static void printArray(String[] namesPassed, int[] scoresPassed) {
            int numberOfStudentsPassed = namesPassed.length;
            for(int i = 0; i < numberOfStudentsPassed; ++i) {
                System.out.println(namesPassed[i] + " " + scoresPassed[i]);
            }
        }
    
        public static void bubbleSortInt(int[] scoresPassed, String[] names) {
            boolean changed = true;
            do {
                changed = false;
    
                for(int i = 0; i < scoresPassed.length - 1; ++i) {
                    if(scoresPassed[i] < scoresPassed[i + 1]) {
                        int tempScore = scoresPassed[i];
                        String temp = names[i];
                        scoresPassed[i] = scoresPassed[i + 1];
                        names[i] = names[i + 1];
                        scoresPassed[i + 1] = tempScore;
                        names[i + 1] = temp;
                        changed = true;
                    }
                }
            } while(changed);
        }
    }

  3. #3
    lowpro is offline Member
    Join Date
    Nov 2007
    Location
    boston
    Posts
    10
    Rep Power
    0

    Question

    thnks hardwired. a small question how can i fixe the output if the names enter some are biger than other so it can print the scores in aliment some like this
    peter............60
    marco paulo...80

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Run through the names and get the length of the longest name. When you print each name add the number of
    spaces = longestNameLength - thisNameLength + whateverExtra;
    after the name and before printing the score. You can make up a method to return the spaces
    Java Code:
    private int space(int n) {
        for(int j = 0; j < n; j++)
            s += " ";
        return s;
    }

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  2. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:42 PM
  3. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 02:11 AM
  4. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 07:56 AM
  5. how to sort 2 tables
    By valery in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •