File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments