Question

In: Computer Science

Please Use your keyboard (Don't use handwriting) CS141 I need new and unique answers, please. (Use...

Please Use your keyboard (Don't use handwriting)

CS141

I need new and unique answers, please. (Use your own words, don't copy and paste)

Q1:

Show all the steps of the following data set when it being sorted with Insertion, Selection, and Merge Sort Algorithms. Then, calculate the performance of each one of them.(Big –O Notation)

18

4

26

9

21

15

20

10

Q2:

Write a program using LinkedList and ListIterator to obtain the following statements:

1. Create a linked list named "list" with these elements: "one", "four" "three", and “five.

2. Create a List Iterator named "iter1" related to "list".

3. Add the new element "two" after the element "one"

4. Remove the element "four" and “five”

5 Display all element of the list

6. Create a new List Iterator named "iter2" related to "list", to add new elements in the second, fourth and sixth positions and to create each new element you must use the value of the current element concatenated to " and a half". The new obtained list is: [one, one and a half, two, two and a half, three, three and a half]

Output sample:

[one, four, three, five]

[one, two, three]

[one, one and a half, two, two and a half, three, three and a half]

Q3:

a) Write the java code for doing a Linear Search on the array given below.

{24,2,45,20,56,75,2,56,99,53,12}

b) What would your search method return if we asked to search for the number 2 and return it’s index position?

Solutions

Expert Solution

Q1:

Insertion Sort:

Step 1:

4 18 26 9 21 15 20 10 //swapping 4 from 18

Step 2:

4 18 26 9 21 15 20 10 //remaining the same as 18 is smaller than 26

Step 3:

4 9 18 26 21 15 20 10 //bringing 9 to the front of 18

Step 4:

4 9 18 21 26 15 20 10 //bringing 21 to the front of 26

Step 5:

4 9 15 18 21 26 20 10 //bringing 15 to the front of 18

Step 6:

4 9 15 18 20 21 26 10 //bringing 20 to the front of 21

Step 7:

4 9 10 15 18 20 21 26 //bringing 10 to the front of 15

Thus it takes 7 steps to sort the given list using insertion sort.


Selection Sort:

Step 1:

4 18 26 9 21 15 20 10 //reading the smallest element 4 and placing it ahead of the unsorted list

Step 2:

4 9 26 18 21 15 20 10 //reading the smallest element 9 and placing it ahead of the unsorted list

Step 3:

4 9 10 18 21 15 20 26 //reading the smallest element 10 and placing it ahead of the unsorted list

Step 4:

4 9 10 15 18 26 21 20 //reading the smallest element 15 and placing it ahead of the unsorted list

Step 5:

4 9 10 15 18 26 21 20 //reading the smallest element 18 and placing it ahead of the unsorted list

Step 6:

4 9 10 15 18 20 21 26 //reading the smallest element 20 and placing it ahead of the unsorted list


Thus it takes 6 steps to sort the given list using selection sort.


Merge Sort:


Step 1:

18|4       26|9       21|15       20|10   //Dividing the list

Step 2:

18   4   26   9   21   15   20   10   //Dividing the list more further so that each element is separate

Step 3:

4|18       9|26       15|21   10|20   //Merging and sorting the array using 2 elements at a time

Step 4:

   4|9|18|26           10|15|20|21       //Merging and sorting the array using 4 elements at a time

Step 5:

           4|9|10|15|18|20|21|26           //Merging and sorting the array using all the 8 elements at a time  

Thus it takes 5 steps to sort the given list using merge sort.


Performance:

Insertion Sort:
The performace on the given list of 8 elements is 56.

Selection Sort:
The performace on the given list of 8 elements is 28.

Merge Sort:
The performace on the given list of 8 elements is 15.22.

Q2:

import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
  
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
  
// Use add() method to add elements in the list
list.add("one");
list.add("four");
list.add("three");
list.add("five");
  
// Displaying the linkedlist
System.out.println("LinkedList:" + list);
  
// Setting the ListIterator iter1
ListIterator<String> iter1 = list.listIterator();

   // Adding new Element at 5th Position
   list.add(1, "two");

   // Remove the head using remove()
list.remove("four");
list.remove("five");

   // Displaying the linkedlist
System.out.println("LinkedList:" + list);

   // Setting the ListIterator iter1
ListIterator<String> iter2 = list.listIterator(0);

   //add one and a half to the second element
   list.add(1, iter2.next() + " and a half");
  
   //move the iterator to the 3 position
   iter2.next();
   list.add(3, iter2.next() + " and a half");

   //move the iterator to the 5 position
   iter2.next();
   list.add(3, iter2.next() + " and a half");
          
  
// Iterating through the created list from the position
System.out.println("The list is as follows:");
while(list_Iter.hasNext()){
System.out.print(list_Iter.next());
System.out.print(" , ");
}
}
}


