-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTXT.txt
97 lines (88 loc) · 2.56 KB
/
TXT.txt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package qsort;
/**
*
* @author student_user
*/
public class QSort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int i;
int arr[] = {5, 36, 5, 7, 68, 45, 4, 58};
quickSort(arr, 0, arr.length - 1);
System.out.println("Sorted Arr : ");
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ", ");
}
}
static void quickSort(int a[], int beg, int end) {
int loc;
if (beg < end) {
loc = partition(a, beg, end);
quickSort(a, beg, loc - 1);
quickSort(a, loc + 1, end);
}
}
public static int partition(int a[], int l, int r) {
int pivot = l;
int too_big = l + 1;
int too_small = r;
while (too_small > too_big) {
while (a[too_big] <= a[pivot]) {
too_big++;
}
while (a[too_small] > a[pivot]) {
too_small--;
}
if (too_big < too_small) {
int temp = a[too_big];
a[too_big] = a[too_small];
a[too_small] = temp;
}
}
int temp = a[too_small];
a[too_small] = a[pivot];
a[pivot] = temp;
pivot = too_small;
return pivot;
}
// public static int partition(int a[], int beg, int end) {
// int left, right, temp, loc, flag;
// loc = left = beg;
// right = end;
// flag = 0;
// while (flag != 1) {
// while ((a[loc] <= a[right]) && (loc != right)) {
// right--;
// }
// if (loc == right) {
// flag = 1;
// } else if (a[loc] > a[right]) {
// temp = a[loc];
// a[loc] = a[right];
// a[right]=temp;
// loc = right;
// }
//
// if (flag != 1) {
// while ((a[loc] >= a[left]) && (loc != left)) {
// left++;
// }
// if (loc == left) {
// flag = 1;
// } else if (a[loc] < a[left]) {
// temp = a[loc];
// a[loc] = a[left];
// a[left]=temp;
// loc = left;
// }
// }
// }
// return loc;
// }
}