BAEL-3484: Partitioning and Sorting an Array With Many Repeated Entries (#8369)
This commit is contained in:
parent
ac4d7757b7
commit
e5b4048419
|
@ -0,0 +1,4 @@
|
|||
/target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
|
@ -0,0 +1,64 @@
|
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>algorithms-sorting-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-sorting-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter-api.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import static com.baeldung.algorithms.quicksort.SortingUtils.swap;
|
||||
|
||||
public class BentleyMcIlroyPartioning {
|
||||
|
||||
public static Partition partition(int input[], int begin, int end) {
|
||||
int left = begin, right = end;
|
||||
int leftEqualKeysCount = 0, rightEqualKeysCount = 0;
|
||||
|
||||
int partitioningValue = input[end];
|
||||
|
||||
while (true) {
|
||||
while (input[left] < partitioningValue)
|
||||
left++;
|
||||
|
||||
while (input[right] > partitioningValue) {
|
||||
if (right == begin)
|
||||
break;
|
||||
right--;
|
||||
}
|
||||
|
||||
if (left == right && input[left] == partitioningValue) {
|
||||
swap(input, begin + leftEqualKeysCount, left);
|
||||
leftEqualKeysCount++;
|
||||
left++;
|
||||
}
|
||||
|
||||
if (left >= right) {
|
||||
break;
|
||||
}
|
||||
|
||||
swap(input, left, right);
|
||||
|
||||
if (input[left] == partitioningValue) {
|
||||
swap(input, begin + leftEqualKeysCount, left);
|
||||
leftEqualKeysCount++;
|
||||
}
|
||||
|
||||
if (input[right] == partitioningValue) {
|
||||
swap(input, right, end - rightEqualKeysCount);
|
||||
rightEqualKeysCount++;
|
||||
}
|
||||
left++; right--;
|
||||
}
|
||||
right = left - 1;
|
||||
for (int k = begin; k < begin + leftEqualKeysCount; k++, right--) {
|
||||
if (right >= begin + leftEqualKeysCount)
|
||||
swap(input, k, right);
|
||||
}
|
||||
for (int k = end; k > end - rightEqualKeysCount; k--, left++) {
|
||||
if (left <= end - rightEqualKeysCount)
|
||||
swap(input, left, k);
|
||||
}
|
||||
return new Partition(right + 1, left - 1);
|
||||
}
|
||||
|
||||
public static void quicksort(int input[], int begin, int end) {
|
||||
if (end <= begin)
|
||||
return;
|
||||
Partition middlePartition = partition(input, begin, end);
|
||||
quicksort(input, begin, middlePartition.getLeft() - 1);
|
||||
quicksort(input, middlePartition.getRight() + 1, end);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import static com.baeldung.algorithms.quicksort.SortingUtils.compare;
|
||||
import static com.baeldung.algorithms.quicksort.SortingUtils.swap;
|
||||
|
||||
public class DutchNationalFlagPartioning {
|
||||
|
||||
public static Partition partition(int[] a, int begin, int end) {
|
||||
int lt = begin, current = begin, gt = end;
|
||||
int partitioningValue = a[begin];
|
||||
|
||||
while (current <= gt) {
|
||||
int compareCurrent = compare(a[current], partitioningValue);
|
||||
switch (compareCurrent) {
|
||||
case -1:
|
||||
swap(a, current++, lt++);
|
||||
break;
|
||||
case 0:
|
||||
current++;
|
||||
break;
|
||||
case 1:
|
||||
swap(a, current, gt--);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new Partition(lt, gt);
|
||||
}
|
||||
|
||||
public static void quicksort(int[] input, int begin, int end) {
|
||||
if (end <= begin)
|
||||
return;
|
||||
|
||||
Partition middlePartition = partition(input, begin, end);
|
||||
|
||||
quicksort(input, begin, middlePartition.getLeft() - 1);
|
||||
quicksort(input, middlePartition.getRight() + 1, end);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
public class Partition {
|
||||
private int left;
|
||||
private int right;
|
||||
|
||||
public Partition(int left, int right) {
|
||||
super();
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public int getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(int left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public int getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(int right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
public class SortingUtils {
|
||||
|
||||
public static void swap(int[] array, int position1, int position2) {
|
||||
if (position1 != position2) {
|
||||
int temp = array[position1];
|
||||
array[position1] = array[position2];
|
||||
array[position2] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public static int compare(int num1, int num2) {
|
||||
if (num1 > num2)
|
||||
return 1;
|
||||
else if (num1 < num2)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void printArray(int[] array) {
|
||||
if (array == null) {
|
||||
return;
|
||||
}
|
||||
for (int e : array) {
|
||||
System.out.print(e + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BentleyMcilroyPartitioningUnitTest {
|
||||
|
||||
@Test
|
||||
public void given_IntegerArray_whenSortedWithBentleyMcilroyPartitioning_thenGetSortedArray() {
|
||||
int[] actual = {3, 2, 2, 2, 3, 7, 7, 3, 2, 2, 7, 3, 3};
|
||||
int[] expected = {2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 7, 7, 7};
|
||||
BentleyMcIlroyPartioning.quicksort(actual, 0, actual.length - 1);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DNFThreeWayQuickSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithThreeWayQuickSort_thenGetSortedArray() {
|
||||
int[] actual = {3, 5, 5, 5, 3, 7, 7, 3, 5, 5, 7, 3, 3};
|
||||
int[] expected = {3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7};
|
||||
DutchNationalFlagPartioning.quicksort(actual, 0, actual.length - 1);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
|
@ -341,6 +341,7 @@
|
|||
<module>algorithms-miscellaneous-4</module>
|
||||
<module>algorithms-miscellaneous-5</module>
|
||||
<module>algorithms-sorting</module>
|
||||
<module>algorithms-sorting-2</module>
|
||||
<module>algorithms-searching</module>
|
||||
<module>animal-sniffer-mvn-plugin</module>
|
||||
<module>annotations</module>
|
||||
|
@ -985,6 +986,7 @@
|
|||
<module>algorithms-miscellaneous-4</module>
|
||||
<module>algorithms-miscellaneous-5</module>
|
||||
<module>algorithms-sorting</module>
|
||||
<module>algorithms-sorting-2</module>
|
||||
<module>algorithms-searching</module>
|
||||
<module>animal-sniffer-mvn-plugin</module>
|
||||
<module>annotations</module>
|
||||
|
|
Loading…
Reference in New Issue