Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Queue and Linear Search Algorithm.

Queue is a linear data structure that orders items in a first-in, first-out manner. It can be used for buffering between processes with different speeds, breadth-first search algorithms, handling web server requests to avoid overloading, simulating real-world queues like at stores, communicating between threads, scheduling tasks in a specific order, and handling events that have occurred but not yet been processed.

Uploaded by

Samrawit Dawit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Queue and Linear Search Algorithm.

Queue is a linear data structure that orders items in a first-in, first-out manner. It can be used for buffering between processes with different speeds, breadth-first search algorithms, handling web server requests to avoid overloading, simulating real-world queues like at stores, communicating between threads, scheduling tasks in a specific order, and handling events that have occurred but not yet been processed.

Uploaded by

Samrawit Dawit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Application of queue

Queue is a linear data structure containing ordered sequence. We insert data at one end of the
queue and remove data from the other end. It uses first in first out principle.

Some of its applications are listed below:

1. Buffering: It can be used to buffer data between two processes that operate at different
speeds.
For example:
 Message buffer stores the message in the order they arrive and process them one by
one.
 A printer uses queue to temporarily store the data until it is ready to print. This
process may produce data faster than a printer can print.
2. Breadth-first search: It can be used to implement a breadth-first search algorithm. The
nodes will be explored in layers, with nodes in the same layer being explored before nodes
in the next layer. It uses queue to find the shortest path in the graph. A queue stores the
nodes in each layer, with the nodes being explored in the order they were added to the
queue.
3. Web server request handling: It can be used to avoid heavy traffic web situations. It can be
used in order to handle incoming request to a web server. When a request arrives it is
added to a queue and the server process in the order they were received. This ensures that
no requests are lost and that the server is not overwhelmed with too many requests at once.
One of the most common website traffic management solutions is by implementing a virtual
HTTP request queue.
4. Simulation: it can be used to simulate real world scenarios.
For example:
 A queue can be used to model line of customers waiting to be used to model a line of
customers waiting to be served at store or a bank.
5. Multi-threaded programming: it can be used to communicate between threads in multi-
threaded program.
For example:
 One thread may produce data, while another thread consumes the data. A queue
can be used to pass the data from the producer thread to th consumer thread.
6. Task scheduling: it is an ideal data structure for task scheduling which lets you perform all
the tasks in order. Queue can be used to schedule tasks in a specific order. All tasks are
pushed to the end of the queue. We can pick the task at the front of the queue and execute it.
Once you complete the task, it is removed from the queue, and you select the next task. This
process will continue until the queue is empty.
For example:
 A printer queue can be used to prioritize print jobs based on their urgency or
importance.
7. Event handling: In event handling a queue data structure stores all events which have
occurred but are yet to be processed by the system if a new event occurs, it is pushed to the
end of the queue and then process the event form the front of the queue and removes it
once handled.

Linear search
Class searching:
def linear_search(self, arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
import time
def test_search():
arr = [i for i in range(1,000,000)]
x= 999999
searching =Searching ()
srart_time = time.time()
index = searching.linear_search(arr, x)
end_time = time.time()

print (“Time taken: ” end_time - start_time, “seconds” )

test_search()

You might also like