-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickSort.java
50 lines (40 loc) · 1.38 KB
/
quickSort.java
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
43
44
45
46
47
48
49
50
import java.util.*;
public class quicksort {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array:");
int n = sc.nextInt();
int a[] = new int[n];
System.out.print("Enter the array elements:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
QuickSort(a, 0, n - 1);
System.out.println("Sorted Array:");
for (int i : a) {
System.out.print(i + " ");
}
}
private static void QuickSort(int[] a, int lowindex, int highindex) {
if (lowindex >= highindex)
return;
int pivot = a[highindex];
int leftPointer = lowindex;
int rightPointer = highindex;
while (leftPointer < rightPointer) {
while (a[leftPointer] <= pivot && leftPointer < rightPointer)
leftPointer++;
while (a[rightPointer] >= pivot && leftPointer < rightPointer)
rightPointer--;
swap(a, leftPointer, rightPointer);
}
swap(a, leftPointer, highindex);
QuickSort(a, lowindex, leftPointer - 1);
QuickSort(a, leftPointer + 1, highindex);
}
private static void swap(int[] a, int index1, int index2) {
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
}