Skip to content

Commit 54b874c

Browse files
author
Jubayer Alam
authored
Add files via upload
1 parent b7d80fe commit 54b874c

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

bin/Quicksort.class

995 Bytes
Binary file not shown.

bin/QuicksortTest.class

3.07 KB
Binary file not shown.

src/Quicksort.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public class Quicksort {
2+
private int[] numbers;
3+
private int number;
4+
public void sort(int[] values) { //S1
5+
// check for empty or null array
6+
if (values ==null || values.length==0){ //C1
7+
return; //S2
8+
}
9+
this.numbers = values; //S3
10+
number = values.length; //S4
11+
quicksort(0, number - 1); //S5
12+
}
13+
private void quicksort(int low, int high) {
14+
int i = low, j = high; //S6
15+
// get the pivot element from the middle of the list
16+
int pivot = numbers[low + (high-low)/2]; //S7
17+
// divide into two lists
18+
while (i <= j) { //C2
19+
// if the current value from the left list is smaller than the pivot
20+
// element then get the next element from the left list
21+
while (numbers[i] < pivot) { //C3
22+
i++; //S8
23+
}
24+
// if the current value from the right list is larger than the pivot
25+
// element then get the next element from the right list
26+
while (numbers[j] > pivot) { //C4
27+
j--; //S9
28+
}
29+
30+
// if we have found a value in the left list which is larger than
31+
// the pivot element and if we have found a value in the right list
32+
// which is smaller than the pivot element then we exchange the
33+
// values.
34+
// as we are done we can increase i and j
35+
if (i <= j) { //C5
36+
exchange(i, j); //S10
37+
i++; //S11
38+
j--; //S12
39+
}
40+
}
41+
// recursion method
42+
if (low < j) //C6
43+
quicksort(low, j); //S13
44+
if (i < high) //C7
45+
quicksort(i, high); //S14
46+
}
47+
private void exchange(int i, int j) {
48+
int temp = numbers[i];
49+
numbers[i] = numbers[j];
50+
numbers[j] = temp;
51+
}
52+
}

src/QuicksortTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.fail;
7+
public class QuicksortTest {
8+
private int[] numbers;
9+
private final static int SIZE = 7;
10+
private final static int MAX = 15;
11+
@Before
12+
public void setUp() throws Exception {
13+
numbers = new int[SIZE];
14+
Random generator = new Random();
15+
for (int i = 0; i < numbers.length; i++) {
16+
numbers[i] = generator.nextInt(MAX);
17+
}
18+
}
19+
@Test
20+
public void testNull() {
21+
Quicksort sorter = new Quicksort();
22+
sorter.sort(null);
23+
}
24+
@Test
25+
public void testEmpty() {
26+
Quicksort sorter = new Quicksort();
27+
sorter.sort(new int[0]);
28+
}
29+
@Test
30+
public void testSimpleElement() {
31+
Quicksort sorter = new Quicksort();
32+
int[] test = new int[1];
33+
test[0] = 7;
34+
sorter.sort(test);
35+
}
36+
@Test
37+
public void testSpecial() {
38+
Quicksort sorter = new Quicksort();
39+
int[] test = { 8,9,13,14,1,3,6,12,10,2,11,4,5,7,15 };
40+
sorter.sort(test);
41+
if (!validate(test)) {
42+
fail("Should not happen");
43+
}
44+
printResult(test);
45+
}
46+
@Test
47+
public void testQuickSort() {
48+
for (Integer i : numbers) {
49+
System.out.println(i + " ");
50+
}
51+
long startTime = System.currentTimeMillis();
52+
Quicksort sorter = new Quicksort();
53+
sorter.sort(numbers);
54+
long stopTime = System.currentTimeMillis();
55+
long elapsedTime = stopTime - startTime;
56+
System.out.println("Quicksort " + elapsedTime);
57+
if (!validate(numbers)) {
58+
fail("Should not happen");
59+
}
60+
assertTrue(true);
61+
}
62+
@Test
63+
public void testStandardSort() {
64+
long startTime = System.currentTimeMillis();
65+
Arrays.sort(numbers);
66+
long stopTime = System.currentTimeMillis();
67+
long elapsedTime = stopTime - startTime;
68+
System.out.println("Standard Java sort " + elapsedTime);
69+
if (!validate(numbers)) {
70+
fail("Should not happen");
71+
}
72+
assertTrue(true);
73+
}
74+
private boolean validate(int[] numbers) {
75+
for (int i = 0; i < numbers.length - 1; i++) {
76+
if (numbers[i] > numbers[i + 1]) {
77+
return false;
78+
}
79+
}
80+
return true;
81+
}
82+
private void printResult(int[] numbers) {
83+
for (int i = 0; i < numbers.length; i++) {
84+
System.out.print(numbers[i]);
85+
}
86+
System.out.println();
87+
}
88+
}

0 commit comments

Comments
 (0)