Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Implement the insertion sort and the Shell sort so that they count the numb

ID: 3603851 • Letter: J

Question

JAVA

Implement the insertion sort and the Shell sort so that they count the number of comparisons made during a sort. Use your implementations to compare the two sorts on arrays of random Integer objects of various sizes. Also, compare the Shell sort as implemented in Segment 8.23(8.23 The heart of the Shell sort is the adaptation of the insertion sort to work on a subarray of equally spaced entries.) with a revised Shell sort that adds 1 to space any time it is even. For what array size does the difference in the number of comparisons become significant? Is the size consistent with the size predicted by the algorithm’s Big Oh?

Implement and exercise the algorithm to verify its functionality. Be sure to test with multiple values to insure its correct implementation.

Explanation / Answer

// Java implementation of ShellSort

class ShellSort

{

    /* An utility function to print array of size n*/

    static void printArray(int arr[])

    {

        int n = arr.length;

        for (int i=0; i<n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    /* function to sort arr using shellSort */

    int sort(int arr[])

    {

        int n = arr.length;

int count=0;

        // Start with a big gap, then reduce the gap

        for (int gap = n/2; gap > 0; gap /= 2)

        {

            // Do a gapped insertion sort for this gap size.

            // The first gap elements a[0..gap-1] are already

            // in gapped order keep adding one more element

            // until the entire array is gap sorted

            for (int i = gap; i < n; i += 1)

            {

                // add a[i] to the elements that have been gap

                // sorted save a[i] in temp and make a hole at

                // position i

                int temp = arr[i];

                // shift earlier gap-sorted elements up until

                // the correct location for a[i] is found

                int j;

                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)

                    arr[j] = arr[j - gap];

                // put temp (the original a[i]) in its correct

                // location

                arr[j] = temp;

            }

count++;

        }

        return 0;

    }

    // Driver method

    public static void main(String args[])

    {

        int arr[] = {12, 34, 54, 2, 3};

        System.out.println("Array before sorting");

        printArray(arr);

        ShellSort ob = new ShellSort();

        ob.sort(arr);

        System.out.println("Array after sorting");

        printArray(arr);

    }

// Java program for implementation of Insertion Sort

class InsertionSort

{

    /*Function to sort array using insertion sort*/

int count=0;

    void sort(int arr[])

    {

        int n = arr.length;

        for (int i=1; i<n; ++i)

        {

            int key = arr[i];

            int j = i-1;

            /* Move elements of arr[0..i-1], that are

               greater than key, to one position ahead

               of their current position */

            while (j>=0 && arr[j] > key)

            {

                arr[j+1] = arr[j];

                j = j-1;

            }

            arr[j+1] = key;

        }

count=0;

    }

    /* A utility function to print array of size n*/

    static void printArray(int arr[])

    {

        int n = arr.length;

        for (int i=0; i<n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    // Driver method

    public static void main(String args[])

    {       

        int arr[] = {12, 11, 13, 5, 6};

        InsertionSort ob = new InsertionSort();       

        ob.sort(arr);

         

        printArray(arr);

    }

}

}

// Java implementation of ShellSort

class ShellSort

{

    /* An utility function to print array of size n*/

    static void printArray(int arr[])

    {

        int n = arr.length;

        for (int i=0; i<n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    /* function to sort arr using shellSort */

    int sort(int arr[])

    {

        int n = arr.length;

int count=0;

        // Start with a big gap, then reduce the gap

        for (int gap = n/2; gap > 0; gap /= 2)

        {

            // Do a gapped insertion sort for this gap size.

            // The first gap elements a[0..gap-1] are already

            // in gapped order keep adding one more element

            // until the entire array is gap sorted

            for (int i = gap; i < n; i += 1)

            {

                // add a[i] to the elements that have been gap

                // sorted save a[i] in temp and make a hole at

                // position i

                int temp = arr[i];

                // shift earlier gap-sorted elements up until

                // the correct location for a[i] is found

                int j;

                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)

                    arr[j] = arr[j - gap];

                // put temp (the original a[i]) in its correct

                // location

                arr[j] = temp;

            }

count++;

        }

        return 0;

    }

    // Driver method

    public static void main(String args[])

    {

        int arr[] = {12, 34, 54, 2, 3};

        System.out.println("Array before sorting");

        printArray(arr);

        ShellSort ob = new ShellSort();

        ob.sort(arr);

        System.out.println("Array after sorting");

        printArray(arr);

    }

// Java program for implementation of Insertion Sort

class InsertionSort

{

    /*Function to sort array using insertion sort*/

int count=0;

    void sort(int arr[])

    {

        int n = arr.length;

        for (int i=1; i<n; ++i)

        {

            int key = arr[i];

            int j = i-1;

            /* Move elements of arr[0..i-1], that are

               greater than key, to one position ahead

               of their current position */

            while (j>=0 && arr[j] > key)

            {

                arr[j+1] = arr[j];

                j = j-1;

            }

            arr[j+1] = key;

        }

count=0;

    }

    /* A utility function to print array of size n*/

    static void printArray(int arr[])

    {

        int n = arr.length;

        for (int i=0; i<n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    // Driver method

    public static void main(String args[])

    {       

        int arr[] = {12, 11, 13, 5, 6};

        InsertionSort ob = new InsertionSort();       

        ob.sort(arr);

         

        printArray(arr);

    }

}

}

-->When elemsnts are in descending order, no. of comparisins for insertion sort becomes signifiantly larger than shell sort.