Hello, I need answers for those questions JAVA : Write the 6 standard methods ev
ID: 3812172 • Letter: H
Question
Hello, I need answers for those questions JAVA :
Write the 6 standard methods every self-respecting class should have for the Song class. Here’s the driver class:
public class SongTester
{
public static void main(String args[])
{
private Song[] songs;
songs = new Song[3];
songs[0] = new Song(); //defaults to: “Untitled” for name of song, and
// “Undetermined” for name of artist…
songs[1] = new Song(“I Can’t Stop Loving You”, “Ray Charles”);
songs[2] = new Song(“Daylight”, “Matt & Kim”);
System.out.println(songs[0]);
SortSearchUtil.insertionSort(songs);
System.out.println(“After sorting”);
System.out.println(songs[0]); // Is the first element the right value?
if (SortSearchUtil.linearSearch(songs, “Chain Gang”))
{
System.out.println(“Already on file.”);
}
else
{
System.out.println(“Not on file.”);
}
}// end method
}//end class SongTester
• Which is more efficient for an array of elements:
insertionSort or selectionSort..? Why?
linearSearch or binarySearch…? Why?
• What condition must be met for binarySearch to work?
• What is the time-complexity formula for binarySearch?
Thank you.
Explanation / Answer
1. insertion sort or selection sort: more efficient is insertion sort.
selection sort always has the complexity of O(n^2) in all cases of complexity. while insertion sort has the complexity of O(n) when all elements are sorted that is the best case of insertion sort.
2.Linear search or binary search:
Linear search has the complexity of O(N) in worst and average case.
Binary search has the complexity of O(log N) in worst and average case.But binary search work with sorted list only.
where N is no of elements in we have to search element.
so binary search has less complexity as compared to linear search.
3. For binary search to work, all elements should be given in sorted form as input.
4. Binary search has the complexity of O(log N)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.