-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
68 lines (54 loc) · 1.48 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
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Scanner;
public class QuickSort
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++)
{
System.out.print("Enter element: ");
arr[i] = sc.nextInt();
}
QuickSort sort = new QuickSort();
sort.quickSort(arr, 0, arr.length - 1);
System.out.print("The Sorted Array: [ ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.print("]");
sc.close();
}
public void quickSort(int arr[], int left, int right)
{
if (left < right)
{
int pivot = partition(arr, left, right);
quickSort(arr, left, pivot - 1);
quickSort(arr, pivot + 1, right);
}
}
public static int partition(int arr[], int left, int right)
{
int pivot = arr[right];
int i = left - 1;
int temp;
for (int j = left; j < right; j++)
{
if (arr[j] <= pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[right];
arr[right] = temp;
return i + 1;
}
}