Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/com/thealgorithms/sorts/PostmanSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.thealgorithms.sorts;

/**
* Postman Sort Algorithm Implementation
*
* @see <a href="https://en.wikipedia.org/wiki/Postman_sort">Postman Sort Algorithm</a>
*/
public class PostmanSort implements SortAlgorithm {

@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array == null || array.length <= 1) {
return array;
}
postmanSort(array);
return array;
}

private <T extends Comparable<T>> void postmanSort(T[] array) {
int n = array.length;
double gapFactor = 1.3;
int gap = (int) (n / gapFactor);

while (gap > 0) {
for (int i = gap; i < n; i++) {
T temp = array[i];
int j = i;
while (j >= gap && SortUtils.greater(array[j - gap], temp)) {
array[j] = array[j - gap];
j -= gap;
}
array[j] = temp;
}
if (gap == 1) {
break;
}
gap = (int) (gap / gapFactor);
if (gap < 1) {
gap = 1;
}
}
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/thealgorithms/sorts/PostmanSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thealgorithms.sorts;

public class PostmanSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new PostmanSort();
}
}