-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
64 lines (54 loc) · 1.19 KB
/
Copy pathQuickSort.java
File metadata and controls
64 lines (54 loc) · 1.19 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
56
57
58
59
60
61
62
63
64
// The "QuickSort" class.
import java.awt.*;
import hsa.Console;
public class QuickSort
{
static Console c; // The output console
static int list[] = {1,2,3,4,5};
static int quickcounter = 0;
public static void printStuff ()
{
for (int i = 0 ; i < list.length ; i++)
System.out.print (list [i]);
System.out.println ();
}
public static void swap (int i, int j)
{
int temp = list [i];
list [i] = list [j];
list [j] = temp;
}
public static void quickSort (int left, int right)
{
quickcounter++;
int pivotPlace;
int mid = (left + right) / 2;
swap (left, mid);
int lastSmall = left;
for (int i = left + 1 ; i <= right ; i++)
{
printStuff ();
if (list [i] < list [left])
{
lastSmall++;
swap (lastSmall, i);
}
}
swap (left, lastSmall);
pivotPlace = lastSmall;
if (left < pivotPlace - 1)
quickSort (left, pivotPlace - 1);
if (pivotPlace + 1 < right)
quickSort (pivotPlace + 1, right);
}
public static void main (String[] args)
{
printStuff();
{
}
;
quickSort (0, list.length-1);
printStuff ();
// Place your program here. 'c' is the output console
} // main method
} // QuickSort class