Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement both MergeSort and Quicksort by following the pseudocode: THANK YOU!!! Algorithm merge(a, tempArray, first, mid, last) beginHalf1 = first; endHalf1 = mid; beginHalf2 =

Implement both MergeSort and Quicksort by following the pseudocode: THANK YOU!!! Algorithm merge(a, tempArray, first, mid, last) beginHalf1 = first; endHalf1 = mid; beginHalf2 = mid + 1; endHalf2 = last // while both subarrays are not empty, compare an entry in one subarray with // entry in the other, then copy the smaller item into tempArray. index = 0; while ((beginHalf1 <= endHalf1) and (beginHalf2 <= endHalf2) if (a[beginHalf1] <= a[beginHalf2]) tempArray[index] = a[beginHalf1] beginHalf1++ else tempArray[index] = a[beginHalf2] beginHalf2++ index++ Copy remaining entries from other subarray to tempArray Copy entries from tempArray to array a void quicksort(index low, index high) { index pivotpoint; if (high > low) { partition(low, high, pivotpoint); quicksort(low, pivotpoint - 1); quicksort(pivotpoint + 1, high); } } LinkedList.java  public class LinkedList> { private Node firstNode; int length; public LinkedList() { firstNode = null; length = 0; } public void add(T anEntry) { Node newNode = new Node(anEntry); newNode.setNextNode(firstNode); firstNode = newNode; length ++; } public void insertionSort() { if (length > 1) { Node unsortedPart = firstNode.getNextNode(); firstNode.setNextNode(null); while (unsortedPart != null) { Node nodeToInsert = unsortedPart; unsortedPart = unsortedPart.getNextNode(); insertInOrder(nodeToInsert); } } } private void insertInOrder(Node nodeToInsert) { T item = nodeToInsert.getData(); Node currentNode = firstNode; Node previousNode = null; // Locate insertion point while ((currentNode != null) && (item.compareTo(currentNode.getData()) > 0)) { previousNode = currentNode; currentNode = currentNode.getNextNode(); } if (previousNode != null) // make the insertion { previousNode.setNextNode(nodeToInsert); nodeToInsert.setNextNode(currentNode); } else // Insert at beginning { nodeToInsert.setNextNode(firstNode); firstNode = nodeToInsert; } } private class Node { private Node next; private T data; private Node(T d) { data = d; } public Node getNextNode() { return next; } public void setNextNode(Node n) { next = n; } public T getData() { return data; } } @Override public String toString() { String returnString = ""; for (Node iterator = firstNode; iterator != null; iterator = iterator.getNextNode()) { returnString += iterator.getData().toString() + " "; } return returnString; } } 

SortArray.java

public class SortArray { public static > void bubbleSort(T[] a, int n) { // ... } public static > void selectionSort(T[] a, int n) { for (int index = 0; index < n - 1; index++) { int indexOfNextSmallest = getIndexOfSmallest(a, index, n - 1); swap(a, index, indexOfNextSmallest); } } public static > void insertionSort(T[] a, int first, int last) { if (first < last) { // sort all but the last entry insertionSort(a, first, last - 1); // Insert the last entry in sorted order insertInOrder(a[last], a, first, last - 1); } } public static > void insertInOrder(T anEntry, T[] a, int begin, int end) { // Inserts anEntry into the sorted array entries a[begin] through a[end] if(anEntry.compareTo(a[end]) > 0) { a[end + 1] = anEntry; } else if (begin < end) { a[end + 1] = a[end]; insertInOrder(anEntry, a, begin, end - 1); } else { a[end + 1] = a[end]; a[end] = anEntry; } } private static > int getIndexOfSmallest(T[] a, int first, int last) { T min = a[first]; int indexOfMin = first; for (int index = first + 1; index <= last; index ++) { if(a[index].compareTo(min) < 0) { min = a[index]; indexOfMin = index; } } return indexOfMin; } private static void swap(Object[] a, int i, int j) { Object temp = a[i]; a[i] = a[j]; a[j] = temp; } } 

Main.java

public class Main { public static void main(String[] args) { Integer [] myArray = { 5, 1, 8, 10, 2, 4 }; SortArray.insertionSort(myArray, 0, myArray.length - 1); for(Integer i : myArray) System.out.println(i); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions

Question

Draw significant resonance structures for the following compound:

Answered: 1 week ago

Question

b. Will new members be welcomed?

Answered: 1 week ago

Question

c. Will leaders rotate periodically?

Answered: 1 week ago

Question

b. Will there be one assigned leader?

Answered: 1 week ago