Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import static java.lang.System.*;
- /**
- * Created by MOHIT on 31-12-2016.
- */
- public class shell {
- public static void main(String arg[]) {
- Scanner sc = new Scanner(in);
- System.out.print("Enter the Array to be sorted : ");
- String str1 = sc.next();
- String[] strArr = str1.split(",");
- int[] numbers = new int[strArr.length];
- int i=0;
- for (String str2 :strArr) {
- int temp = Integer.parseInt(str2);
- numbers[i] = temp;
- i++;
- }
- numbers = shellSort(numbers);
- System.out.println("The Array after sorting is : ");
- for(i=0; i<numbers.length; i++){
- System.out.print(numbers[i] + " ");
- }
- }
- private static int[] shellSort(int[] numbers){
- int gap = numbers.length / 2;
- while (gap > 0){
- // loop till i is in range from gaps to lenght of the list
- for (int i = 0; i < numbers.length; i++){
- // store the ith element in the temp variable
- int temp = numbers[i];
- int j = i;
- // applying insertion sort on the sublist
- // shifting the elements larger than the ith element to the right
- while(j >= gap && numbers[j - gap] > temp){
- numbers[j] = numbers[j - gap];
- j -= gap;
- }
- // puting the element at its correct position
- numbers[j] = temp;
- }
- // reduce gap by it half
- gap = gap/2;
- }
- return numbers;
- }
- }
- /*
- Enter the Array to be sorted : 231,3423,35,12,34,54,12,5
- The Array after sorting is :
- 5 12 12 34 35 54 231 3423
- */
Add Comment
Please, Sign In to add comment