-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab2.java
More file actions
55 lines (51 loc) · 1.52 KB
/
lab2.java
File metadata and controls
55 lines (51 loc) · 1.52 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
51
52
53
54
55
// Quick Sort using User Input
import java.util.Arrays;
//import java.util.Scanner;
public class lab2 {
int partition(int a[], int lb, int ub) {
int pivot = a[lb];
int start = lb + 1;
int end = ub;
while (start <= end) {
while (start <= ub && a[start] <= pivot) {
start++;
}
while (a[end] > pivot) {
end--;
}
if (start < end) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
int temp = a[lb];
a[lb] = a[end];
a[end] = temp;
return end;
}
void quickSort(int a[], int lb, int ub) {
if (lb < ub) {
int loc = partition(a, lb, ub);
quickSort(a, lb, loc - 1);
quickSort(a, loc + 1, ub);
}
}
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// System.out.print("Enter number of elements: ");
// int n = sc.nextInt();
// int a[] = new int[n];
// System.out.println("Enter elements:");
// for (int i = 0; i < n; i++) {
// a[i] = sc.nextInt();
// }
int a[] = {34, 7, 23, 32, 5, 62};
int n = a.length;
System.out.println("Before Sorting: " + Arrays.toString(a));
lab2 obj = new lab2();
obj.quickSort(a, 0, n - 1);
System.out.println("After Sorting: " + Arrays.toString(a));
//sc.close();
}
}