(10 points) Basic. Decision tree for Selection-Sort. Selection-Sort (A) n A. len
ID: 3854170 • Letter: #
Question
(10 points) Basic. Decision tree for Selection-Sort. Selection-Sort (A) n A. length For j 1 to n -1 Do smallest i For i j 1 to n Do if A Oil A[smallest] en smallest i Exchange A [j] with A Dsmallest] (a) (6 points) Draw the decision tree corresponding to Selection-Sort when running on an array of n 3 elements A 3 a1, a2, a3) as in fig. 8.1 (keep "S" on the left child and on the right child). (b) (2 points) Mark the execution path followed for the array A (6,4, 2), as in fig. 8.1. (c) (2 points For the tree you drew, how many leaves does it have? Compare this with n! and comment on the result.Explanation / Answer
Selection Sort general code (for n elements)
-------------------------------------------------------------------------------------------------------
public class MySelectionSort {
public static int[] doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
public static void main(String a[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.