Question One
A)What is the Output of the following Linear (Sequential) Search program? It searches the given array for the number 52. [2.5 mark points]
B)Write the one (or more) line of code that you would change in this program, so it will only output the first occurrence of the number 52 and then stops. [2.5 mark points]
int n = 52;
System.out.println("Searching for number: " + n );
boolean found = false;
int i = 0;
while (!found || i < a.length )
**
if (a[i] == n)
**
System.out.println(n + " is Found at index position " + i);
found = true;
**
i++;
**
if ( !found)
System.out.println(n + " was Not Found!");
**
**
Question Two
Part A) Calculate the Big Oh performance for the Sequential Search on the array given below that has 15 elements? Given that sequential search of an array of 10 elements took on average 1 second. [2.5 mark points]
[ 99, 101, 8, -22, 3, 55, 42, -1, 0, 52, 33, -4, 66, 1, 52]
Part B) Calculate the Big Oh performance for the Binary Search on the sorted array given below that has 15 elements? Given that Binary search of an array of 10 elements took on average 1 second. [2.5 mark points]
[ -22, -4, -1, 0, 1, 3, 8, 22, 42, 52, 52, 55, 66, 99, 101]
Question Three
Write a complete program that creates two stacks with the following element:
s1 = {10, 3, 1, 5**
s2 = {7, 9, 2, 8, 12, 4**
Use the pop method to extract the top element from each stack and print the summation of these two elements. Finally, print the remaining elements from both stacks, indicate if empty or show what is left in the stack.
Sample Output:
Question Four
Part (A) Draw a diagram of a linked list that contains the following city nodes: “Jeddah”, “Mecca” and “Dammam”. Include an instance variable (pointer) to indicate the beginning of the list. In addition, for each element, clearly show its fields, contents and the references. [2.5 mark points]
Part B) List three differences between Linked List structure and Array structure? Include the differences of: 1. How we add/remove elements in each. 2. Differences in structure itself 3. How each access its elements. [2.5 mark points]
NOTE: You can use MS Word or any diagraming tool to draw. Or simply “hand” drawing. Then, take picture snapshot and paste your diagram here.
Question Five
Using a LinkedList and ListIterator classes from the java library packages. Write a program that contains a list of courses’ codes. Your program should do the following functions in order.
1.Add the following courses in the list: “CS141”, “CS140” and “IT409”.
2.Remove the course “CS141” from the list and return it in “removed” variable.
3.Add the course “IT448” after the course “CS140”.
4.Prints all the courses in the list using ListIterator object.
الأعضاء الذين قالوا شكراً لـ Malak ammar على المشاركة المفيدة:
جواب السؤال الاول
a-
[99, 101, 8, -22, 3, 55, 42, -1, 0, 52, 33, -4, 66, 1, 52]
Searching for number: 52
52 is Found at index position 9
52 is Found at index position 14
b-
if (a[i] == n)
**
System.out.println(n + " is Found at index position " + i);
found = true;
break;
**
جواب السؤال الاول
a-
[99, 101, 8, -22, 3, 55, 42, -1, 0, 52, 33, -4, 66, 1, 52]
searching for number: 52
52 is found at index position 9
52 is found at index position 14
b-
if (a[i] == n)
**
system.out.println(n + " is found at index position " + i);
found = true;
break;
**
Question Two
Part A) Calculate the Big Oh performance for the Sequential Search on the array given below that has 15 elements? Given that sequential search of an array of 10 elements took on average 1 second. [2.5 mark points]
[ 99, 101, 8, -22, 3, 55, 42, -1, 0, 52, 33, -4, 66, 1, 52]
15 / 10 = 1.5 times
So O(n)=1.5*1=1.5 second Part B) Calculate the Big Oh performance for the Binary Search on the sorted array given below that has 15 elements? Given that Binary search of an array of 10 elements took on average 1 second. [2.5 mark points]
[ -22, -4, -1, 0, 1, 3, 8, 22, 42, 52, 52, 55, 66, 99, 101]
log(15 )/ log(10) = 1.176 times
So O(log n)=1.761*1=1.176 second
question two
part a) calculate the big oh performance for the sequential search on the array given below that has 15 elements? Given that sequential search of an array of 10 elements took on average 1 second. [2.5 mark points]
[ 99, 101, 8, -22, 3, 55, 42, -1, 0, 52, 33, -4, 66, 1, 52]
15 / 10 = 1.5 times
so o(n)=1.5*1=1.5 second part b) calculate the big oh performance for the binary search on the sorted array given below that has 15 elements? Given that binary search of an array of 10 elements took on average 1 second. [2.5 mark points]
[ -22, -4, -1, 0, 1, 3, 8, 22, 42, 52, 52, 55, 66, 99, 101]
log(15 )/ log(10) = 1.176 times
so o(log n)=1.761*1=1.176 second
مرحبا ... هل ممكن تقول لي من وين حصلت على هذه الاجابة ؟
يعني هل في أمثله طبقتها مشابهه لهذا المثال ؟
شكرا لك مقدما
التوقيع
اللهم صل على محمد وآل محمد
اللهم اجعلنا من العلماء الصالحين في الدنيا والاخرة
العلم يحرسك وأنت تحرس المال , العلم يزكو على الإنفاق والمال تنقصه النفقة العلم حاكم والمال محكوم عليه’ ومحبة العلم دين يدان بها , العلم يكسب العالم الطاعة في حياته
مرحبا ... هل ممكن تقول لي من وين حصلت على هذه الاجابة ؟
يعني هل في أمثله طبقتها مشابهه لهذا المثال ؟
شكرا لك مقدما
Big O(N) for sequential search on the array
we use O(n) to find the time in sec, where "n" is the number of elements
Big O(n) for Binary search on sorted array
we use log(n) to find the time in sec, where "n" is also the number of the elements
: by data was given in this Q we'll find
n = 15 , n1=10
t= ? , t1=1
and you can find in attached how to calculate Big O notation