-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_7 (Quicksort)
More file actions
42 lines (38 loc) · 1.04 KB
/
Chapter_7 (Quicksort)
File metadata and controls
42 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package intro_to_algorithms;
// The purpose of this algorithm is to sort an unordered Array
public class Quicksort {
// Quicksort recursively calls itself to sort the sub-arrays input[p..q-1] and input[q+1..r]
public static void Quicksort(int[] input,int p, int r) {
if(p<r) {
int q=Partition(input,p,r);
Quicksort(input,p,q-1);
Quicksort(input,q+1,r);
}
}
// Partition rearranges the subarrays in place. An element acts as a "pivot" around which the
// array is split
public static int Partition(int[] input, int p, int r) {
int x=input[r];
int i=p-1;
for(int j=p;j<=r-1;j++) {
if(input[j]<=x) {
i++;
int temp=input[i];
input[i]=input[j];
input[j]=temp;
}
int temp=input[i+1];
input[i+1]=input[r];
input[r]=temp;
}
return i;
}
public static void main(String[] args) {
int[] array= {2,4,5,3,6,7,4,8,9,1,0,9,8,0};
Quicksort(array,0,array.length-1);
for(int i=0;i<array.length;i++) {
System.out.println(array[i]);
}
}
}
// I found that writing quicksort was much easier than heapsort.