VB icon

How to Bubble-Sort a Vector

Email
Submitted on: 1/2/2015 10:18:00 AM
By: Eric Larson (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Java (JDK 1.2)
Views: 2641
 
     I was curious if you could even do this in Java (sort a vector). I guess I proved to myself that you can. Please vote.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: How to Bubble-Sort a Vector
// Description:I was curious if you could even do this in Java (sort a vector). I guess I proved to myself that you can. Please vote.
// By: Eric Larson (from psc cd)
//
// Inputs:Just input any numbers in the dialog box that pops up, then -99 to get the results.
//
// Assumes:Just compile and run!
//**************************************

//How to Bubble-sort a vector in java
import java.lang.*;
import javax.swing.*;
import java.util.*;
public class chapter4
{
 public static void main(String argv[])
 {
String sInput;
final Vector v = new Vector(1);
int x, one1, two2;
Integer iInput;
Integer bInput1;
Integer cInput2;
boolean unsorted = true;
int last, last2; 
sInput = JOptionPane.showInputDialog("enter number or -99");
iInput = new Integer(sInput);
x = Integer.parseInt(sInput);
while ( x != -99)
{
 v.addElement(iInput);
 sInput = JOptionPane.showInputDialog("enter number or -99");
 iInput = new Integer(sInput);
 x = Integer.parseInt(sInput);
}
//sorting starts here
for(int pass = 0; pass < v.size() -1 && unsorted; pass++)
{
 unsorted = false;
 last = v.size()- 1;
 //last--;
 for( int i =0; i < last; i++)
 {
 //bInput1 = v.elementAt(i);// ERROR MESSAGE:
 //Explicit cast needed to convert java.lang.Object to java.lang.Integer
 //must be type cast from Object to Integer like the lines below.
 bInput1 = (Integer)v.elementAt(i);
 cInput2 = (Integer)v.elementAt(i + 1);
 one1 = bInput1.intValue();
 two2 = cInput2.intValue();
 if(one1 < two2)
 {
v.setElementAt(v.elementAt(i + 1), i);
v.setElementAt(bInput1, i + 1);
unsorted = true;
 }
 }
 last--;
}
JOptionPane.showMessageDialog(null, "largest: " + v.elementAt(0) + "\n" +
 "Second: " + v.elementAt(1) + "\n" + "Number: " + v.size()+ "\n" +
 "" + v.toString());
System.exit(0);
 }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.