Skip to content

Add chapter 24 answers #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions ch_23/Exercise23_04.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ch_23;

import java.util.Arrays;

/**
* 23.4 (Improve quick sort) The quick sort algorithm presented in the book selects the
* first element in the list as the pivot. Revise it by selecting the median among the
* first, middle, and last elements in the list.
*/
public class Exercise23_04 {
public static void quickSort(int[] list) {
/* Exercise 23.4 */
int medianIdx = indexOfMedian(list);
quickSort(list, medianIdx, list.length - 1);
}

public static void quickSort(int[] list, int first, int last) {
if (last > first) {
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
}

/**
* Partition the array list[first..last]
*/
public static int partition(int[] list, int first, int last) {
int pivot = list[first]; // Choose the first element as the pivot
int low = first + 1; // Index for forward search
int high = last; // Index for backward search

while (high > low) {
// Search forward from left
while (low <= high && list[low] <= pivot)
low++;

// Search backward from right
while (low <= high && list[high] > pivot)
high--;

// Swap two elements in the list
if (high > low) {
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}

while (high > first && list[high] >= pivot)
high--;

// Swap pivot with list[high]
if (pivot > list[high]) {
list[first] = list[high];
list[high] = pivot;
return high;
} else {
return first;
}
}

/**
*
* @param list int array to find median of first, middle and last nums
* @return the index of the median value
*/
static int indexOfMedian(int[] list) {
int[] temp = {list[0], list[list.length / 2], list[list.length - 1]};
Arrays.sort(temp);
if (list[0] == temp[1]) {
return 0;
}
else if (list[list.length / 2] == temp[1]) {
return list.length / 2;
}

return list.length - 1;
}

/**
* A test method
*/
public static void main(String[] args) {
int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
quickSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}