-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.java
More file actions
54 lines (48 loc) · 1.23 KB
/
Copy pathquickSort.java
File metadata and controls
54 lines (48 loc) · 1.23 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
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.*;
public class quickSort {
static void swap(int[] arr,int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static int partitiion(int[] arr,int l,int h)
{
int start=l;
int end=h,pivot=arr[l];
while(start<end)
{
while(start<arr.length&&pivot>=arr[start]) {
start++;
}
while(pivot<arr[end]) {
end--;
}
if(start<end)
{
swap(arr,start,end);
}
}
swap(arr,l,end);
return end;
}
static void quickSort(int[] arr,int l,int h)
{
if(l<h) {
int pivot = partitiion(arr, l, h);
quickSort(arr, l, pivot - 1);
quickSort(arr, pivot + 1, h);
}
}
public static void main(String[] args)
{
int[] arr={5,4,3,2,1};
quickSort(arr,0,arr.length-1);
for(int i:arr)
{
System.out.println(i);
}
}
}