English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Also, could you send me a pseudo code to explain algorithm:

public class QuickSorter //method call to do the sort
{
public static void quickSort(int[ ] array){
quickSort(array, 0, array.length);
}

private static void quickSort(int[ ] array, int first, int last) { // main method
if (first < last) {
int pivot = pivotList(array, first, last); //
quickSort(array, first, pivot - 1);
quickSort(array, pivot + 1, last);
}
}
//* @param a an array of Comparable items.
private static int pivotList(int[ ] array, int first, int last) { //take in the list
int pivotValue = array[first]; // assign first element of array
int pivotPoint = first; //
for (int index = first + 1; index < last; index++) { // recursive

2007-12-13 04:34:11 · 1 answers · asked by sprite 3 in Computers & Internet Programming & Design

if (array[index] < pivotValue) {
pivotPoint = pivotPoint + 1;
int temp = array[pivotPoint];// assign array temp
array[pivotPoint] = array[index];
array[index] = temp;
}
}
int temp = array[first]; // sort by rearange
array[first] = array[pivotPoint];
array[pivotPoint] = temp;
return pivotPoint;
}
}

2007-12-13 04:34:29 · update #1

1 answers

Quicksort takes the first item in the list and treats it as a partition - that is, it moves that item (and others) until the partition splits the list into items that are less than or equal to the partition and items that are greater than the partition. This element need never be inspected again, and quicksort is invoked on the two partitions.

To get a feel for how it works, write down a list of a dozen or so random integers and step through the algorithm while applying it to your list. I actually required my students to do this with several sorting algorithms on their final exams this morning (e.g. show the state of this list after two passes by quicksort).

2007-12-13 04:40:45 · answer #1 · answered by jgoulden 7 · 0 0

fedest.com, questions and answers