Saturday, January 25, 2014

ShellSort Programme in Java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package genericspro;

/**
 *
 * @author Rishitha
 */
public class ShellSort {

    public static void main(String args[]) {

        int arr[] = {15, 14, 13, 12, 11, 10, 9,8,7,6,5,4,3,2,1};
        int num = 15;
        int tmp;
        System.out.println("Before Sorting ");
        for (int k = 0; k < num; k++) {
            System.out.println(" " + arr[k] + "\n");
        }

        for (int i = num / 2; i > 0; i = i / 2) {
            for (int j = i; j < num; j++) {
                for (int k = j - i; k >= 0; k = k - i) {
                    //System.out.println("i:"+i+" j: "+j+" k: "+k);
                    if (arr[k + i] >= arr[k]) {
                       //System.out.println(" "+arr[k+i] +">= "+arr[k]);
                        break;
                    } else {
                        //System.out.println(" "+arr[k+i] +"not >= "+arr[k]);
                        tmp = arr[k];
                        arr[k] = arr[k + i];
                        arr[k + i] = tmp;
                    }
                }
            }
        }
        System.out.println("\t**** After Shell Sorting ****\n");
        for (int k = 0; k < num; k++) {
            System.out.println(" " + arr[k] + "\n");
        }

    }

}

No comments:

Post a Comment