Skip to content

Commit ea22559

Browse files
dionisPriftimaibin
authored andcommitted
BAEL-2209 : Quicksort Dionis Prifti (eugenp#5365)
* Merged changes from the original repository. * Added event streaming example with WebFlux. * Deleted auto-generated code. * Deleted auto-generated code. * BAEL-2209 : Added java class and JUnit test for QuickSort implementation. * Revert "Added event streaming example with WebFlux." This reverts commit 21527b3. * BAEL-2209: Removed main method from Quicksort class. * BAEL-2209 : Added the implementation and unit test for 3-Way Quicksort.
1 parent 59a5313 commit ea22559

4 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.algorithms.quicksort;
2+
3+
public class QuickSort {
4+
5+
public static void quickSort(int arr[], int begin, int end)
6+
{
7+
if (begin < end) {
8+
int partitionIndex = partition(arr, begin, end);
9+
10+
// Recursively sort elements of the 2 sub-arrays
11+
quickSort(arr, begin, partitionIndex-1);
12+
quickSort(arr, partitionIndex+1, end);
13+
}
14+
}
15+
16+
private static int partition(int arr[], int begin, int end)
17+
{
18+
int pivot = arr[end];
19+
int i = (begin-1);
20+
21+
for (int j=begin; j<end; j++)
22+
{
23+
if (arr[j] <= pivot) {
24+
i++;
25+
26+
int swapTemp = arr[i];
27+
arr[i] = arr[j];
28+
arr[j] = swapTemp;
29+
}
30+
}
31+
32+
int swapTemp = arr[i+1];
33+
arr[i+1] = arr[end];
34+
arr[end] = swapTemp;
35+
36+
return i+1;
37+
}
38+
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.algorithms.quicksort;
2+
3+
public class ThreeWayQuickSort {
4+
5+
public static void threeWayQuickSort(int[] a, int begin, int end)
6+
{
7+
if (end <= begin) return;
8+
9+
// partition
10+
int i = begin;
11+
int less = begin;
12+
int greater = end;
13+
14+
while (i <= greater){
15+
if (a[i] < a[less]) {
16+
int tmp = a[i];
17+
a[i] = a[less];
18+
a[less] = tmp;
19+
20+
i++;
21+
less++;
22+
}
23+
else if (a[less] < a[i]) {
24+
int tmp = a[i];
25+
a[i] = a[greater];
26+
a[greater] = tmp;
27+
28+
greater--;
29+
}
30+
else {
31+
i++;
32+
}
33+
}
34+
35+
threeWayQuickSort(a, begin, less - 1);
36+
threeWayQuickSort(a, greater + 1, end);
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.algorithms.quicksort;
2+
3+
import com.baeldung.algorithms.quicksort.QuickSort;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class QuickSortUnitTest {
8+
9+
@Test
10+
public void givenIntegerArray_whenSortedWithQuickSort_thenGetSortedArray() {
11+
int[] actual = { 9, 5, 1, 0, 6, 2, 3, 4, 7, 8 };
12+
int[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
13+
QuickSort.quickSort(actual, 0, actual.length-1);
14+
Assert.assertArrayEquals(expected, actual);
15+
}
16+
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.algorithms.quicksort;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ThreeWayQuickSortUnitTest {
7+
8+
@Test
9+
public void givenIntegerArray_whenSortedWithThreeWayQuickSort_thenGetSortedArray() {
10+
int[] actual = { 3, 5, 5, 5, 3, 7, 7, 3, 5, 5, 7, 3, 3 };
11+
int[] expected = { 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7 };
12+
ThreeWayQuickSort.threeWayQuickSort(actual, 0, actual.length-1);
13+
Assert.assertArrayEquals(expected, actual);
14+
}
15+
}

0 commit comments

Comments
 (0)