Computer Science - Queue and Stack Operations, Bubble Sort and Selection Sort
Verified
Added on 2023/06/10
|6
|584
|352
AI Summary
This article covers Queue and Stack Operations, Bubble Sort and Selection Sort in Computer Science. It explains the worst case complexity of each operation and provides examples of Bubble Sort and Selection Sort.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Running head: COMPUTER SCIENCE Computer Science Name of the Student: Name of the University: Author note:
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
1 COMPUTER SCIENCE Question 1 Answer A public void printLocations(Queue locations){ String str; while(locations.isEmpty()==false){ str = locations.dequeue(); System.out.println(str); } } Worst Case complexity: O(n) Answer B public void queueToStack(Queue queue, Stack stack){ String str; while(queue.isEmpty()==false){ str = queue.dequeue(); stack.push(str); } } Worst Case complexity: O(n) Answer C public void stackToQueue (Queue queue, Stack stack){ String str; while(stack.isEmpty()==false){ str = stack.pop(); queue.enqueue(str); } } Worst Case complexity: O(n)
2 COMPUTER SCIENCE Answer E public void reverse(Queue locations){ Stack stack = new Stack(); String str; while(queue.isEmpty()==false){ str = queue.dequeue(); stack.push(str); } while(stack.isEmpty()==false){ str = stack.pop(); queue.enqueue(str); } } Considering that the queue consists of the following data : Original Queue: jungle trail waterfall beach cave Creating the stack from the queue with FIFO technique… Stack: jungle trail waterfall beach cave
3 COMPUTER SCIENCE The queue is emptied from the front that is “cave” and then this value is inserted into the stack. Then “beach” is taken out from the queue and put into stack. Similarly, the queue is dequeued from the front and the stack is filled. In this situation, the stack looks exactly like the queue was at the beginning. Filling the queue by removing elements from the stack following LIFO technique… Queue: cave beach waterfall jungle trail Once the stack has been filled, the stack is now emptied one from the top and the values are enqueued into the queue. The new formed queue is reversed as the stack follows a LIFO technique and it gives out elements from its top instead of its front and these elements are put in as the queue’s front. This finally creates the final queue as shown above, which is reversed from the original. Worst Case complexity: O(n) Question 2 Bubble Sort Sequence 1 Iteration 1:21 19 6 40
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.