Why wont my generic quick sort and binary search work? String searchName; String
ID: 649207 • Letter: W
Question
Why wont my generic quick sort and binary search work?
String searchName;
String data;
String input;
int count=0;
Scanner kb = new Scanner(System.in);
nameSearch ns = new nameSearch();
name[] lastNames=new name[151671];
File file = new File("list.txt");
Scanner list = new Scanner(file);
while(list.hasNextLine()) {
data = list.nextLine();
StringTokenizer tokens = new StringTokenizer(data, ",");
String name=tokens.nextToken();
int rank=Integer.parseInt(tokens.nextToken());
int data2=Integer.parseInt(tokens.nextToken());
double data3=Double.parseDouble(tokens.nextToken());
double data4=Double.parseDouble(tokens.nextToken());
//set Person class objects in Person array
lastNames[count]=new name(name,rank,data2,data3,data4);
//increment the count value by one
count++;
}
ns.quickSort(lastNames);
do {
System.out.println("Enter a name: ");
searchName = kb.nextLine();
int result = nameSearch.binarySearch(lastNames, searchName);
if(result == -1)
System.out.println("Name was not found");
else
System.out.println(searchName + "was found at the position " +
(result+1));
System.out.println("Search again?");
input=kb.nextLine();
} while (input.charAt(0)=='y'|| input.charAt(0) == 'Y');
}
public class nameSearch> {
public int search(Comparable[] array,T value){
int first;
int last;
int middle;
int position;
boolean found;
first = 0;
last = array.length - 1;
position = -1;
found = false;
while (!found && first<=last) {
middle = (first + last) / 2;
if(array[middle].compareTo(value) == 0){
found = true;
position = middle;
}
else if(array[middle].compareTo(value) > 0) {
last = middle - 1;
}
else
first = middle + 1;
}
return position;
}
public void quickSort(T array[]) {
doQuickSort(array,0,array.length -1);
}
private void doQuickSort(T array[],int start, int end) {
int pivot;
if(start pivot =partition(array,start,end);
doQuickSort(array,start,pivot-1);
doQuickSort(array,pivot +1,end);
}
}
private int partition(T[] array,int start,int end){
T pivotValue;
int endOfLeftList;
int mid;
mid =(start+end)/2;
swap(array,start,mid);
pivotValue=array[start];
endOfLeftList = start;
for(int scan = start+1; scan<= end;scan++) {
if(array[scan].compareTo(pivotValue) <0){
endOfLeftList++;
swap(array,endOfLeftList,scan);
}
}
swap(array,start,endOfLeftList);
return endOfLeftList;
}
private void swap(T[] array, int a, int b) {
T temp;
temp = array[a];
array[a] = array[b];
array[b]=temp;
}
Explanation / Answer
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; public class Program { public static void bubbleSort1(ref int[] x) { bool exchanges; do { exchanges = false; for (int i = 0; i x[i + 1]) { // Exchange elements int temp = x[i]; x[i] = x[i + 1]; x[i + 1] = temp; exchanges = true; } } } while (exchanges); } public static void DisplayElements(ref int[] xArray, char status, string sortname) { if (status == 'a') Console.WriteLine("After sorting using algorithm: " + sortname); else Console.WriteLine("Before sorting"); for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.