java - Modifying quicksort to quicksort using to use pivot 'median of three' -


I am trying to modify a quecontal program that uses the first element as a pivot for a quickort In the form of the three middle (middle, first and last element) axis, thus my implementation is yet, although I have to give ArrayIndexOfOfBoundsException (s) while doing this test. I think I am missing something here, but I do not know where I am wrong. Any help and advice is highly appreciated

  Types of public class {Private static zero swap (int [] array, int index1, int index2) {// Precondition: index1 and index2 are & Gt; = 0 and & lt; Shape. // // values ​​index 1 and index swaps the integer at 2 places. Int temp = array [index1]; Array [index1] = array [index 2]; Array [index2] = temp; } Private static int medianOfThree (int [] array, int first, int last {int mid = array [(first + last) / 2]; If (array [first]> array [middle]) {swap (array, first, middle); } If (array [first]> array [last]) {swap (first, last); } If (array [middle]> array [last]) {swap (array, middle, last); } Swap (array, middle, last -1); Return array [last 1]; } Private static int partition (int [] array, int first, int last, int median) {int pivot = array [last-1]; Int saveF = Final -1; Fine side on boolean; First ++; Doing {onCorrectSide = true; While (Onsite Side) {// First move to the first (Array [First]> Pivot) {onCorrectSide = false; } Else {First ++; OnCorrectSide = (previously & lt; = final); }} OnCorrectSide = (previously & lt; = final); While (current sensation) {// move towards the first (array [last] & lt; = pivot) {onCorrectSide = false; } Else {last--; OnCorrectSide = (previously & lt; = final); }} If (first & lt; last) {swap (array, first, last); First ++; Last--; }} While (first & lt; = final); Swap (array, save, last); Previous return; } Private static void quickSort (int [] array, int first, int last {if (last  Smoke Quick sort (array, first, smoke - 1); Quick format (array, smoke + 1, last); }} Public Static void quickSort (int [] array) {quickSort (array, 0, array.length-1); }}  

You are not using the variable correctly:

  int mid = array [first + last / 2];  

Gives you the value in the middle of the array, but not the offset (index) of the array. But you are using the middle as an indicator variable in your call methods:

  swap (array, first, middle);  

Comments