Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShellSort.java Add shell sort algorithm implementation in Java #141

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions shell_sort/ShellSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.Arrays;

public class ShellSort {

// Driver method for testing sort algorithm
public static void main(String[] args) {
int[] array = {21, 3, 82, 12, 93, 56, 74, 38};
System.out.println("Before sort: " + Arrays.toString(array));
shellSort(array);
System.out.println("After sort: " + Arrays.toString(array));
}

/**
*
* Sorts an array by implementing the shell sort algorithm.
* Shell sort is an in-place comparison sort
* <p>
* Average case = depends on the gap<br>
* Worst case = O(n * log^2 n)<br>
* Best case = O(n)<br>
*
* @param array an unsorted array
*/
public static void shellSort(int[] array) {
int inner, outer;
int temp;

int h = 1;
while (h <= array.length / 3) {
h = h * 3 + 1;
}
while (h > 0) {
for (outer = h; outer < array.length; outer++) {
temp = array[outer];
inner = outer;

while (inner > h - 1 && array[inner - h] >= temp) {
array[inner] = array[inner - h];
inner -= h;
}
array[inner] = temp;
}
h = (h - 1) / 3;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include EOL also. @JonathanGin52

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just to clarify, are you asking for a blank line at the end of the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you take a look again?