-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquicksort.java
56 lines (46 loc) · 1.17 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
//https://www.youtube.com/watch?v=-7pzsM6gxgY&t=2s
public class quicksort {
public static int partition(int[] a,int start,int end)
{
int i=start,pi= a[end];
int current= start;
while(current<end)
{
if(a[current]>pi)
{
current++;
}
else
{
int temp= a[current];
a[current]=a[i];
a[i]=temp;
i++;
current++;
}
}
int temp= pi;
a[current]=a[i];
a[i]=temp;
return i;
}
public static void quickSort(int[] a, int left,int right)
{
if(left<right)
{
int pi=partition(a,left,right);
quickSort(a,left,pi-1);
quickSort(a,pi+1,right);
}
}
//driver program
public static void main(String[] args) {
int arr[]={12,0,3,17,8,23,4};
quickSort(arr,0,arr.length-1);
System.out.println("sorted array is :-");
for(int element: arr){
System.out.print(element+" ");
}
}
}
// youtube link for this program understanding :- https://www.youtube.com/watch?v=-7pzsM6gxgY