Page 1 of 1

The first part, part A, is a gnome sort that uses iteration to achieve the sorting of Comparable objects. Part B & C are

Posted: Mon May 09, 2022 7:18 am
by answerhappygod
The first part, part A, is a gnome
sort that uses iteration to achieve the sorting of
Comparable objects.
Part B & C are both parts of the same type of algorithm,
just implemented with recursion.
The First Part Part A Is A Gnome Sort That Uses Iteration To Achieve The Sorting Of Comparable Objects Part B C Are 1
The First Part Part A Is A Gnome Sort That Uses Iteration To Achieve The Sorting Of Comparable Objects Part B C Are 1 (84.42 KiB) Viewed 21 times
The First Part Part A Is A Gnome Sort That Uses Iteration To Achieve The Sorting Of Comparable Objects Part B C Are 2
The First Part Part A Is A Gnome Sort That Uses Iteration To Achieve The Sorting Of Comparable Objects Part B C Are 2 (57.32 KiB) Viewed 21 times
DRIVER:
import java.util.Arrays;
import java.util.stream.IntStream;
public class SortingDriver {
public static void main(String[] args) {
Integer[] arr =
IntStream.generate(() -> (int) (Math.random() * 5000))


.boxed()


.limit(100)


.toArray(Integer[]::new);
printFirstTenOfArray(arr);
Integer[] copy1 =
copyArray(arr);
Integer[] copy2 = copyArray(arr);
System.out.println("\nGnome
Sort...");
SortingUtility.gnomeSort(copy1);
printFirstTenOfArray(copy1);
System.out.println("\nGnomier
Sort...");

SortingUtility.gnomierSort(copy2);
printFirstTenOfArray(copy2);
}
private static <T> void
printFirstTenOfArray(T[] obj) {
if (obj != null) {
int counter = 1;

System.out.println("\nFirst 10 of Array...");
for (T i : obj)
{

System.out.println(counter + ": " + i.toString());
if
(counter >= 10) {

break;
}

counter++;
}
} else {
System.out.println("Array
is null.");
}
}
private static <T> T[] copyArray(T[] obj)
{
T[] copy = (T[]) Arrays.copyOf(obj,
obj.length);
return copy;
}
}
public class SortingUtility { /** * Part A Gnome Sort Algorithm * <p> * Implement gnome sort per this pseudocode. ** <p> <pre> method gnomeSort(a[]) pos = 0 while pos < length(a) if (pos == 0 or a[pos] >= a[pos-1]) pos = pos + 1 else swap a[pos] and a[pos-1] pos = pos - 1 * </pre> * @param data @param <T> * @see <a href="https://en.wikipedia.org/wiki/Gnome_sor ... me_sort</a> * public static <t extends Comparable<T>> void gnomeSort(T[] data) { // TODO implement pseudocode above }
* * Part B Optimized Gnome Sort Algorithm. <p> * Implement an optimized gnome sort per the pseudocode below. <p> sk sk <pre> method gnomierSort(a[]). for pos in 1 to length(a) gnomiersort(a, pos) </pre> > @param data @param <T> @see <a href="https://en.wikipedia.org/wiki/Gnome_sor ... me_sort</a> * public static <t extends Comparable<T>> void gnomiersort(T[] data) { // TODO implement pseudocode above
} ** /** * Part C Optimized Gnome Sort Algorithm <p> Implement an optimized gnome sort per the pseudocode below. <p> <pre> method gnomierSort(a[], upperBound) pos = upperBound while pos > 0 and a[pos-1] > a[pos] swap a[pos-1] and a[pos] pos = pos - 1 </pre> @param data * @param <T> */ private static <t extends Comparable<T>> void gnomiersort(T[] data, int upperBound) { // TODO implement pseudocode above } private static <t extends Comparable<T>> void swap(T[] data, int index1, int index2) { I temp = data[index1]; data[index1] data[index2]; data[index2] = temp; = } }