Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c) Suppose you take a size n array (assume that n = 2m), and start dividing the

ID: 3597740 • Letter: C

Question

c) Suppose you take a size n array (assume that n = 2m), and start dividing the array into halves until you have one element in the bottom level. Show that the height of the tree structure created by the above process has the height m and m=log (n).

d) Compare and contrast above three sorting algorithm based on time and space complexity. Consider the best-case, worst-case, and average case scenarios.

3. Experimentally proving the time complexity of sorting algorithms. During lectures we have discussed how to determine the time run a program. Starting from 10000 elements until 60000 elements increment by 10000, create an integer array with random numbers and apply the above three sorting algorithms and the Java built in sorting algorithm Arrays.sort() separately to sort the array and time it. Then fill the following table. Using Microsoft excel, plot the times against the size of the array and see if part (d) matches with your data.

Array Size Selection Sort Merge sort Quick Sort Arrays.Sort() 10000 20000 30000 40000 50000 60000

Explanation / Answer

SelectionSortProgram :

package com.nt.Prasad;

public class Selectionsortprogram {

public static void selectionSort(int[] array){  

for (int i = 0; i < array.length - 1; i++)  

{  

int index = i;  

for (int j = i + 1; j < array.length; j++){  

if (array[j] < array[index]){  

index = j;

}  

}  

int smallNumber = array[index];

array[index] = array[i];  

array[i] = smallNumber;  

}  

}  

public static void main(String a[]){  

int[] array1 = {60000,20000,10000,30000,50000,40000};  

System.out.println("-----------Before Selection Sort--------------");  

for(int i:array1){  

System.out.print(i+" ");  

}  

System.out.println();  

  

selectionSort(array1);//sorting array using selection sort  

System.out.println("------------After Selection Sort------------");  

for(int i:array1){  

System.out.print(i+" ");  

}  

}  

}

MergeSort:

package com.nt.Prasad;

import java.util.Arrays;

public class MergesortProgram {

public static void main(String[] args)

{

Integer[] array1 = {20000,40000,10000,30000,60000,50000};

System.out.println("---------Before Merge Sort-----------");

System.out.println(Arrays.toString(array1));

mergeSort(array1);

System.out.println("---------After Merge Sort-----------");

System.out.println(Arrays.toString(array1));

}

public static void mergeSort(Comparable [ ] i)

{

Comparable[] temp = new Comparable[i.length];

mergeSort(i, temp, 0, i.length - 1);

}

private static void mergeSort(Comparable [ ] i, Comparable [ ] temp, int left, int right)

{

if( left < right )

{

int center = (left + right) / 2;

mergeSort(i, temp, left, center);

mergeSort(i, temp, center + 1, right);

mergesort(i, temp, left, center + 1, right);

}

}

private static void mergesort(Comparable[ ] a, Comparable[ ] temp, int left, int right, int rightEnd )

{

int leftEnd = right - 1;

int k = left;

int num = rightEnd - left + 1;

while(left <= leftEnd && right <= rightEnd)

if(a[left].compareTo(a[right]) <= 0)

temp[k++] = a[left++];

else

temp[k++] = a[right++];

while(left <= leftEnd)

temp[k++] = a[left++];

while(right <= rightEnd)

temp[k++] = a[right++];

for(int i = 0; i < num; i++, rightEnd--)

a[rightEnd] = temp[rightEnd];

}

}

Quick Sort:

package com.nt.Prasad;

public class QuickSortProgram {

private int array[];

private int length;

public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {

return;

}

this.array = inputArr;

length = inputArr.length;

quickSort(0, length - 1);

}

private void quickSort(int lowerIndex, int higherIndex) {

int a = lowerIndex;

int b = higherIndex;

int pivotnumber = array[lowerIndex+(higherIndex-lowerIndex)/2];

while (a <= b) {

while (array[a] < pivotnumber) {

a++;

}

while (array[b] > pivotnumber) {

b--;

}

if (a <= b) {

changeNumbers(a, b);

a++;

b--;

}

}

if (lowerIndex < b)

quickSort(lowerIndex, b);

if (a < higherIndex)

quickSort(a, higherIndex);

}

private void changeNumbers(int i, int j) {

int temp = array[i];

array[i] = array[j];

array[j] = temp;

}

public static void main(String a[]){

QuickSortProgram sorter = new QuickSortProgram();

int[] input = {40000,20000,30000,60000,10000,50000};

sorter.sort(input);

for(int i:input){

System.out.print(i);

System.out.print(" ");

}

}

}

ArraySortExample :

package com.nt.Prasad;

import java.util.Arrays;

public class ArraySort {

public static void main(String[] args) {

int[] array = { 30000, 60000, 50000, 20000, 40000, 10000 };

System.out.println("---------Before Merge Sort-----------");

System.out.println(Arrays.toString(array));

Arrays.sort(array);

System.out.println("---------After Merge Sort-----------");

System.out.println(Arrays.toString(array));

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote