For reference, I am programming Java in BlueJ .
I am new to the language and am having problems sorting.
I am trying to call / check all 5 sorting methods in the main class.
I figured out how to call / check Quicksort:
Sorting.quickSort(friends, 0, friends.length-1);
But others are not working properly. In particular, they:
Sorting.mergeSort(friends, 0, friends.length-1); Sorting.PbubbleSort(friends, 0, friends.length-1); Sorting.PinsertionSort(friends, 0, friends.length-1); Sorting.selectionSort(friends, 0, friends.length-1);
Hope someone can help me.
For reference, this is the result when it is not sorted:
Smith, John 610-555-7384 Barnes, Sarah 215-555-3827 Riley, Mark 733-555-2969 Getz, Laura 663-555-3984 Smith, Larry 464-555-3489 Phelps, Frank 322-555-2284 Grant, Marsha 243-555-2837
This is the result when sorting :
Barnes, Sarah 215-555-3827 Getz, Laura 663-555-3984 Grant, Marsha 243-555-2837 Phelps, Frank 322-555-2284 Riley, Mark 733-555-2969 Smith, John 610-555-7384 Smith, Larry 464-555-3489
This is the Sorting class, which I should note, everything is correct:
public class Sorting{ private static <T extends Comparable<? super T>> void swap(T[] data, int index1, int index2){ T temp = data[index1]; data[index1] = data[index2]; data[index2] = temp; } public static <T extends Comparable<? super T>> void quickSort(T[] data){ quickSort(data, 0, data.length - 1); } public static <T extends Comparable<? super T>> void quickSort(T[] data, int min, int max){ if (min < max){
Main class, SortPhoneList :
public class SortPhoneList{ public static void main (String[] args){ Contact[] friends = new Contact[7]; friends[0] = new Contact ("John", "Smith", "610-555-7384"); friends[1] = new Contact ("Sarah", "Barnes", "215-555-3827"); friends[2] = new Contact ("Mark", "Riley", "733-555-2969"); friends[3] = new Contact ("Laura", "Getz", "663-555-3984"); friends[4] = new Contact ("Larry", "Smith", "464-555-3489"); friends[5] = new Contact ("Frank", "Phelps", "322-555-2284"); friends[6] = new Contact ("Marsha", "Grant", "243-555-2837"); Sorting.quickSort(friends, 0, friends.length-1); Sorting.mergeSort(friends, 0, friends.length-1); Sorting.PbubbleSort(friends, 0, friends.length-1); Sorting.PinsertionSort(friends, 0, friends.length-1); Sorting.selectionSort(friends, 0, friends.length-1); for (int index = 0; index < friends.length; index++) System.out.println (friends[index]); }