Implementing Quick Sort

To implement quick sort in Java, follow these steps:

  1. Implement the pseudocode shown in Snippet 2.8 in Java, calling the partitioning method shown in Snippet 2.7.
  2. The following code shows the recursive implementation in Java, making use of the partition method developed in the preceding section:
private void sort(int[] numbers, int start, int end) {
if (start < end) {
int p = partition(numbers, start, end);
sort(numbers, start, p - 1);
sort(numbers, p + 1, end);
}
}
Snippet 2.9: Solution for quick sort. Source class name: Quicksort

In this section, we have described the quick sort algorithm, which is much faster than the bubble sort algorithm that we saw in the previous section. On average, this algorithm performs in O(n log n), a huge improvement over bubble sort's O(n2). However, in the worst case, the algorithm still performs in O(n2). The worst-case input of quick sort depends on the type of partitioning scheme in use. In the Lomuto scheme discussed in this section, the worst case occurs when the input is already sorted. In the next section, we will examine another sorting algorithm for which the worst runtime case is O(n log n).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.15