mardi 24 mars 2015

Quicksort recursively



Hy I'm trying to implement quicksort sorting method recursively. The only parameter is the array.



public void quicksort(int [] myArray) {
int left = myArray[0];
int right = myArray[myArray.length - 1];
int pivot = myArray[myArray.length - 1];
for (int element = 0; element < myArray.length - 1; element++) {

while (myArray[left] > pivot) {
left--;
this.swapElements(right, left, myArray);
}

while (myArray[right] > pivot) {
right++;
this.swapElements(right, left, myArray);
}



}


}

}


Here is the helper method swapElements



swapElements(int firstIndex, int secondIndex, int [] theArray) {
int temporary = theArray[firstIndex];
theArray[firstIndex] = theArray[secondIndex];
theArray[secondIndex] = temporary;

}
}


As you can guess the current output is not sorting the array in decreasing order correctly. Not sure how to resolve the issue. Any help would be appreciated. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire