import java.util.*; public class vectorSelection { public static voidmain(String
ID: 3619382 • Letter: I
Question
import java.util.*;public class vectorSelection
{
public static voidmain(String [] args)
{
Vector<String> b = new Vector<String>();
b.addElement("Arron");
b.addElement("Zed");
b.addElement("James");
b.addElement("Shayne");
b.addElement("Harry");
b.addElement("Colon");
b.addElement("Bea");
b.addElement("Francis");
System.out.println("Vector values before sorting:");
for(int i = 0; i < b.size(); i++)
System.out.print(b.elementAt(i) + " ");
System.out.println();
System.out.println("Vector values after sorting:");
selectionSort(b,b.size());
}
public static voidbubbleSort(Vector<String> b, int length){
boolean swapped = true;
// Setup our outer loop for detecting if a swap was made
while (swapped) {
swapped = false;
for(int i=0; i < length - 1; i++)
{
if(b.elementAt(i).compareTo(b.elementAt(i+ 1)) > 0)
{
String temp = b.elementAt(i);
b.setElementAt(b.elementAt(i+1),i);
b.setElementAt(temp,i+1);
swapped = true;
}
}
}
// After all sorting is finished, just list all the items in thearray.
for(int i = 0; i < b.size(); i++){
System.out.println(b.elementAt(i));
}
}
public static voidselectionSort(Vector<String> b, int length)
{
int index;
int smallestIndex;
int minIndex;
String temp = "";
int compCounter = 0,assignCounter = 0;
for (index = 0; index <length - 1; index++)
{
smallestIndex = index;
for(minIndex = index + 1; minIndex < length; minIndex++){
if(b.elementAt(minIndex).compareTo(b.elementAt(smallestIndex))>0)
smallestIndex = minIndex;
}
temp= b.elementAt(smallestIndex);
b.setElementAt(b.elementAt(smallestIndex), index);
b.setElementAt(temp, index);
}
for(int i = 0; i < b.size(); i++){
System.out.println(b.elementAt(i));
}
}
}
having a problem with the output... i get this output...can some1fix this plsssssss
output:
Vector values before sorting:
Arron Zed James Shayne Harry Colon Bea Francis
Vector values after sorting:
Zed
Zed
Shayne
Shayne
Harry
Francis
Francis
Francis
Explanation / Answer
Your problem is in this part of the selectionSort() function: temp = b.elementAt(smallestIndex); b.setElementAt(b.elementAt(smallestIndex), index); b.setElementAt(temp, index); You are taking an element from one index and inserting it inanother index (twice) rather than taking one element from one indexand swapping it with an element from another index. Basically this is redundant and it is what is causing data loss(you could be losing the data from the index at 'index') This should correct your issue: temp = b.elementAt(smallestIndex); b.setElementAt(b.elementAt(index), smallestIndex); b.setElementAt(temp, index); This will print the names in reverse alphabetical order. Ifyou need them in alphabetical order then all you need to do ischange the if statement in the function from: if(b.elementAt(minIndex).compareTo(b.elementAt(smallestIndex))>0) to if(b.elementAt(minIndex).compareTo(b.elementAt(smallestIndex))Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.