how to construct an array of 100 elements containing the numbers 1 -100 for which shellsort, with the increments 1 4 13 40 (The worst case)?

42 views Asked by At

This is the original question

"Shell Sort worst case. Construct an array of 100 elements containing the numbers 1 through 100 for which shellsort, with the increments 1 4 13 40, uses as large a number of compares as you can find."

There are 100! permutations for an array of 100 elements, it's terrifying to go through each permutation and find which one has the maximum number of compares. Is there any smarter way to approach this problem? My approach this problem is through violence, but only randomly shuffle the array 100000000 time which is less than 100! and it take me half an hour to get the final output.

I pasted my code below. I appreciate any suggestions from you guys!

`

package ch_2_1;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

import java.util.Arrays;

public class exer_19
{
    public static void main(String[] args)
    {
        // initial  permutation
        int[] array = new int[100];
        for ( int i = 1; i < 101; i++)
        {
            array[i-1] = i;
        }
        // find the worst case and the number of compares
        worst_input(array);
    }

    private static void worst_input(int[] array)
    {
        int max_count = 0;
        int[] copy = new int[100];
        int[] worst_case = new int[100];
        for ( int i = 0; i < 100000000; i++)
        {
            int[] temp = generate(array);
            for (int j = 0; j < 100; j++){ copy[j] = temp[j];}
            Shell_sort operation = new Shell_sort();
            operation.shell_sort(temp);
            if (operation.compare() > max_count)
            {
                max_count = operation.compare();
                worst_case = copy;
            }
            System.out.println(i);
        }
        for ( int s : worst_case){ System.out.print(s + " ");}
        System.out.println();
        System.out.println(max_count);
        System.out.println();

    }
    private static int[] generate( int[] array)
    {
        StdRandom.shuffle(array);
        return array;
    }

    private static class Shell_sort // it's necessary to create a private class to hold the shell sort method
        // b/c the method must record the # of compares to sort the array, and this # count need to be returned
        // to the worst_input method. Therefore, having a class to encapsulate the two methods is very helpful
    {
        private int count = 0;
        private void shell_sort(int[] test)
        {
            int N = test.length;
            int h = 1;
            while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121...
            while ( h > 0)
            {
                for ( int i = h; i < N; i++) // starting from the largest h-value th element of the array (simplified: ith element)
                {
                    // if ith element is less than i-h th element, swap the two, continue this process until the condition is not met
                    for ( int j = i; j >= h && less( test[j], test[j-h]); j = j - h)
                    {
                        exchange( test, j, j-h);
                        count++;
                    }
                }
                // when reached the end of the array, update h value
                h = h/3;
            }
        }

        private int compare()
        {
            return count;
        }
    }

    private static boolean less( int current, int previous)
    {
        return current < previous;
    }

    private static void exchange(int[] array, int cur_index, int pre_index)
    {
        int temp = array[pre_index];
        array[pre_index] = array[cur_index];
        array[cur_index] = temp;
    }

}

`

0

There are 0 answers