Java Sorting (Insertion, Merge, Quick)

Associate
Joined
28 May 2008
Posts
346
ok so for my uni assignment i had to create a bi**ke class (done) then make it compa**rable (done) and then create So**rt methods to sort the arr**ay of compar**ables (not so good) this is the code i have so far but alas the file does not compile and I am in 2 minds if i have even done the thing right :(, any opinions from the experts would be greatly appreciated ;) and do i even need the Partationing code?

(sorry for the random ***s but i dont want classmates to google my work)

Code so Far

public class BikeMySort {

public static int numberComps = 0;

private static long startTime;
private static long timeTaken;

//insertion start//

public static <T extends Comparable<? super T>> void insertionSort (T[] number)
{
startTime = System.currentTimeMillis();

for (intindex = 1; index < numbers.length; index++)
{
T temp = numbers[index];
int position = index;

//shift larger values to right

while (position > 0 && numbers[position-1].compareTo(temp) > 0)
{

numbers[position] - numbers[position-1];
position --;

numberComps ++;
}
numbers[position] = temp;

system.out.println("count" + "");
}

timetaken = System.currentTimeMillis() - startTime;
}
public static <T extends Comparable<? super T>> void mergeSort(T[] numbers, int min, int max)
{
T[] temp;
int index1, left, right;
int comparisons = 0;

//return length list 1//
if (mind==max){
return;
}
//find lenth + midpoint of Z list//

int size = max - min + 1;
int pivot = (mind + max) / 2;

temp = (T[])(new Comparable[size]);

//sort left half of list
mergeSort (numbers, min, pivot);

//sort right half
mergeSort(numbers, pivot +1, max);

//copied data written to workspace

for (index1 = 0; index1 < size; index1++) {
temp[index = numbers[min + index1];
numberComps ++;
}
//sorted lists merged//

left = 0;
right = pivot - min + 1;
for (index1 = 0; index1 < size; index1++)
{
if (right <= max - min)
if (left <= pivot - min)
if (temp
.compareTo(temp
) >0)
numbers[index1 + min] = temp[right ++1];

else numbers[index1 + min] = temp[left++];

else numbers [index1 + min = temp[right++];

else numbers[index1 + min] = temp[left++1];

numberComps++;

}
timeTaken = System.currentTimeMillis() - startTime;

}
//quicksort//
public static <t extends Comparable<? super T>> int quickSort (T[] numbers, int min, int max)
{
int indexofpartition;
int count = 0;
if (max - min >0)
{
numberComps++;
//CREATE PARTITION
indexofpartition = findPartition(numbers, min , max);
//sort left
quickSort(numbers,min, indexofpartition - 1);
//sort right
quickSort(numbers,indexofpartition + 1, max);
count++;
}
return count;
}
private static <T extends Comparable<? super T>> int findPartition (T[] numbers, int min, int max)
{
startTime = System.currentTimeMillis();

int left, right;
T temp, partitionelement;

//use first element as partition element
partitionelement = numbers [min];

left = min;
right = max;

while (left<right)
{
//serch for element that is right of pARTITION ELEMENT//

while (numbers
.compareTo(partitionelement) <=0 && left < right)
left++;
numberComps++;

//serch for element to left

while (numbers
.compareTo(partationelement) > 0)

numberComps++;

//swap elemennts//

if (left<right)
{
temp = numbers
;
numbers
= numbers
;
numbers
= temp;

}


}

}
//move partition element to partition index
temp = data[min];
data[min] = data
;
data
= temp;

numberComps++;

timeTaken = System.currentTimeMillis() - startTime;




return right;

}
//end//

the next plan of attack is to make the driver program to create the details of the bikes in Z array (done) and call these sort methods but i cant do this untill i get this code somewhat correct :(
 
Last edited:
First thing that jumps out is the generics stuff (i.e <T extends Comparable>).

First thing - it's in the wrong place in the method signature (typically <T extends Comparable> type stuff is defined at class level then used at method level) and tbh it doesn't really have any place something like this. Instead, change the methods so that it takes all Objects which implement the interface Comparable. So:

PHP:
public static <t extends Comparable<? super T>> int quickSort (T[] numbers, int min, int max)

becomes

PHP:
public static int quickSort (Comparable[] numbers, int min, int max)

then replace 'T' with 'Comparable' through out your code.

With regards to the code and the algorithms, my knowledge of sorting is a bit rusty (oops!) but looks vaguely correct but very complicated. But I do see a few Java syntax errors. For example:

PHP:
numbers[position] - numbers[position-1];

isn't correct because you aren't assigning the result of the calculation. Pretty sure that'll throw a wobbler at compile time.

Best advice I can give is to go through the compiler output. Stack traces are scary at times but they do give you all the info you need. I assume you're using a text editor + javac? Get it compiling first by investigating the stack traces and then start debugging the semantics using a driver class and many many println statements :D
 
Back
Top Bottom