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! - import java.lang.*;
-
import java.util.*;
-
import java.io.*;
-
import ch10.*;
-
-
public class ContestManagement {
-
-
//define local variables
-
static final int MAX_LENGTH = 20;
-
static Competitor competitorArray[] = new Competitor[MAX_LENGTH];
-
static int arraySize = 0;
-
-
//define method swap for bubbleSort
-
static public void swap(int index1, int index2) {
-
//precondition: index 1 and index 2 are >= 0 and < MAX_LENGTH
-
//swaps the integers at locations index1 and index2 of the competitorsArray
-
Competitor temp = competitorArray[index1];
-
competitorArray[index1] = competitorArray[index2];
-
competitorArray[index2] = temp;
-
}
-
-
//define method bubbleUp2 for bubbleSort
-
static boolean bubbleUp2(int startIndex, int endIndex) {
-
//switches adjacent pairs that are out of order
-
//between values[startIndex]...values[endIndex]
-
//beginning at values[endIndex]
-
//returns false if a swap was made: otherwise, returns true.
-
boolean sorted = true;
-
for (int index = endIndex; index > startIndex; --index)
-
if (competitorArray[index].compareTo(competitorArray[index - 1]) < 0) {
-
swap(index, index - 1);
-
sorted = false;
-
}
-
return sorted;
-
}
-
-
//define method shortBubble for bubbleSort
-
static void shortBubble() {
-
//sorts the values array using the bubble sort algorithm
-
//the process stops as soon as values is sorted
-
int current = 0;
-
boolean sorted = false;
-
while ((current < (MAX_LENGTH - 1)) && !sorted) {
-
sorted = bubbleUp2(current, MAX_LENGTH - 1);
-
++current;
-
}
-
}
-
-
//define main method
-
public static void main (String [] args) throws IOException{
-
-
-
Scanner stdin = new Scanner(System.in);
-
-
//set up file stream
-
Scanner fileIn = null;
-
-
//read and process the file Competitors.txt
-
try {
-
my code is here...
-
}
-
}
-
catch (InputMismatchException e) {
-
System.out.println("uh oh, got an exception error!");
-
}
-
-
//read and process the file Marks.txt
-
//when same ID is found mark is reset to new value
-
//display unsorted list of competitors
-
try {
-
my code is here
-
}
-
for (int i = 0; i < arraySize; ++i) {
-
System.out.println(competitorArray[i]);
-
}
-
}
-
catch (NullPointerException e) {
-
System.out.println("uh oh, got an exception error!");
-
}
-
}
-
}
2 1993
That method is in a class called ContestManageme nt. To call it in main ,you may create an instance of the ContestManageme nt class.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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();
}
|
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:
|
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...
|
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...
|
by: Jim Michaels |
last post by:
=> Array
(
=> UML:CLASS
=> open
=> 5
=> Array
(
=> .:00000000000008EC
=> quiz_batteries
=> public
| |
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...
|
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?
|
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...
|
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);
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |