chore(Java): add shell sort (#262)

pull/269/head
Vasu Manhas 2021-04-26 20:30:44 +05:30 committed by GitHub
parent 6df823b865
commit 190d51583c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -1,4 +1,4 @@
# Python
# Java
## Arrays
@ -45,6 +45,7 @@
5. [Merge Sort](sorting/merge-sort.java)
6. [Quick Sort](sorting/quick-sort.java)
7. [Selection Sort](sorting/selection-sort.java)
8. [Shell Sort](sorting/shell-sort.java)
## Stacks

View File

@ -0,0 +1,19 @@
public class ShellSortApp {
public static void main(String[] args) {
int[] intArray = { 20, 35, -15, 7, 55, 1, -22 };
for (int gap = intArray.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < intArray.length; i++) {
int newElement = intArray[i];
int j = i;
while (j >= gap && intArray[j - gap] > newElement) {
intArray[j] = intArray[j - gap];
j -= gap;
}
intArray[j] = newElement;
}
}
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}
}
}