-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.java
59 lines (49 loc) · 1.55 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
public class Quicksort
{
// Оргаанизовать фактический вызов метода быстрой сортировки
static void qsort (char items[])
{
qs (items, 0, items.length - 1);
}
// рекурскивная версия быстрой сортировки символов
private static void qs (char items[], int left, int right)
{
int i, j;
char x, y;
i = left;
j = right;
x = items[(left + right) / 2];
do
{
while ((items[i] < x) && (i < right)) i++;
while ((x < items[j]) && (j > left)) j--;
if (i <= j)
{
y = items[i];
items[i] = items[j];
items[j] = y;
i++; j--;
}
}
while (i <= j);
if (left < j) qs(items, left, j);
if (i < right) qs(items, i, right);
}
}
class QSDemo
{
public static void main (String args[])
{
char a[] = {'d', 'x', 'a', 'r', 'p', 'j', 'i'};
int i;
System.out.print("Исходный массив: ");
for (i = 0; i < a.length; i++)
System.out.print(a[i]);
System.out.println();
// Отсортировать массив
Quicksort.qsort(a);
System.out.print("Отсортированный массив: ");
for (i = 0; i < a.length; i++)
System.out.print(a[i]);
}
}