Skip to main content

Student Portal > Contests > 

Programming Contest Central

   
Welcome students Resources Competition ProfilesFAQ
  Contest calendar
  Problem of the month
  Past problems
  Games and brainteasers
  Successes
  Tutorials

Shell Sort

public class ShellSort{
  public static void main(String[] args) {
   
    int array[] = {6, 3, 5, 4, 9, 2, 7};
   
    int arraySize = 7;
   
    int i, j, increment, temp;
   
    increment = 3;
   
    while (increment > 0)
    {
      for (i=0; i < arraySize; i++)
      {
        j = i;
        temp = array
          [i];
        while ((j >= increment) && (array[j-increment] > temp))
        {
          array
            [j] = array
            [j - increment];
          j = j - increment;
        }
        array[j] = temp;
      }
      if (increment/2 != 0) {
        increment = increment/2;
      }
      else if (increment == 1) {
        increment = 0;
      }
      else {
        increment = 1;
      }
    }
   
    for (i = 0; i < arraySize; i++) {
      System.out.print(array[i] + " ");
    }
  }
}


Back to top
 


Quick links

Student forum

Teacher resources

RSS Feed