How to Implement Shell Sort in Java


In this example we will show the method of shell sort in Java.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class ShellSortExample {

    public static void main(String[] args) {

        int[] arr = {10,34,235,22,1,93};

        sort(arr);

        System.out.println(Arrays.toString(arr));

    }

    private static void sort(int[] arr) {
    	
    	int length = arr.length;//the array length
        while(length != 0){
        	length = length/2;
            for(int i = 0;i < length;i ++){ //group
                for(int j = i + length;j = 0&&temp < arr[k]){//circulate from back to front
                        arr[k + length] = arr[k];
                        k -= length;
                    }
                    arr[k + length] = temp;
                }
            }
        }
    }
}

Output:

[1, 10, 22, 34, 93, 235]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments