Sunday 24 January 2016

Java program for shell sort



import java.util.*;
class ShellSort {
public static void main(String args[]) {
int[] array = new int[] { 56, 25, 4, 1, 14 };
int p, i, j, increment, temp, number_of_elements = array.length;
/* Shell Sort Program */
for (increment = number_of_elements / 2; increment > 0; increment /= 2)
{
for (i = increment; i < number_of_elements; i++)
{
temp = array[i];
for (j = i; j >= increment; j -= increment)
{
if (temp < array[j - increment]) {
array[j] = array[j - increment];
} else {
break;
}
}
array[j] = temp;
}
}
System.out.println("Shell sorted list are:");
for (p = 0; p < 5; p++) {
System.out.println(array[p]);
}
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac ShellSort.java
C:\Users\saty\Desktop>java ShellSort
Shell sorted list are:

1

4

14

25

56

3 comments:

  1. i studied java in a instittude for 1 month. i didnt understand, Still now i understood java

    ReplyDelete
  2. thank you very much .. this really help me ! i owe you ..

    ReplyDelete
  3. Thank u so much to explaining the thing at a glance. Genuinely its really helpful for the beginners.

    ReplyDelete