Question: a) Explain each line of code of the three codes b) Explain what the co
ID: 3886720 • Letter: Q
Question
Question:
a) Explain each line of code of the three codes
b) Explain what the codes do overall, like the main purpose of them
R LinkedStackotints.java > No S. 88 main(String[] args) 1nversion.javajGclassinversion public class Inversion mport java.util.Randon; import java.util.Scanner public class LinkedStackofInts f public long bruteForceInversionCount (LinkedStackofInts list) public class Driver i public int sizee public Node first = null; public boolean isEmpty) long count0; LinkedStackofints.Node a = list·first; LinkedStackofInts.Node bi public static void printList LinkedStackofInts list) LinkedStack0f Int s . Node temp while (temp != null) { = list, first; System.out.print(temp.value , temp = temp.next; while (anull) return first null; b = a.next; while (bnull) if (a.value > b.value) count++; b = b.next; public void push(int value) public static LinkedStackOfInts filllist(int n) i Node oldFirst = first; first- new Node); first.valuevalue first.next oldFirst; size++ LinkedStackOfInts list- new LinkedStack0fInts) for (int i = e; iExplanation / Answer
Solution:
We'll start from left most to right most one by one.
This code snippet contains 2 classes and a method
Class LinkedStackOfInts contains a data member size initialized with zero and a node
one method named push is available here who is creating a node named oldFirst and then initializing it with the argument which the method received as a parameter.
one more class named Node is there defining a node of a linked list.
Driver.java is main body of the code which contains main method as well.
a class named driver is here where a method printList is there which is used to print a linked list
In filllList method, 100 random number has been pushed into the list.
main method first created an object of Bubble sort class and then an object of Inversion class then the value of n is entered
the unsorted numbers are displayed on the screen
then the method brutrForceInversionCount(Third code snippet) is called which is number of inversions required by brute-force method then after returning to the main method bubbleSort is called to sort the list and then sorted numbers are displayed on the screen along with the number of swaps performed by bubble sort.
b)
The code is comparing the number of inversions performed to sort a random list of 100 numbers between brute-force approach and the bubble sort method.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.