Movatterモバイル変換


[0]ホーム

URL:


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

Commit0cda939

Browse files
author
jsquared21
committed
Add Ex 23.4
1 parent8729ae8 commit0cda939

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*********************************************************************************
2+
* (Improve quick sort) The quick sort algorithm presented in the book selects *
3+
* the first element in the list as the pivot. Revise it by selecting the median *
4+
* among the first, middle, and last elements in the list. *
5+
*********************************************************************************/
6+
public class Exercise_23_04 {
7+
public static void quickSort(int[] list) {
8+
quickSort(list, 0, list.length - 1);
9+
}
10+
11+
public static void quickSort(int[] list, int first, int last) {
12+
if (last > first) {
13+
int pivotIndex = partition(list, first, last);
14+
quickSort(list, first, pivotIndex - 1);
15+
quickSort(list, pivotIndex + 1, last);
16+
}
17+
}
18+
19+
/** Partition the aray list[first..last] */
20+
public static int partition(int[] list, int first, int last) {
21+
int pivot = list[first]; // choose the first element as the pivot
22+
int low = first + 1; // Index for forward search
23+
int high = last; // Index for backward search
24+
25+
while (high > low) {
26+
// Search forward from left
27+
while (low <= high && list[low] <= pivot)
28+
low++;
29+
30+
// Search backward from right
31+
while (low <= high && list[high] > pivot)
32+
high--;
33+
34+
// Swap two elements in the list
35+
if (high > low) {
36+
int temp = list[high];
37+
list[high] = list[low];
38+
list[low] = temp;
39+
}
40+
}
41+
42+
while (high > first && list[high] >= pivot)
43+
high--;
44+
45+
// Swap pivot with list[high]
46+
if (pivot > list[high]) {
47+
list[first] = list[high];
48+
list[high] = pivot;
49+
return high;
50+
}
51+
else {
52+
return first;
53+
}
54+
}
55+
56+
/** A test method */
57+
public static void main(String[] args) {
58+
int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
59+
quickSort(list);
60+
for (int i = 0; i < list.length; i++)
61+
System.out.print(list[i] + " ");
62+
}
63+
}
1.43 KB
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*********************************************************************************
2+
* (Improve quick sort) The quick sort algorithm presented in the book selects *
3+
* the first element in the list as the pivot. Revise it by selecting the median *
4+
* among the first, middle, and last elements in the list. *
5+
*********************************************************************************/
6+
publicclassExercise_23_04 {
7+
publicstaticvoidquickSort(int[]list) {
8+
quickSort(list,0,list.length -1);
9+
}
10+
11+
publicstaticvoidquickSort(int[]list,intfirst,intlast) {
12+
if (last >first) {
13+
intpivotIndex =partition(list,first,last);
14+
quickSort(list,first,pivotIndex -1);
15+
quickSort(list,pivotIndex +1,last);
16+
}
17+
}
18+
19+
/** Partition the aray list[first..last] */
20+
publicstaticintpartition(int[]list,intfirst,intlast) {
21+
intmiddle =list[(list.length -1) /2];
22+
// choose the median element as the pivot
23+
intpivot =median(first,middle,last);
24+
intlow =first +1;// Index for forward search
25+
inthigh =last;// Index for backward search
26+
27+
while (high >low) {
28+
// Search forward from left
29+
while (low <=high &&list[low] <=pivot)
30+
low++;
31+
32+
// Search backward from right
33+
while (low <=high &&list[high] >pivot)
34+
high--;
35+
36+
// Swap two elements in the list
37+
if (high >low) {
38+
inttemp =list[high];
39+
list[high] =list[low];
40+
list[low] =temp;
41+
}
42+
}
43+
44+
while (high >first &&list[high] >=pivot)
45+
high--;
46+
47+
// Swap pivot with list[high]
48+
if (pivot >list[high]) {
49+
list[first] =list[high];
50+
list[high] =pivot;
51+
returnhigh;
52+
}
53+
else {
54+
returnfirst;
55+
}
56+
}
57+
58+
/** A test method */
59+
publicstaticvoidmain(String[]args) {
60+
int[]list = {2,3,2,5,6,1, -2,3,14,12};
61+
quickSort(list);
62+
for (inti =0;i <list.length;i++)
63+
System.out.print(list[i] +" ");
64+
System.out.println();
65+
}
66+
67+
/** Returns the median of three integers */
68+
publicstaticintmedian(intfirst,intmiddle,intlast) {
69+
returnMath.max(Math.min(first,middle),
70+
Math.min(Math.max(first,middle),last));
71+
}
72+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp