Time Complexity for Shell Sort using bubble sort

45 Views Asked by At

I am a Computer science student learning algorithms and time complexity.

I am trying to work out the time complexity for the best, worst and average case for a shell sort that uses bubble sort instead of insertion sort. This was the sort that I wrote.

public static <T extends Comparable<T>> void shellSort(T[] data) {
    int length = data.length;
    int gap = length / 2;
    boolean swapped;
    // gap is reduced in half each pass until the gap is zero
    while (gap > 0) {
        swapped = true;
        // will keep iterating until we get not values swapped
        while (swapped) {
            swapped = false;
            for (int scan = 0; scan < (length - gap); scan++) {
                // swaps larger values to the right accross a gap
                if (data[scan].compareTo(data[scan + gap]) > 0) {
                    swap(data, scan, (scan + gap));
                    swapped = true;
                    printArr(data);
                }
            }
        }
        gap = gap / 2;
    }
}

I worked out that the best case is when we have a sorted array and this would produce a O(n*logn) time complexity.

Now for the worst case, it seems to me that we will have a O(n^2*logn) complexity?

I am trying to understand how the middle loop will change the overall complexity.

0

There are 0 best solutions below