Skip to content

Commit a9b1a49

Browse files
Merge pull request kishanrajput23#161 from sourav2210/main
Create Quick Sort
2 parents 9ed68a3 + 9629704 commit a9b1a49

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Quick Sort

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.*;
2+
3+
public class quick_sort {
4+
5+
static void swap(int[] arr, int i, int j)
6+
{
7+
int temp = arr[i];
8+
arr[i] = arr[j];
9+
arr[j] = temp;
10+
}
11+
12+
static int partition(int[] arr, int low, int high)
13+
{
14+
int pivot = arr[high];
15+
16+
17+
int i = (low - 1);
18+
19+
for (int j = low; j <= high - 1; j++) {
20+
21+
if (arr[j] < pivot) {
22+
i++;
23+
swap(arr, i, j);
24+
}
25+
}
26+
swap(arr, i + 1, high);
27+
return (i + 1);
28+
}
29+
30+
static void quickSort(int[] arr, int low, int high)
31+
{
32+
if (low < high) {
33+
34+
int pi = partition(arr, low, high);
35+
36+
quickSort(arr, low, pi - 1);
37+
quickSort(arr, pi + 1, high);
38+
}
39+
}
40+
public static void printArr(int[] arr)
41+
{
42+
for (int i = 0; i < arr.length; i++) {
43+
System.out.print(arr[i] + " ");
44+
}
45+
}
46+
47+
public static void main(String[] args) {
48+
Scanner sc =new Scanner(System.in);
49+
int n=sc.nextInt();
50+
int[] arr=new int[n];
51+
for(int i=0;i<n;i++) {
52+
arr[i]=sc.nextInt();
53+
}
54+
quickSort(arr,0,n-1);
55+
printArr(arr);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)