-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
69 lines (51 loc) · 1.08 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Arrays;
//This program is an implementation of the Quick Sort algorithm
public class QuickSort {
public static void main(String [] args)
{
int[] arr = {9,7,5,11,12,2,14,3,10,6};
System.out.println(Arrays.toString(arr));
int low=0;
int high = arr.length-1;
quicksort(arr, low, high);
System.out.println(Arrays.toString(arr));
}
public static void quicksort(int[] arr, int low, int high)
{
int i = low;
int j = high;
int temp;
//setting pivot point
int midval = arr[(low+high)/2];
//partitioning the array
while(i<=j)
{
while(arr[i]<midval)
{
i++;
}
while(arr[j]>midval)
{
j--;
}
if(i<=j)
{
//swapping ith and jth elements
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
};
//recursively sorting sub arrays
if(low<j)
{
quicksort(arr, low, j);
}
if(i<high)
{
quicksort(arr, i, high);
}
}
}