Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the Below codes and need help with Modify the shellSort, bubbleSort, and bubbleSort2 methods from above by adding code to each to tally

I have the Below codes and need help with

Modify the shellSort, bubbleSort, and bubbleSort2 methods from above by adding code to each to tally the total number of comparisons made between the elements being sorted (i.e., just the compareTo() calls; ignore relational operators between indexes). Also tally and report the number of swaps that occur. Determine and report the total execution time of each algorithm. Comment out the outputs of each swap or pass for this final step. Execute each of these sort algorithms against the same list, recording information for the total number of comparisons and total execution time. The driver should construct lists of size 10, 100, and 1000both in random and already in sorted order. Use the Integer wrapper class for the array type.

Use a spreadsheet to present the test cases you have prepared, along with the comparisons, swaps, and execution time. Describe how the data obtained relates to the theoretical discussion of algorithm efficiency presented in the chapter.

Codes:

ShellArray

import java.util.Random;

class ShellArray {

/* function to implement shellSort */

static void shell(int a[], int n)

{

/* Rearrange the array elements at n/2, n/4, ..., 1 intervals */

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

{

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

{

/* store a[i] to the variable temp and make

the ith position empty */

int temp = a[i];

int j;

for (j = i; j >= interval && a[j - interval] >

temp; j -= interval)

a[j] = a[j - interval];

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

position */

a[j] = temp;

}

}

}

static void printArr(int a[], int n) /* function to print the array elements */

{

int i;

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

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

}

public static void main(String args[])

{

int a[] = { 3, 4, 6, 7, 9, 15, 18, 20, 11, 18, 40, 60, 15, 40, 50, 10, 17, 19, 15, 11};

int n = a.length;

System.out.println("Before sorting array elements are - ");

printArr(a, n);

shell(a, n);

System.out.println(" After applying shell sort, the array elements are - ");

printArr(a, n);

}

}

Bubble Sorts:

import java.util.Arrays;

public class TestSorting { // To swap the two given data in array public static void swap(int temparr[], int a, int b) { int temp = temparr[a]; temparr[a] = temparr[b]; temparr[b] = temp; } // Normal bubble sort public static void bubbleSort(int array[], int size) { int temparr[] = Arrays.copyOf(array, size); // Create a copy of given array of given size System.out.println(" Sorting array in ascending order using normal bubble sort: "); // Outer for loop to access each elements of array for (int i = 0; i < size - 1; i++) {

// Inner loop to compare and swap array elements if they are out of order for (int j = 0; j < size - i - 1; j++) { if (temparr[j] > temparr[j + 1]) { swap(temparr, j, (j + 1)); // swap the data in array } } // Print array after every pass System.out.println("Array after " + (i+1) + " pass: " + Arrays.toString(temparr)); } }

// Updated bubble sort public static void bubbleSort2(int array[], int size) { int temparr[] = Arrays.copyOf(array, size); // Create a copy of given array of given size int i = 0; int flag = 0; boolean swapFlag = true; System.out.println(" Sorting array in ascending order using updated bubble sort: "); // Outer while loop to access each elements of array and stops as soon as array gets sorted while(i < size - 1 && swapFlag){

// Inner for loop to compare and swap array elements if they are out of order for (int j = 0; j < size - i - 1; j++) {

if (temparr[j] > temparr[j + 1]) { swap(temparr, j, (j + 1)); // swap the data in array flag = 1; // mark flag as 1 } } // Print array after every pass System.out.println("Array after " + (i+1) + " pass: " + Arrays.toString(temparr)); // Check if flag is not changed then mark the swapFlag as false if(flag == 0) { swapFlag = false; } i++; // increment i by 1 (index counting for outer while loop) } } public static void main(String args[]) {

int[] arr1 = {10, 16, 11, 15, 8, 12, 7, 14, 10, 14, }; // Random data int[] arr2 = {7, 8, 10, 10, 11, 12, 14, 14, 15, 16, }; // Already sorted data

// call both sorting method passing the random data bubbleSort(arr1, arr1.length); bubbleSort2(arr1, arr1.length);

// call both sorting method passing the already sorted data bubbleSort(arr2, arr2.length); bubbleSort2(arr2, arr2.length);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions