Shell sort : Sort Search : Collections Data Structure : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  Collections Data Structure   » [  Sort Search  ]  Screenshots 
 



Shell sort



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 * 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 - && data[inner - h>= temp) {
          data[inner= data[inner - h];
          inner -= h;
        }
        data[inner= temp;
      }
      h = (h - 13// 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();
  }
}

           
       
Related examples in the same category
1.  Simple Sort Demo Simple Sort Demo
2.  A simple applet class to demonstrate a sort algorithm
3.  Sorting an array of Strings Sorting an array of Strings
4.  Simple version of quick sort Simple version of quick sort
5.  Combine Quick Sort Insertion Sort Combine Quick Sort Insertion Sort
6.  Quick sort with median-of-three partitioning Quick sort with median-of-three partitioning
7.  Selection sort Selection sort
8.  Insert Sort for objects Insert Sort for objects
9.  Insert sort Insert sort
10.  Bubble sort Bubble sort
11.  Merge sort Merge sort
12.  Binary Search Binary Search
13.  Topological sorting Topological sorting
14.  Heap sort Heap sort
15.  Sort Numbers Sort Numbers
16.  A quick sort demonstration algorithm A quick sort demonstration algorithm
17.  Customized Sort Test
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.