forked from AdityaSubrahmanyaBhat/hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.java
More file actions
46 lines (44 loc) · 1013 Bytes
/
Copy pathquickSort.java
File metadata and controls
46 lines (44 loc) · 1013 Bytes
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
package com.company;
import java.util.Arrays;
public class quickSort {
public static void main(String[] args) {
int[] arr={3,2,1,4,5,7,12,15,13,14,6,9,8,11,10};
sort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
static void sort(int[] arr,int low,int hi)
{
if(low>=hi)
{
return;
}
int s=low,e=hi;
int m=s+(e-s)/2;
int pivot=arr[m];
while(s<=e)
{
while(arr[s]<pivot)
{
s++;
}
while(arr[e]>pivot)
{
e--;
}
if(s<=e)
{
swap(arr,s,e);
s++;
e--;
}
sort(arr,low,e);
sort(arr,s,hi);
}
}
static void swap(int[] arr,int f1,int f2)
{
int t=arr[f1];
arr[f1]=arr[f2];
arr[f2]=t;
}
}