473,809 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bubbleSort for Competitor[] array attributes?

47 New Member
Ok I think I have added the bubbleSort methods correctly I am not getting any errors and the program runs as if I had not added them yet! My question is this! how would I now in the main method implement the shortBubble() so that it could sort the array?

Here is my code!

Expand|Select|Wrap|Line Numbers
  1. import java.lang.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import ch10.*;
  5.  
  6. public class ContestManagement {
  7.  
  8.     //define local variables
  9.     static final int MAX_LENGTH = 20;
  10.     static Competitor competitorArray[] = new Competitor[MAX_LENGTH];
  11.     static int arraySize = 0;
  12.  
  13.     //define method swap for bubbleSort
  14.     static public void swap(int index1, int index2) {
  15.       //precondition: index 1 and index 2 are >= 0 and < MAX_LENGTH
  16.       //swaps the integers at locations index1 and index2 of the competitorsArray
  17.       Competitor temp = competitorArray[index1];
  18.       competitorArray[index1] = competitorArray[index2];
  19.       competitorArray[index2] = temp;
  20.     }
  21.  
  22.     //define method bubbleUp2 for bubbleSort
  23.     static boolean bubbleUp2(int startIndex, int endIndex) {
  24.       //switches adjacent pairs that are out of order
  25.       //between values[startIndex]...values[endIndex]
  26.       //beginning at values[endIndex]
  27.       //returns false if a swap was made: otherwise, returns true.
  28.       boolean sorted = true;
  29.       for (int index = endIndex; index > startIndex; --index) 
  30.             if (competitorArray[index].compareTo(competitorArray[index - 1]) < 0) {
  31.                   swap(index, index - 1);
  32.                   sorted = false;
  33.             }
  34.       return sorted;            
  35.     }
  36.  
  37.     //define method shortBubble for bubbleSort
  38.     static void shortBubble() {
  39.       //sorts the values array using the bubble sort algorithm
  40.       //the process stops as soon as values is sorted
  41.       int current = 0;
  42.       boolean sorted = false;
  43.       while ((current < (MAX_LENGTH - 1)) && !sorted) {
  44.             sorted = bubbleUp2(current, MAX_LENGTH - 1);
  45.             ++current;
  46.       }
  47.     }
  48.  
  49.     //define main method
  50.     public static void main (String [] args)  throws IOException{
  51.  
  52.  
  53.       Scanner stdin = new Scanner(System.in);
  54.  
  55.       //set up file stream
  56.         Scanner fileIn = null;
  57.  
  58.       //read and process the file Competitors.txt
  59.       try {
  60.            my code is here...
  61.           }
  62.       }
  63.       catch (InputMismatchException e) {
  64.             System.out.println("uh oh, got an exception error!");
  65.             }
  66.  
  67.       //read and process the file Marks.txt
  68.       //when same ID is found mark is reset to new value
  69.       //display unsorted list of competitors
  70.       try {
  71.             my code is here
  72.             }
  73.             for (int i = 0; i < arraySize; ++i) {
  74.                   System.out.println(competitorArray[i]);
  75.             }
  76.       }
  77.       catch (NullPointerException e) {
  78.             System.out.println("uh oh, got an exception error!");
  79.             }      
  80.     }
  81. }   
Jul 30 '07 #1
2 1993
r035198x
13,262 MVP
That method is in a class called ContestManageme nt. To call it in main ,you may create an instance of the ContestManageme nt class.
Jul 30 '07 #2
JosAH
11,448 Recognized Expert MVP
Judging from your code a Competitor already implements the Comparable
interface so why not use the Arrays.sort() method to sort that array? A bubble
sort algorithm is so newbee Pascalian or Fortranesque ;-)

kind regards,

Jos
Jul 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
2357
by: J. J. Cale | last post by:
I have the following. There must be something less cumbersome without push pop. The function parameter obj is the event.srcElement and has the attributes(properties?) picNum and alb pinned to it. The function is part of a process to delete the selected obj, a photo in this case from the album vis albumArr. TIA Jimbo function Album(albumName) { this.name=albumName; this.paths=new Array(); }
1
2181
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not work. It works if the tags are <input type=radio...>. This line is OK:
10
1553
by: Thomas Mlynarczyk | last post by:
Hi, For objects (non-arrays) I can use for(i in object) to loop through all its members. For arrays, this does not always seem to work, and I am forced to use for(i=0; i<array.length;i++) instead. Is there a way to make an array accessible using the for-in loop? During my experiments I found that if the array is a member of an object, but not the first one, the for-in will work... seems a strange behaviour to me, so I'd be grateful if...
1
2191
by: Michal Piatkowski | last post by:
I have a problem with serialization of a polymorphic array. It has attributes defined XmlArray and XmlArrayItem for XML serialization. The attribute specifies Type1 but as my array is polymorphic I really want to have more than one type inside of it. So I have objects of Type2, Type3, etc. which inherit from Type1. They are in my array. To be able to serialize the array, I would need to supply all the types (Type2, Type3, etc...) as...
1
29785
by: Jim Michaels | last post by:
=> Array ( => UML:CLASS => open => 5 => Array ( => .:00000000000008EC => quiz_batteries => public
0
1967
by: magic9baller | last post by:
I have to use bubblesort to sort a singly linked list. (Yes I am a n00b.) The tricky part is that I have to move pointers rather than moving the contents of one node to the other. I think my problem is incrementing the pointers after a swap. When I didn’t have the code below “THIS IS WRONG,” the function infinitely copied one node into the next. Now that I added that code, I get a segmentation fault. Please help!! void bubbleSort() { ..int...
6
3006
by: dawnerd | last post by:
Hello everyone. I have a question, or problem if you will, that I'm sure someone knows the answer to. I have a database that stores information on a given user. The information is stored in a serialized array. This works flawlessly when using only single line text. When I tried to store multiline text, the problem arose. When the serialized data is deserialized, the array breaks. Any suggestions?
16
2405
by: NoviceJava | last post by:
hello, I am having trouble with a program. It seems easy, but for some reason I am just not getting it. I need to create an array of random numbers and call a bubblesort method that I create myself. I then need to print the sorted array. I created the random array but I cant seem to figure out the sort method, which needs to follow in descending order. This is what I have: public static int sorting(int vals) { int i,j; int...
3
3636
by: natvarara | last post by:
#include <stdio.h> #include <string.h> #include <conio.h> #include <ctype.h> #define MAX 30 void getData (FILE*,int,char,int,int,float,float,int,int*); void computeTotal(int,int,float,float,int ,int,float,float); int maxScore(float,int);
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.