Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c5b8ef

Browse files
author
Harry Dulaney
committed
Add Ex 23.03
1 parent 1100c7c commit 3c5b8ef

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

‎ch_23/Exercise23_03.java‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package ch_23;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* 23.3 (Generic quick sort) Write the following two generic methods using quick sort.
8+
* The first method sorts the elements using the Comparable interface and the
9+
* second uses the Comparator interface.
10+
* public static <E extends Comparable<E>>
11+
* void quickSort(E[] list)
12+
* public static <E> void quickSort(E[] list,
13+
* Comparator<? super E> comparator)
14+
*/
15+
public class Exercise23_03 {
16+
17+
public static void main(String[] args) {
18+
Integer[] arr = new Integer[]{23, 25, 7, 14, 85, 65};
19+
quickSort(arr);
20+
System.out.println(Arrays.toString(arr));
21+
22+
Integer[] arr2 = new Integer[]{3, 2, 7, 4, 8, 6, 1, 7};
23+
quickSort(arr2, Integer::compareTo);
24+
System.out.println(Arrays.toString(arr2));
25+
26+
}
27+
28+
// Using Comparable
29+
public static <E extends Comparable<E>> void quickSort(E[] list) {
30+
quickSort(list, 0, list.length - 1);
31+
}
32+
33+
public static <E extends Comparable<E>> void quickSort(E[] list, int first, int last) {
34+
if (last > first) {
35+
int pivotIndex = partition(list, first, last);
36+
quickSort(list, first, pivotIndex - 1);
37+
quickSort(list, pivotIndex + 1, last);
38+
}
39+
}
40+
41+
public static <E extends Comparable<E>> int partition(E[] list, int first, int last) {
42+
E pivot = list[first]; // Choose the first element as the pivot
43+
int low = first + 1; // Index for forward search
44+
int high = last; // Index for backward search
45+
46+
while (high > low) {
47+
// Search forward from left
48+
while (low <= high && list[low].compareTo(pivot) <= 0) low++;
49+
50+
// Search backward from right
51+
while (low <= high && list[high].compareTo(pivot) > 0) high--;
52+
53+
// Swap two elements in the list
54+
if (high > low) {
55+
E temp = list[high];
56+
list[high] = list[low];
57+
list[low] = temp;
58+
}
59+
}
60+
61+
while (high > first && list[high].compareTo(pivot) >= 0)
62+
high--;
63+
64+
// Swap pivot with list[high]
65+
if (pivot.compareTo(list[high]) > 0) {
66+
list[first] = list[high];
67+
list[high] = pivot;
68+
return high;
69+
} else {
70+
return first;
71+
}
72+
}
73+
74+
// Using Comparator
75+
public static <E> void quickSort(E[] list,
76+
Comparator<? super E> comparator) {
77+
quickSort(list, 0, list.length - 1, comparator);
78+
79+
80+
}
81+
82+
// Using Comparator
83+
public static <E> void quickSort(E[] list, int first, int last,
84+
Comparator<? super E> comparator) {
85+
if (last > first) {
86+
int pivotIndex;
87+
// find pivot index
88+
E pivot = list[first]; // Choose the first element as the pivot
89+
int low = first + 1; // Index for forward search
90+
int high = last; // Index for backward search
91+
92+
while (high > low) {
93+
// Search forward from left
94+
while (low <= high && comparator.compare(list[low], pivot) <= 0) low++;
95+
96+
// Search backward from right
97+
while (low <= high && comparator.compare(list[high], pivot) > 0) high--;
98+
99+
// Swap two elements in the list
100+
if (high > low) {
101+
E temp = list[high];
102+
list[high] = list[low];
103+
list[low] = temp;
104+
}
105+
}
106+
107+
while (high > first && comparator.compare(list[high], pivot) >= 0)
108+
high--;
109+
110+
// Swap pivot with list[high]
111+
if (comparator.compare(pivot, list[high]) > 0) {
112+
list[first] = list[high];
113+
list[high] = pivot;
114+
pivotIndex = high;
115+
} else {
116+
pivotIndex = first;
117+
}
118+
119+
quickSort(list, first, pivotIndex - 1, comparator);
120+
quickSort(list, pivotIndex + 1, last, comparator);
121+
}
122+
}
123+
124+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /