Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-17-2007, 08:11 AM
Member
 
Join Date: Nov 2007
Location: boston
Posts: 10
lowpro is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-17-2007, 08:40 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-17-2007, 11:13 AM
Member
 
Join Date: Nov 2007
Location: boston
Posts: 10
lowpro is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-17-2007, 06:27 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
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
Code:
private int space(int n) { for(int j = 0; j < n; j++) s += " "; return s; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 07:45 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org