Results 1 to 1 of 1
Thread: Shell Sort in Java
-
Shell Sort in Java
Java Code:public class ShellSort { private long[] data; private int len; public ShellSort(int max) { data = new long[max]; len = 0; } public void insert(long value){ data[len] = value; len++; } public void display() { System.out.print("Data:"); for (int j = 0; j < len; j++) System.out.print(data[j] + " "); System.out.println(""); } public void shellSort() { int inner, outer; long temp; //find initial value of h int h = 1; while (h <= len / 3) h = h * 3 + 1; // (1, 4, 13, 40, 121, ...) while (h > 0) // decreasing h, until h=1 { // h-sort the file for (outer = h; outer < len; outer++) { temp = data[outer]; inner = outer; // one subpass (eg 0, 4, 8) while (inner > h - 1 && data[inner - h] >= temp) { data[inner] = data[inner - h]; inner -= h; } data[inner] = temp; } h = (h - 1) / 3; // decrease h } } public static void main(String[] args) { int maxSize = 10; ShellSort arr = new ShellSort(maxSize); for (int j = 0; j < maxSize; j++) { long n = (int) (java.lang.Math.random() * 99); arr.insert(n); } arr.display(); arr.shellSort(); arr.display(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Heap Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-16-2008, 10:27 PM -
Merge Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:43 PM -
Bubble Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks