Difference between a Static Queue and a Singly Linked List
Last Updated :
08 Jul, 2024
Static Queue: A queue is an ordered list of elements. It always works in first in first out(FIFO) fashion. All the elements get inserted at the REAR and removed from the FRONT of the queue. In implementation of the static Queue, an array will be used so all operation of queue are index based which makes it faster for all operations except deletion because deletion requires shifting of all the remaining elements to the front by one position.
A Static Queue is a queue of fixed size implemented using array.


Singly Linked List: A linked list is also an ordered list of elements. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list. Each node in the list stores the content and a pointer or reference to the next node in the list. To store a single linked list, only the reference or pointer to the first node in that list must be stored. The last node in a single linked list points to nothing (or null).

Here are some of the major differences between a Static Queue and a Singly Linked List
Static Queue | Singly Linked List |
---|
Queue is a collection of one or more elements arranged in memory in a contiguous fashion. | A linked list is a collection of one or more elements arranged in memory in a dis-contiguous fashion. |
---|
Static Queue is always fixed size. | List size is never fixed. |
---|
In Queue, only one and single type of information is stored because static Queue implementation is through Array. | List also stored the address for the next node along with it's content. |
---|
Static Queue is index based. | Singly linked list is reference based. |
---|
Insertion can always be performed on a single end called REAR and deletion on the other end called FRONT. | Insertion as well as deletion can performed any where within the list. |
---|
Queue is always based on FIFO. | List may be based on FIFO or LIFO etc. |
---|
Queue have two pointer FRONT and REAR. | While List has only one pointer basically called HEAD. |
---|
Below is the implementation of a Static Queue:
C++
#include <iostream>
using namespace std;
class Queue {
private:
static int front, rear, capacity;
static int* queue;
public:
Queue(int c) {
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
// function to insert an element
// at the rear of the queue
static void queueEnqueue(int data) {
// check queue is full or not
if (capacity == rear) {
cout << "\nQueue is full\n";
return;
}
// insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
// function to delete an element
// from the front of the queue
static void queueDequeue() {
// if queue is empty
if (front == rear) {
cout << "\nQueue is empty\n";
return;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return;
}
// print queue elements
static void queueDisplay() {
int i;
if (front == rear) {
cout << "\nQueue is Empty\n";
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
cout << " " << queue[i] << " <-- ";
}
return;
}
// print front of queue
static void queueFront() {
if (front == rear) {
cout << "\nQueue is Empty\n";
return;
}
cout << "\nFront Element is: " << queue[front];
return;
}
};
int Queue::front;
int Queue::rear;
int Queue::capacity;
int* Queue::queue;
int main() {
// Create a queue of capacity 4
Queue q(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
cout << "\n\nafter two node deletion\n\n";
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
return 0;
}
Java
// Java program to implement a queue using an array
class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
// function to insert an element
// at the rear of the queue
static void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
}
// insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
// function to delete an element
// from the front of the queue
static void queueDequeue()
{
// if queue is empty
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return;
}
// print queue elements
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
System.out.printf(" %d <-- ", queue[i]);
}
return;
}
// print front of queue
static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
System.out.printf("\nFront Element is: %d", queue[front]);
return;
}
}
public class StaticQueueinjava {
// Driver code
public static void main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\n\nafter two node deletion\n\n");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
}
Python
# Python program for the above approach
class Queue:
def __init__(self, c):
self.front = self.rear = 0
self.capacity = c
self.q = [0] * self.capacity
# function to insert an element
# at the rear of the queue
def queueEnqueue(self, data):
# check queue is full or not
if self.capacity == self.rear:
print("\nQueue is full")
return
# insert element at the rear
else:
self.q[self.rear] = data
self.rear += 1
return
# function to delete an element
# from the front of the queue
def queueDequeue(self):
# if queue is empty
if self.front == self.rear:
print("\nQueue is empty\n")
return
# shift all the elements from index 2 till rear
# to the right by one
else:
for i in range(self.rear - 1):
self.q[i] = self.q[i + 1]
# store 0 at rear indicating there's no element
if self.rear < self.capacity:
self.q[self.rear] = 0
# decrement rear
self.rear -= 1
return
# print queue elements
def queueDisplay(self):
i = 0
if self.front == self.rear:
print("\nQueue is Empty")
return
# traverse front to rear and print elements
for i in range(self.front, self.rear):
print(f" {self.q[i]} <-- ", end="")
return
# print front of queue
def queueFront(self):
if self.front == self.rear:
print("\nQueue is Empty\n")
return
print("\nFront Element is:", self.q[self.front])
return
# Create a queue of capacity 4
q = Queue(4)
# print Queue elements
q.queueDisplay()
# inserting elements in the queue
q.queueEnqueue(20)
q.queueEnqueue(30)
q.queueEnqueue(40)
q.queueEnqueue(50)
# Print the queue elements
q.queueDisplay()
# Insert the element in queue
q.queueEnqueue(60)
# Print the queue elements
q.queueDisplay()
q.queueDequeue()
q.queueDequeue()
print("\n\nafter two node deletion\n\n")
# Print queue elements
q.queueDisplay()
# Print front of the queue
q.queueFront()
# This code is contributed by princekumaras
C#
// C# program to implement a queue using an array
using System;
public class Queue
{
private static int front, rear, capacity;
private static int []queue;
public Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
// function to insert an element
// at the rear of the queue
public void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear)
{
Console.Write("\nQueue is full\n");
return;
}
// insert element at the rear
else
{
queue[rear] = data;
rear++;
}
return;
}
// function to delete an element
// from the front of the queue
public void queueDequeue()
{
// if queue is empty
if (front == rear)
{
Console.Write("\nQueue is empty\n");
return;
}
// shift all the elements from index 2 till rear
// to the right by one
else
{
for (int i = 0; i < rear - 1; i++)
{
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return;
}
// print queue elements
public void queueDisplay()
{
int i;
if (front == rear)
{
Console.Write("\nQueue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++)
{
Console.Write(" {0} <-- ", queue[i]);
}
return;
}
// print front of queue
public void queueFront()
{
if (front == rear)
{
Console.Write("\nQueue is Empty\n");
return;
}
Console.Write("\nFront Element is: {0}", queue[front]);
return;
}
}
public class StaticQueueinjava
{
// Driver code
public static void Main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
Console.Write("\n\nafter two node deletion\n\n");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
//javascript code addition
class queue {
constructor(c) {
this.front = this.rear = 0;
this.capacity = c;
this.q = new Array(this.capacity);
}
// function to insert an element
// at the rear of the queue
queueEnqueue(data) {
// check queue is full or not
if (this.capacity == this.rear) {
process.stdout.write("\nQueue is full\n");
return;
}
// insert element at the rear
else {
this.q[this.rear] = data;
this.rear++;
}
return;
}
// function to delete an element
// from the front of the queue
queueDequeue() {
// if queue is empty
if (this.front == this.rear) {
process.stdout.write("\nQueue is empty\n");
return;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for (let i = 0; i < this.rear - 1; i++) {
this.q[i] = this.q[i + 1];
}
// store 0 at rear indicating there's no element
if (this.rear < this.capacity)
this.q[this.rear] = 0;
// decrement rear
this.rear--;
}
return;
}
// print queue elements
queueDisplay() {
let i = 0;
if (this.front == this.rear) {
process.stdout.write("\nQueue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = this.front; i < this.rear; i++) {
process.stdout.write(" " + this.q[i] + " <-- ");
}
return;
}
// print front of queue
queueFront() {
if (this.front == this.rear) {
process.stdout.write("\nQueue is Empty\n");
return;
}
process.stdout.write("\nFront Element is: " + this.q[this.front]);
return;
}
};
// int Queue::front;
// int Queue::rear;
// int Queue::capacity;
// int* Queue::queue;
// Create a queue of capacity 4
let q = new queue(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
process.stdout.write("\n\nafter two node deletion\n\n");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
// The code is contributed by Arushi Jindal.
OutputQueue is Empty
20 <-- 30 <-- 40 <-- 50 <--
Queue is full
20 <-- 30 <-- 40 <-- 50 <--
after two node deletion
40 <-- 50 <--
Front Element is: 40
Below is the implementation of a Singly Linked List:
C++
#include <iostream>
class SinglyLList {
private:
class Node {
public:
int data;
Node* next;
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
Node* head;
public:
SinglyLList() { head = nullptr; }
void InsertAtStart(int data)
{
Node* new_node = new Node(data);
new_node->next = head;
head = new_node;
}
void InsertAtLast(int data)
{
Node* new_node = new Node(data);
if (head == nullptr) {
head = new_node;
return;
}
new_node->next = nullptr;
Node* last = head;
while (last->next != nullptr) {
last = last->next;
}
last->next = new_node;
}
void DeleteAtStart()
{
if (head == nullptr) {
std::cout << "List is empty" << std::endl;
return;
}
head = head->next;
}
void DeleteAtPos(int pos)
{
int position = 0;
if (pos > Count() || pos < 0) {
throw "Incorrect position exception";
}
Node* temp = head;
while (position != pos - 1) {
temp = temp->next;
position++;
}
temp->next = temp->next->next;
}
void DeleteAtLast()
{
Node* delete_node = head;
while (delete_node->next != nullptr
&& delete_node->next->next != nullptr) {
delete_node = delete_node->next;
}
delete_node->next = nullptr;
}
void Display()
{
Node* disp = head;
while (disp != nullptr) {
std::cout << disp->data << "->";
disp = disp->next;
}
}
int Count()
{
int elements = 0;
Node* count = head;
while (count != nullptr) {
count = count->next;
elements++;
}
return elements;
}
};
int main()
{
SinglyLList list;
// insert elements of singly linked list
// at beginning
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
// print linked list elements
list.Display();
std::cout << std::endl;
// insert element at the end of list
list.InsertAtLast(1);
std::cout << "after inserting node at the end\n";
// print linked list elements
list.Display();
std::cout << std::endl;
// delete an element at the given position
list.DeleteAtPos(1);
// delete starting element
list.DeleteAtStart();
// delete last element
list.DeleteAtLast();
std::cout
<< "after deleting node: second, first and last\n";
// print linked list elements
list.Display();
std::cout << std::endl;
return 0;
}
Java
// Java program to implement singly linked list
class SinglyLList {
class Node {
// node variables
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
// create reference variable of Node
Node head;
// function to insert a node
// at the beginning of the list
void InsertAtStart(int data)
{
// create a node
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
// function to insert node
// at the end of the list
void InsertAtLast(int data)
{
Node new_node = new Node(data);
if (head == null) {
head = new_node;
return;
}
new_node.next = null;
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
// function to delete a node
// at the beginning of the list
void DeleteAtStart()
{
if (head == null) {
System.out.println("List is empty");
return;
}
head = head.next;
}
// function to delete a node at
// a given position in the list
void DeleteAtPos(int pos) throws Exception
{
int position = 0;
if (pos > Count() || pos < 0) {
throw new Exception("Incorrect position exception");
}
Node temp = head;
while (position != pos - 1) {
temp = temp.next;
position++;
}
temp.next = temp.next.next;
}
// function to delete a node
// from the end of the list
void DeleteAtLast()
{
Node delete = head;
while (delete.next != null
&& delete.next.next != null) {
delete = delete.next;
}
delete.next = null;
}
// function to display all the nodes of the list
void Display()
{
Node disp = head;
while (disp != null) {
System.out.print(disp.data + "->");
disp = disp.next;
}
}
// function to return the total nodes in the list
int Count()
{
int elements = 0;
Node count = head;
while (count != null) {
count = count.next;
elements++;
}
return elements;
}
}
public class GFG {
// Driver code
public static void main(String[] args) throws Exception
{
// create object of class singlyList
SinglyLList list = new SinglyLList();
// insert elements of singly linked list
// at beginning
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
// print linked list elements
list.Display();
// insert element at the end of list
list.InsertAtLast(1);
System.out.println("\nafter inserting node at the end\n ");
// print linked list elements
list.Display();
// delete an element at the given position
list.DeleteAtPos(1);
// delete starting element
list.DeleteAtStart();
// delete last element
list.DeleteAtLast();
System.out.println("\nafter deleting node: second, first and last\n ");
// print linked list elements
list.Display();
}
}
Python
class SinglyLList:
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __init__(self):
self.head = None
def InsertAtStart(self, data):
new_node = self.Node(data)
new_node.next = self.head
self.head = new_node
def InsertAtLast(self, data):
new_node = self.Node(data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next is not None:
last = last.next
last.next = new_node
def DeleteAtStart(self):
if self.head is None:
print("List is empty")
return
self.head = self.head.next
def DeleteAtPos(self, pos):
position = 0
if pos > self.Count() or pos < 0:
raise Exception("Incorrect position exception")
temp = self.head
while position != pos - 1:
temp = temp.next
position += 1
temp.next = temp.next.next
def DeleteAtLast(self):
delete_node = self.head
while delete_node.next is not None and delete_node.next.next is not None:
delete_node = delete_node.next
delete_node.next = None
def Display(self):
disp = self.head
while disp is not None:
print(disp.data, "->", end=" ")
disp = disp.next
def Count(self):
elements = 0
count = self.head
while count is not None:
count = count.next
elements += 1
return elements
if __name__ == "__main__":
# create a singly linked list object
list = SinglyLList()
# insert elements of singly linked list at beginning
list.InsertAtStart(3)
list.InsertAtStart(2)
list.InsertAtStart(1)
# print linked list elements
list.Display()
print()
# insert element at the end of list
list.InsertAtLast(4)
print("after inserting node at the end")
# print linked list elements
list.Display()
print()
# delete an element at the given position
list.DeleteAtPos(1)
# delete starting element
list.DeleteAtStart()
# delete last element
list.DeleteAtLast()
print("after deleting node: second, first and last")
# print linked list elements
list.Display()
print()
C#
// C# program to implement singly linked list
using System;
public class SinglyLList
{
public class Node
{
// node variables
public int data;
public Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
// create reference variable of Node
public Node head;
// function to insert a node
// at the beginning of the list
public void InsertAtStart(int data)
{
// create a node
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
// function to insert node
// at the end of the list
public void InsertAtLast(int data)
{
Node new_node = new Node(data);
if (head == null)
{
head = new_node;
return;
}
new_node.next = null;
Node last = head;
while (last.next != null)
{
last = last.next;
}
last.next = new_node;
}
// function to delete a node
// at the beginning of the list
public void DeleteAtStart()
{
if (head == null)
{
Console.WriteLine("List is empty");
return;
}
head = head.next;
}
// function to delete a node at
// a given position in the list
public void DeleteAtPos(int pos)
{
int position = 0;
if (pos > Count() || pos < 0)
{
throw new Exception("Incorrect position exception");
}
Node temp = head;
while (position != pos - 1)
{
temp = temp.next;
position++;
}
temp.next = temp.next.next;
}
// function to delete a node
// from the end of the list
public void DeleteAtLast()
{
Node delete = head;
while (delete.next != null
&& delete.next.next != null)
{
delete = delete.next;
}
delete.next = null;
}
// function to display all the nodes of the list
public void Display()
{
Node disp = head;
while (disp != null)
{
Console.Write(disp.data + "->");
disp = disp.next;
}
}
// function to return the total nodes in the list
public int Count()
{
int elements = 0;
Node count = head;
while (count != null)
{
count = count.next;
elements++;
}
return elements;
}
}
class GFG
{
// Driver code
public static void Main(String[] args)
{
// create object of class singlyList
SinglyLList list = new SinglyLList();
// insert elements of singly linked list
// at beginning
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
// print linked list elements
list.Display();
// insert element at the end of list
list.InsertAtLast(1);
Console.WriteLine("\nafter inserting node at the end\n ");
// print linked list elements
list.Display();
// delete an element at the given position
list.DeleteAtPos(1);
// delete starting element
list.DeleteAtStart();
// delete last element
list.DeleteAtLast();
Console.WriteLine("\nafter deleting node: second, first and last\n ");
// print linked list elements
list.Display();
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to implement
// singly linked list
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
// create reference variable of Node
var head;
// function to insert a node
// at the beginning of the list
function InsertAtStart(data) {
// create a node
var new_node = new Node(data);
new_node.next = head;
head = new_node;
}
// function to insert node
// at the end of the list
function InsertAtLast(data) {
var new_node = new Node(data);
if (head == null) {
head = new_node;
return;
}
new_node.next = null;
var last = head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
// function to delete a node
// at the beginning of the list
function DeleteAtStart() {
if (head == null) {
document.write("List is empty");
return;
}
head = head.next;
}
// function to delete a node at
// a given position in the list
function DeleteAtPos(pos) {
var position = 0;
if (pos > Count() || pos < 0) {
}
var temp = head;
while (position != pos - 1) {
temp = temp.next;
position++;
}
temp.next = temp.next.next;
}
// function to delete a node
// from the end of the list
function DeleteAtLast() {
var deletenode = head;
while (deletenode.next != null &&
deletenode.next.next != null) {
deletenode = deletenode.next;
}
deletenode.next = null;
}
// function to display all the nodes of the list
function Display() {
var disp = head;
while (disp != null) {
document.write(disp.data + "->");
disp = disp.next;
}
}
// function to return the total nodes in the list
function Count() {
var elements = 0;
var count = head;
while (count != null) {
count = count.next;
elements++;
}
return elements;
}
// Driver code
// create object of class singlyList
// insert elements of singly linked list
// at beginning
InsertAtStart(3);
InsertAtStart(2);
InsertAtStart(1);
// print linked list elements
Display();
// insert element at the end of list
InsertAtLast(1);
document.write(
"<br/>after inserting node at the end<br/><br/> "
);
// print linked list elements
Display();
// delete an element at the given position
DeleteAtPos(1);
// delete starting element
DeleteAtStart();
// delete last element
DeleteAtLast();
document.write(
"<br/>after deleting node: second, first and last<br/>"
);
// print linked list elements
Display();
// This code is contributed by todaysgaurav
</script>
Output1->2->3->
after inserting node at the end
1->2->3->1->
after deleting node: second, first and last
3->
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all
Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read