:: DATA STRUCTURE :: solve these problems according to data structure course. it
ID: 3861370 • Letter: #
Question
:: DATA STRUCTURE ::
solve these problems according to data structure course.
its preferred to use JAVA language in coding if needed!!!
use simple and short methods and give the answers its better to be by KEYBOARD not hand-written answers.
make the answers clear in both questions.
2. Determine the worst-case complexity of the following program void transpose (int al llMAX SIZE] int ii, temp; for i 0; i MAX SIZE-1, i++) for J i 1; j MAX SIZE; j++) SWAP (a[i][jkalj][il,temp); 3. Please write down the code to do selection sort a) Analysis the worst-case complexity of the selection sort b) Write down the code to do the performance measurement of the selection sortExplanation / Answer
Q2 worst case complexity of the code is o(n2)
since there are two nested loops and complexity if the 2 nested loops is o(n2)
where n is Max_size
Q3
selection sort code
public class SelectionSort {
public static void PerformselectionSort(int[] a){
for (int i = 0; i < a.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < a.length; j++){
if (a[j] < a[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = a[index];
a[index] = a[i];
a[i] = smallerNumber;
}
}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
PerformselectionSort(arr1);
System.out.println("After Selection Sort the final array is ");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
a) worst case complexity is o(n2) since there are two nested loops
Selection sort is not an adaptive sorting algorithm. i.e It performs the same number of comparisons in its best case, average case and worst case because it did not get use of any existing order in the input elements
b) to check the performance please find the code
package package1;
public class SelectionSort {
public static void PerformselectionSort(int[] a){
for (int i = 0; i < a.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < a.length; j++){
if (a[j] < a[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = a[index];
a[index] = a[i];
a[i] = smallerNumber;
}
}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22,122,3,4,5,6,7,8,88,5,55,4,3,32,4,54,78,9,76,4,231,113,6,57,9,9,8,43,4,2,32,3,2,3,23,2};
System.out.println();
long startTime = System.currentTimeMillis();
System.out.println("sorting starts at "+startTime);
PerformselectionSort(arr1);
long stopTime = System.currentTimeMillis();
System.out.println("sorting ends at "+stopTime);
long elapsedTime = stopTime - startTime;
System.out.println("time to sort "+arr1.length +" elemets "+elapsedTime);
}
}
just change the array size and elemensts and you will see the time selesction sort has taken to sort the array elements
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.