-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.java
More file actions
50 lines (49 loc) · 1.35 KB
/
quicksort.java
File metadata and controls
50 lines (49 loc) · 1.35 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
public class quicksort
{
public static int Partition(int[] arr, int low , int high)
{
int pivot = arr[low];
int i= low;
int j= high;
System.out.println(low);
System.out.println(high);
while(i<j)
{
while(arr[i]<= pivot && i<high)
{
i++;
}
while( arr[j]> pivot && j>= low+1)
{
j--;
}
if (i < j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
arr[low] = arr[j];
arr[j] = pivot;
return j;
}
public static void QuickSort(int[] arr , int low, int high)
{
if(low >= high)
{
return;
}
int PIndex =Partition(arr,low,high);
QuickSort(arr, low, PIndex-1);
QuickSort(arr, PIndex+1 ,high);
}
public static void main(String args[])
{
int arr[]={5,9,3,1,2};
QuickSort(arr,0,arr.length-1);
for(int i=0;i< arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
}