Skip to content

Commit 7496870

Browse files
Queue (#70)
* Create RandomizedQuick_Sort.md * Create Queues.md * Update Queues.md
1 parent f9e07aa commit 7496870

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Queues/Queues.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# What are queues?
2+
3+
Queues are lists where insertion is done at the end while the deletion is done at the front end. It follows the first in first out order(FIFO).
4+
5+
# Operations on queue
6+
7+
1. Enqueue- Inserts the element at the rear end of the list.
8+
2. Dequeue- Deletes an element from the beginning of the list.
9+
10+
# Priority Queue
11+
12+
Priority Queue is a queue where the elements of it have an associated priority with it.
13+
Every item has a priority associated with it.
14+
An element with high priority is dequeued before an element with low priority.
15+
If two elements have the same priority, they are served according to their order in the queue.
16+
17+
# Types of Priority Queue
18+
19+
1. Ascending Queue- In ascending order priority queue, the element with a lower priority value is given a higher priority in the priority list.
20+
21+
2. Descending Queue- The root node is the maximum element in a max heap, as you may know. It will also remove the element with the highest priority first.
22+
23+
# Operations in a Priority Queue
24+
25+
1. Insertion- When the element is inserted in a Priority Queue it is checked if the element is in the correct order or not. If not, elements are swapped.
26+
2. Deletion- Maximum element from the Max Heap is deleted.
27+
3. Peek- Returns the maximum element from Max Heap or the minimum element from Min Heap.
28+
29+
# Circular Queue
30+
31+
Circular Queue is a special version of queue where the last element of the queue is connected to the first element of the queue forming a circle.
32+
33+
# Operations on Circular Queue
34+
35+
1. Front- Get the front item from queue.
36+
2. Rear- Get the last item from queue.
37+
3. Enqueue- This function is used to insert an element into the circular queue at rear position.
38+
4. Dequeue- This function is used to delete an element from the circular queue from fron position.
39+
40+
Ref. Taken- GeeksForGeeks

Sorting/RandomizedQuick_Sort.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## Description
2+
3+
In QuickSort we first partition the array in place such that all elements to the left of the pivot element are smaller, while all elements to the right of the pivot are greater than the pivot. Then we recursively call the same procedure for left and right subarrays. Using a randomly generated pivot we can further improve the time complexity of QuickSort.
4+
5+
# Code
6+
7+
```cpp
8+
#include <cstdlib>
9+
#include <time.h>
10+
#include <iostream>
11+
using namespace std;
12+
13+
int partition(int arr[], int low, int high)
14+
{
15+
// pivot
16+
int pivot = arr[high];
17+
18+
// Index of smaller element
19+
int i = (low - 1);
20+
21+
for (int j = low; j <= high - 1; j++)
22+
{
23+
// If current element is smaller
24+
// than or equal to pivot
25+
if (arr[j] <= pivot) {
26+
27+
// increment index of
28+
// smaller element
29+
i++;
30+
swap(arr[i], arr[j]);
31+
}
32+
}
33+
swap(arr[i + 1], arr[high]);
34+
return (i + 1);
35+
}
36+
37+
// Generates Random Pivot, swaps pivot with
38+
// end element and calls the partition function
39+
int partition_r(int arr[], int low, int high)
40+
{
41+
// Generate a random number in between
42+
// low .. high
43+
srand(time(NULL));
44+
int random = low + rand() % (high - low);
45+
46+
// Swap A[random] with A[high]
47+
swap(arr[random], arr[high]);
48+
49+
return partition(arr, low, high);
50+
}
51+
52+
/* The main function that implements
53+
QuickSort
54+
arr[] --> Array to be sorted,
55+
low --> Starting index,
56+
high --> Ending index */
57+
void quickSort(int arr[], int low, int high)
58+
{
59+
if (low < high) {
60+
61+
/* pi is partitioning index,
62+
arr[p] is now
63+
at right place */
64+
int pi = partition_r(arr, low, high);
65+
66+
// Separately sort elements before
67+
// partition and after partition
68+
quickSort(arr, low, pi - 1);
69+
quickSort(arr, pi + 1, high);
70+
}
71+
}
72+
73+
/* Function to print an array */
74+
void printArray(int arr[], int size)
75+
{
76+
int i;
77+
for (i = 0; i < size; i++)
78+
cout<<arr[i]<<" ";
79+
}
80+
81+
// Driver Code
82+
int main()
83+
{
84+
int arr[] = { 10, 7, 8, 9, 1, 5 };
85+
int n = sizeof(arr) / sizeof(arr[0]);
86+
87+
quickSort(arr, 0, n - 1);
88+
printf("Sorted array: \n");
89+
printArray(arr, n);
90+
91+
return 0;
92+
}
93+
```
94+
95+
## Run Code
96+
https://ide.geeksforgeeks.org/bd2d6f18-3d4a-4ca0-a0a8-492c16d40c7e
97+
98+
## Complexities
99+
### Time complexity :
100+
Best Case : O(nlogn)
101+
Worst Case : O(n^2)
102+
### Space complexity :
103+
O(N)

0 commit comments

Comments
 (0)