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 sort

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
:: DATA STRUCTURE :: solve these problems according to data structure course. its preferred to use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • How do I write this in the c++ language? 1. (10 points) Write a program to...

    How do I write this in the c++ language? 1. (10 points) Write a program to implement Heapsort. The input should be an array of size at least 15. Have the user enter the values or you can specify your own array (unsorted). The output should be the final sorted array AND print out the values in the max heap (just the heap, NOT the full array) after each MAX-HEAPIFY (after BUILD- MAX-HEAP i.e. don't print output in BUILD-MAX-HEAP) in...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • 2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running...

    2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running time analysis for both best and worst case. 3. Consider the age data of 12 children who are supposed to undergo for vaccination in ascending order of their age. Suggest and apply a sorting technique which can efficiently handle this data. Show all the intermediate steps clearly. Child ID 01 02 03 04 05 06 07 08 09 10 11 12 2. Age 1...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • Define a structure called Date with month, day and year as its only data members, Time...

    Define a structure called Date with month, day and year as its only data members, Time with hour and minute as its only members and Event with desc (a c-string for description of maximum size 80), date (of type Date) and time (of type Time). Write a readEvents() function that will keep asking the user if he or she wants to add a new event. As long as the user answers with a 'y' or 'Y', allocate memory dynamically for...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to...

    Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...

  • I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning...

    I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning the values in the rows go from low to high). I tried to make the sort function but it just puts all the numbers from lowest to highest, up to down in column format. So please please help me fix it so it sorts correctly, I have bolded the code that needs to be fixed, everything else is fine. The code is below: #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT