QUESTION : (Solved) : Code Public Class Arraysorter Insertion Sort Public Static Void Insertionsort T Int N Inse Q32239286 . . .
this is the code :

public class ArraySorter {

// INSERTION SORT

public static <T extends Comparable<? super T>>

void insertionSort(T[] a, int n) {

insertionSort(a, 0, n – 1);

} // end insertionSort

public static <T extends Comparable<? super T>>

void insertionSort(T[] a, int first, int last) {

for (int unsorted = first + 1; unsorted <= last; unsorted++){ // Assertion: a[first] <= a[first + 1] <= … <=a[unsorted – 1]

T firstUnsorted = a[unsorted];

insertInOrder(firstUnsorted, a, first, unsorted – 1);

} // end for

} // end insertionSort

private static <T extends Comparable<? super T>>

void insertInOrder(T anEntry, T[] a, int begin, int end) {

int index = end;

while ((index >= begin) &&(anEntry.compareTo(a[index]) < 0)) {

a[index + 1] = a[index]; // Make room

index–;

} // end for

// Assertion: a[index + 1] is available

a[index + 1] = anEntry; // Insert

} // end insertInOrder

// SHELL SORT

public static <T extends Comparable<? super T>>

void shellSort(T[] a, int n) {

shellSort(a, 0, n – 1);

} // end shellSort

/**

* Sorts equally spaced elements of an array into ascendingorder.

*

* @param a An array of Comparable objects.

* @param first An integer >= 0 that is the index of the firstarray element

* to consider.

* @param last An integer >= first and < a.length that isthe index of the

* last array element to consider. @param space The differencebetween the

* indices of the

eleme

* nts to sort.

*/

public static <T extends Comparable<? super T>>

void shellSort(T[] a, int first, int last) {

int n = last – first + 1; // Number of array entries

int space = n / 2;

while (space > 0) {

for (int begin = first; begin < first + space; begin++) {

incrementalInsertionSort(a, begin, last, space);

} // end for

space = space / 2;

} // end while

} // end shellSort

// BETTER SHELL SORT

/**

* Avoids even spacing

*/

public static <T extends Comparable<? super T>>

void betterShellSort(T[] a, int n) {

betterShellSort(a, 0, n – 1);

} // end betterShellSort

// Exercise 14, Chapter 8

public static <T extends Comparable<? super T>>

void betterShellSort(T[] a, int first, int last) {

int n = last – first + 1; // Number of array elements

for (int space = n / 2; space > 0; space = space / 2) {

if (space % 2 == 0) // If space is even, add 1

{

space++;

}

for (int begin = first; begin < first + space; begin++) {

incrementalInsertionSort(a, begin, last, space);

}

} // end for

} // end betterShellSort

private static <T extends Comparable<? super T>>

void incrementalInsertionSort(T[] a, int first, int last, intspace) {

int unsorted, index;

for (unsorted = first + space; unsorted <= last;

unsorted = unsorted + space) {

T nextToInsert = a[unsorted];

index = unsorted – space;

while ((index >= first) &&(nextToInsert.compareTo(a[index]) < 0)) {

a[index + space] = a[index];

index = index – space;

} // end while

a[index + space] = nextToInsert;

} // end for

} // end incrementalInsertionSort

} // end ArraySorter

The Algorithm 8.23 The heart of the Shell sort is the adaptation of the insertion sort to work on a subarray of equally spaced entries. By combining and modifying the two algorithms that describe the insertion sort, as given in Segment 8.10, we obtain the following algorithm that sorts array entries whose indices are separated by an increment of space Algorithm incrementalInsertionSort(a, first, last, space) Sorts equally spaced entries of an array a [first..1ast] into ascending order // first >= 0 and = first and = first) and (nextToInsert.compareTo(a[index)) Show transcribed image text

## Expert Answer

. . .

**view full answer**