Skip to main content
deleted 8 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Has this implementation of Shell-sort Algorithm the samealgorithm complexity as the usual implementation?

I tried to implement the Shellshell-sort Algorithmalgorithm using this javaJava code  :

// Using Pratt sequence 
public static void hSort(int tab[]) {
    int N = tab.length;    
    int k = 0;    
    int sequence = 1;

    // Getting the final term of the pratt sequence 
    while(((int)(Math.pow(3, k) - 1) / 2) < N/3) {
        sequence = (int)(Math.pow(3, k) - 1) / 2;
        k++;
    }     
    k--;

    while(sequence > 0) {
        hInsertionSort(tab, sequence);
        k--;
        sequence = (int)(Math.pow(3, k) - 1) / 2;
    }
}

// H sorting using insertion sort 
public static void hInsertionSort(int[] tab, int h) {
    int N = tab.length;
    int k = 0;
    while (k < h) {
        for (int i = k; i < N; i+=h) {
            for (int j = i; j > k+h-1; j-=h) {
                if (less(tab[j], tab[j-h]) == -1) exch(tab, j, j-h);
                else break;
            }
        }
    k++;
    }
}    

You can use the 'hsort'hsort method to try sort a table of integers.

Is the complexity of this implementation the same as the usual implementation  ?

Has this implementation of Shell-sort Algorithm the same complexity as the usual implementation?

I tried to implement the Shell-sort Algorithm using this java code  :

// Using Pratt sequence 
public static void hSort(int tab[]) {
    int N = tab.length;    
    int k = 0;    
    int sequence = 1;

    // Getting the final term of the pratt sequence 
    while(((int)(Math.pow(3, k) - 1) / 2) < N/3) {
        sequence = (int)(Math.pow(3, k) - 1) / 2;
        k++;
    }     
    k--;

    while(sequence > 0) {
        hInsertionSort(tab, sequence);
        k--;
        sequence = (int)(Math.pow(3, k) - 1) / 2;
    }
}

// H sorting using insertion sort 
public static void hInsertionSort(int[] tab, int h) {
    int N = tab.length;
    int k = 0;
    while (k < h) {
        for (int i = k; i < N; i+=h) {
            for (int j = i; j > k+h-1; j-=h) {
                if (less(tab[j], tab[j-h]) == -1) exch(tab, j, j-h);
                else break;
            }
        }
    k++;
    }
}    

You can use the 'hsort' method to try sort a table of integers.

Is the complexity of this implementation the same as the usual implementation  ?

Shell-sort algorithm complexity

I tried to implement the shell-sort algorithm using this Java code:

// Using Pratt sequence 
public static void hSort(int tab[]) {
    int N = tab.length;    
    int k = 0;    
    int sequence = 1;

    // Getting the final term of the pratt sequence 
    while(((int)(Math.pow(3, k) - 1) / 2) < N/3) {
        sequence = (int)(Math.pow(3, k) - 1) / 2;
        k++;
    }     
    k--;

    while(sequence > 0) {
        hInsertionSort(tab, sequence);
        k--;
        sequence = (int)(Math.pow(3, k) - 1) / 2;
    }
}

// H sorting using insertion sort 
public static void hInsertionSort(int[] tab, int h) {
    int N = tab.length;
    int k = 0;
    while (k < h) {
        for (int i = k; i < N; i+=h) {
            for (int j = i; j > k+h-1; j-=h) {
                if (less(tab[j], tab[j-h]) == -1) exch(tab, j, j-h);
                else break;
            }
        }
    k++;
    }
}

You can use the hsort method to try sort a table of integers.

Is the complexity of this implementation the same as the usual implementation?

Source Link
user147641
user147641

Has this implementation of Shell-sort Algorithm the same complexity as the usual implementation?

I tried to implement the Shell-sort Algorithm using this java code :

// Using Pratt sequence 
public static void hSort(int tab[]) {
    int N = tab.length;    
    int k = 0;    
    int sequence = 1;

    // Getting the final term of the pratt sequence 
    while(((int)(Math.pow(3, k) - 1) / 2) < N/3) {
        sequence = (int)(Math.pow(3, k) - 1) / 2;
        k++;
    }     
    k--;

    while(sequence > 0) {
        hInsertionSort(tab, sequence);
        k--;
        sequence = (int)(Math.pow(3, k) - 1) / 2;
    }
}

// H sorting using insertion sort 
public static void hInsertionSort(int[] tab, int h) {
    int N = tab.length;
    int k = 0;
    while (k < h) {
        for (int i = k; i < N; i+=h) {
            for (int j = i; j > k+h-1; j-=h) {
                if (less(tab[j], tab[j-h]) == -1) exch(tab, j, j-h);
                else break;
            }
        }
    k++;
    }
}    

You can use the 'hsort' method to try sort a table of integers.

Is the complexity of this implementation the same as the usual implementation ?