0

I was told to present Shell Sort in the class and I did. I researched a lot just to see that most of them wrote O(n log n) for best case time complexity. Yet after showing this algorithm to my teacher:


import java.util.Arrays;

class ShellSort{
  void shellSort(int array[], int n){
    for (int gap = n/2; gap > 0; gap /= 2){
      for (int i = gap; i < n; i += 1) {
        int temp = array[i];
        int j;
        for (j = i; j >= gap && array[j - gap] > temp; j -= gap){
          array[j] = array[j - gap];
        }
        array[j] = temp;
      }
   }
}

  public static void main(String args[]){
    int[] data={9, 8, 3, 7, 5, 6, 4, 1};
    int size=data.length;
    ShellSort ss = new ShellSort();
    ss.shellSort(data, size);
    System.out.println("Sorted Array in Ascending Order: ");
    System.out.println(Arrays.toString(data));
  }
}

My teacher immediately refuted me by saying that "at most this sorting algorithm is just n^3".

She forgot to teach us how to get the time complexity of a program so I'm at lost here.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

0 Answers0