Algorithm and Asymptotic Analysis for Binary Tree Search
Name Withheld for Peer Grading
University of the People
Basic sorting methods such as insertion, selection, and bubble follow in-place algorithms, while advanced sorting methods, such as quicksort, shellsort, and heapsort follow divide-and-conquer algorithms (Shaffer, 2011).
In this assignment, I will use quick sort in Java language to show that it is more efficient than the given example implemented by insertion sort.
I have presented the creation of the following code in Java language based on the quick sort method taking into consideration the given example of the insertion sort method and the array of 21 numbers given in the assignment question.
The Quick Sort Code:
import jeliot.io.*; public class QuickSortArray{ public static void main(String args[]) { // Create an array containing the required values in the assignment question. int[] myArray = {12,9,4,99,120,1,3,10,23,45,75,69,31,88,101,14,29,91,2,0,77}; QckSort QckArrSorted = new QckSort()i QckArrSorted.sortQuick(myArray); System.out.println("Ascending sort according to the quick sort algorithm: "); for(int i = 0; i <myArray.length; i++) System.out.print(myArray[i]+" "); System.out.println(); }
} /** * The quick sort method is the main method that sorts the supplied array * so that it returns the exchanges' numbers required for the code to sort the values in ascending order. **/ class QckSort { private int inputArr[] private int lengthArr;
public void sortQuick(int[] arr){ // The if condition checks whether the supplied array is empty or not. if(arr==null || arr.length==0) { return;
} this.inputArr = arr; lengthArr = arr.length; qckSort(o, lengthArr - 1); exchangesNum(); } /* * The quick sort algorithm is recursively applied. */ private void qckSort(int pl, int p2) { // the first part, which is the left. int i = pl; // the second part, which is the right int j = p2; // The pivot determines where the array is split in the quick sort algorithm. int QckSortPivot = inputArr[p1 + (p2 - p1) / 2]; // The value greater than the pivot in the left part is exchanged by the one that is less than the pivot in the right part. while (i <= j) { while (inputArr[i] < QckSortPivot) { i++i } while (inputArr[j] > QckSortPivot) { j--i } if (i<= j){ exeExchange(i, j); i++; // i is the pointer doing an increment in the left position. j--; // j is the pointer doing an decrement in the right position } } // Sort left recursively if (p1 < j) { qckSort(p1, j)i } // Sort right recursively if (i < p2) { qckSort(i, p2)i