Q3:

a)

public class LinearSearch {
public static void main(String args[]){
int array[] = {24,2,45,20,56,75,2,56,99,53,12};
int size = array.length;
int value = 2;

for (int i=0 ;i< size-1; i++){
if(array[i]==value){
System.out.println("Element found index is :"+ i);
}else{
System.out.println("Element not found");
}
}
}
}


b) If the number 2 is searched the index of this element will be 1 and 6 as there the number 2 occurs two times in the list. However if you want only one index to be shown that can also be done with the help of a flag variable.


Related Solutions

Please Use your keyboard (Don't use handwriting) MGT211 I need new and unique answers, please. (Use...
Please Use your keyboard (Don't use handwriting) MGT211 I need new and unique answers, please. (Use your own words, don't copy and paste) TOPIC: EMPLOYEE HEALTH AND SAFETY IN THE ORGANIZATION Organization officials have a legal and moral responsibility to ensure that the workplace is free from unnecessary hazards. Employers hold responsibility for understanding what is necessary to keep workers safe from harm. Conditions surrounding the workplace must be secure for employee’s physical and mental health. As many organizations have...
Please Use your keyboard (Don't use handwriting) Econ I need new and unique answers, please. (Use...
Please Use your keyboard (Don't use handwriting) Econ I need new and unique answers, please. (Use your own words, don't copy and paste) Q: how supply, demand, and prices affect the auto industry. includes: supply and demand, public spending and the outcome.
Please Use your keyboard (Don't use handwriting) FIN 101 I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) FIN 101 I need new and unique answers, please. (Use your own words, don't copy and paste) Assignment Questions Q1: Carrefour is expecting its new center to generate the following cash flows: Years 0 1 2 3 4 5 Initial Investment ($35,000,000) Net operating cash-flow $6,000,000 $8,000,000 $16,000,000 $20,000,000 $30,000,000 a. Determine the payback for this new center. (1 mark) b. Determine the net present value using a cost of capital of 15...
Please Use your keyboard (Don't use handwriting) BIOL 102 I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) BIOL 102 I need new and unique answers, please. (Use your own words, don't copy and paste) Pllleeeeasssse i need more than 500 words please ...***** Choose any system of the human body (respiratory system) and prepare a response to the following questions in 1-2 pages: 1. Introduction (Explain the system with the components) 2. Body (Explain how the system relates to achieve homeostasis in human body) 3. Conclusion (Choose any disease common...
Please Use your keyboard (Don't use handwriting) MGT 211 I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) MGT 211 I need new and unique answers, please. (Use your own words, don't copy and paste) subject is human resources management Case Study Imagine you’re the VP of Human resources for a Fortunate 100 company. You’ve spent your entire career attempting to enhance the workplace for employees to support their productive work in the organization. While you understand that bottom-line decisions often dominate many of the matters you have to address. You...
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please. (Use your own words, don't copy and paste) IT243 Course name: System Analysis and Design ***********Please i need more details and more Explain********** Q1: What are the roles of a project sponsor and the approval committee during the different SDLC phases? Q2: As the project sponsor, you suggested that your company that runs multiple local supermarkets should provide an online shopping service to increase...
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please. (Use your own words, don't copy and paste) IT243 Course name: System Analysis and Design ***********Please i need more details and more Explain********** Q2: As the project sponsor, you suggested that your company that runs multiple local supermarkets should provide an online shopping service to increase sales during COVID-19 pandemic. Write a system request to propose this project. System request Project Sponsor Business Need...
Please Use your keyboard (Don't use handwriting) PHC 261 I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) PHC 261 I need new and unique answers, please. (Use your own words, don't copy and paste) ( Please i need more than 500 words please) Q. 1. Write essay note on Occupational safety and health protection of health workers from acute respiratory diseases during respiratory diseases outbreaks following subtitle: (Minimum & Maximum 2-3 pages) 1.   Mention the epidemic and pandemic respiratory diseases recommended for protection of health workers 2.   As a health...
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please. (Use your own words, don't copy and paste) HCI 314 Write a page and a half (400-600 words) in an essay style answer to respond to the following question: Disruptive innovation is a driver for change in public health informatics. As a Health Informatics professional which innovation do you think has the greatest impact during COVID-19 pandemic and why? _____ please re -write my...
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please. (Use your own words, don't copy and paste) IT243 Course name: System Analysis and Design Q2: As the project sponsor, you suggested that your company that runs multiple local supermarkets should provide an online shopping service to increase sales during COVID-19 pandemic. Write a system request to propose this project. System request Project Sponsor Business Need Business Requirements Business Value Special Issues or Constraints...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT