Skip to content

Commit 6941565

Browse files
authored
Add files via upload
1 parent 7a02af1 commit 6941565

File tree

16 files changed

+981
-0
lines changed

16 files changed

+981
-0
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# How to do algorithm analysis?
2+
>Before going to case analysis let's revise linear search, As I am going to explain Case analysis using a linear search algorithm.
3+
## What is a linear search?
4+
Linear search is a simple searching algorithm that searches for an element of a list in sequential order. We start at one end and check every element until the desired elements is found.
5+
6+
<a target="_blank" rel="noopener noreferrer" href="#"><img src="https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif" alt="Linear Search Gif" style="max-width:100%;"></a>
7+
8+
## Example:
9+
```python
10+
# Python 3 implementation of the approach
11+
# Linearly search x in arr[]
12+
def search(arr, x):
13+
for index, value in enumerate(arr):
14+
if value == x:
15+
return index
16+
return -1
17+
18+
# Driver Code
19+
arr = [1, 10, 30, 15]
20+
x = 30
21+
print(x, "is present at index",
22+
search(arr, x))
23+
```
24+
><b>Output : 30 is present at index 2</b>
25+
<hr><br>
26+
There are three cases to analyze an algorithm
27+
28+
1. The worst case
29+
2. Average case
30+
3. Best case
31+
32+
## Worst Case Analysis:
33+
34+
For linear search, the worst case happens when we search for an element which is not present in the array. From the above example if the 'X' is not present in the array then search() function will compare it with every element in the array one by one. Therefore the worst case time complexity of linear search would be O(n).
35+
36+
## Average Case Analysis:
37+
38+
The average case analysis we take the average over all inputs(of the given size). For linear search, we will assume that all cases are uniformly distributed (includes the key(x) not present in the array). We add all the cases and divide the sum by (n+1).
39+
40+
41+
Average Case Time =
42+
<a target="_blank" rel="noopener noreferrer" href="#"><img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/analysis1.png" alt="Linear Search Gif" style="max-width:100%;"></a>
43+
= <a target="_blank" rel="noopener noreferrer" href="#"><img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/analysis2.png" alt="Linear Search Gif" style="max-width:100%;"></a>
44+
= Θ(n)
45+
46+
This analysis may not be possible for every algorithm. So, rarely we do it. Mostly for the average case, the time complexity we get is similar to the worst case.
47+
48+
## Best Case Analysis:
49+
In a linear search, the best case occurs when the key(x) is present at the first index. The no of operations in the best case is constant(1) it doesn't depend on the size of the array. So the time complexity in the best case would be Θ(1)
50+
51+
> ## Bonus
52+
### Here's the list of other algorithms
53+
<a target="_blank" rel="noopener noreferrer" href="#"><img src="https://www.researchgate.net/profile/Jehad_Hammad3/publication/274640372/figure/tbl1/AS:391828730859523@1470430657128/Summary-of-the-best-case-average-case-and-worst-case2.png" alt="Linear Search Gif" style="max-width:100%;"></a>
54+
55+
## Contributed by <a href="https://github.com/ShyamKumar1">Shyam Kumar</a> With 💜.
56+
57+
## Reach me on
58+
<p align='center'>
59+
<a href="https://www.linkedin.com/in/shyam-kumar-9b9841157/"><img src="https://img.shields.io/badge/linkedin-%230077B5.svg?&style=for-the-badge&logo=linkedin&logoColor=white" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
60+
<a href="https://www.instagram.com/_smiling_storm_/" target="_blank"><img src="https://img.shields.io/badge/Instagram-%23E4405F.svg?&style=for-the-badge&logo=instagram&logoColor=white" alt="Instagram"></a>&nbsp;&nbsp;&nbsp;&nbsp;
61+
<a href="mailto:[email protected]?subject=Olá%20Punit"><img src="https://img.shields.io/badge/gmail-%23D14836.svg?&style=for-the-badge&logo=gmail&logoColor=white" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
62+
<a href="https://www.facebook.com/shyam.george15/" target="_blank"><img src="https://img.shields.io/badge/Facebook-%231877F2.svg?&style=for-the-badge&logo=facebook&logoColor=white" alt="Facebook"></a>&nbsp;&nbsp;&nbsp;&nbsp;
63+
</p>
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Why do we need analysis of algorithm?
2+
<p>Analysis of an algorithm is important in practice because the accidental or unintentional use of an inefficient algorithm can affect the system performance. In time-sensitive applications, an algorithm that takes too long to run could give its result outdated or useless. To check the characteristics of the algorithm in order to know wheater the algorithm is suitable for the application or not we do an analysis of an algorithm.</p>

FIFO/How.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<p><b>First in, First out</b> system of approach is used in :-</p>
2+
3+
1. Data Structures:<br/>
4+
There are data structures like queue and other variants of queue where we use FIFO approach of processing the data.
5+
2. Disk Scheduling Algorithms:<br/>
6+
Disk controllers use FIFo as a disk scheduling algorithm for ordering dist I/O Requests.
7+
3. Communications and Networking:<br/>
8+
FIFO system is used in communication network bridges, switches and routers in computer networks to hold data packets enroute to their next destination.
9+
10+
Program example foe FIFO implementation in Queue:
11+
12+
``` java
13+
14+
// Java program to demonstrate
15+
// working of FIFO
16+
// using Queue interface in Java
17+
18+
import java.util.LinkedList;
19+
import java.util.Queue;
20+
21+
public class QueueExample {
22+
public static void main(String[] args)
23+
{
24+
Queue<Integer> q = new LinkedList<>();
25+
26+
// Adds elements {0, 1, 2, 3, 4} to queue
27+
for (int i = 0; i < 5; i++)
28+
q.add(i);
29+
30+
// Display contents of the queue.
31+
System.out.println("Elements of queue-" + q);
32+
33+
// To remove the head of queue.
34+
// In this the oldest element '0' will be removed
35+
int removedele = q.remove();
36+
System.out.println("removed element-" + removedele);
37+
38+
System.out.println(q);
39+
40+
// To view the head of queue
41+
int head = q.peek();
42+
System.out.println("head of queue-" + head);
43+
44+
// Rest all methods of collection interface,
45+
// Like size and contains can be used with this
46+
// implementation.
47+
int size = q.size();
48+
System.out.println("Size of queue-" + size);
49+
}
50+
}
51+
```
52+
53+
```
54+
Output:
55+
56+
Elements of queue-[0, 1, 2, 3, 4]
57+
removed element-0
58+
[1, 2, 3, 4]
59+
head of queue-1
60+
Size of queue-4
61+
```
62+
63+
64+
<hr>
65+
66+
Contributed by <a href="https://github.com/ShyamKumar1">Shyam Kumar</a> With 💜.
67+
68+
Reach me on
69+
<p align='center'>
70+
<a href="https://www.linkedin.com/in/shyam-kumar-9b9841157/"><img src="https://img.shields.io/badge/linkedin-%230077B5.svg?&style=for-the-badge&logo=linkedin&logoColor=white" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
71+
<a href="https://www.instagram.com/_smiling_storm_/" target="_blank"><img src="https://img.shields.io/badge/Instagram-%23E4405F.svg?&style=for-the-badge&logo=instagram&logoColor=white" alt="Instagram"></a>&nbsp;&nbsp;&nbsp;&nbsp;
72+
<a href="mailto:[email protected]?subject=Olá%20Punit"><img src="https://img.shields.io/badge/gmail-%23D14836.svg?&style=for-the-badge&logo=gmail&logoColor=white" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
73+
<a href="https://www.facebook.com/shyam.george15/" target="_blank"><img src="https://img.shields.io/badge/Facebook-%231877F2.svg?&style=for-the-badge&logo=facebook&logoColor=white" alt="Facebook"></a>&nbsp;&nbsp;&nbsp;&nbsp;
74+
</p>

FIFO/WhyAndWhat.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<h1 align="center">FIFO Applications</h1>
2+
<b> What is FIFO?</b>
3+
<p></p>
4+
<p><b>FIFO</b> stands for <b>First In, First Out</b>. It is a way of processing and retriving the data. In FIFO system the first element is processed or servered first and the element which comes next to first element will be processed next.</p>
5+
6+
Real Life Example:
7+
8+
<img src="https://media.geeksforgeeks.org/wp-content/uploads/FIFO.jpg">
9+
10+
* In a ticket counter people take their ticket and go in an organized manner
11+
* In the line (Queue) at the ticket counter the first person get's the ticket first.
12+
* The person next to the first person will get the ticket next.
13+
* In this manner, the person who enter the queue last will get ticket at last.
14+
15+
This is known as FIFO System.
16+
17+
18+
<hr>
19+
20+
Contributed by <a href="https://github.com/ShyamKumar1">Shyam Kumar</a> With 💜.
21+
22+
Reach me on
23+
<p align='center'>
24+
<a href="https://www.linkedin.com/in/shyam-kumar-9b9841157/"><img src="https://img.shields.io/badge/linkedin-%230077B5.svg?&style=for-the-badge&logo=linkedin&logoColor=white" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
25+
<a href="https://www.instagram.com/_smiling_storm_/" target="_blank"><img src="https://img.shields.io/badge/Instagram-%23E4405F.svg?&style=for-the-badge&logo=instagram&logoColor=white" alt="Instagram"></a>&nbsp;&nbsp;&nbsp;&nbsp;
26+
<a href="mailto:[email protected]?subject=Olá%20Punit"><img src="https://img.shields.io/badge/gmail-%23D14836.svg?&style=for-the-badge&logo=gmail&logoColor=white" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
27+
<a href="https://www.facebook.com/shyam.george15/" target="_blank"><img src="https://img.shields.io/badge/Facebook-%231877F2.svg?&style=for-the-badge&logo=facebook&logoColor=white" alt="Facebook"></a>&nbsp;&nbsp;&nbsp;&nbsp;
28+
</p>

0 commit comments

Comments
 (0)