0% found this document useful (0 votes)
25 views

DS Algorithms

The document describes algorithms for common data structures like linear search, binary search, bubble sort, insertion sort, stack, queue, and linked lists. Linear search searches sequentially through an array. Binary search divides the search space in half at each step. Bubble sort compares adjacent elements and swaps them if out of order. Insertion sort inserts elements into a sorted sublist. Common stack and queue operations like push, pop, enqueue and dequeue are covered. Linked list operations like insertion and deletion are also described.

Uploaded by

GR R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

DS Algorithms

The document describes algorithms for common data structures like linear search, binary search, bubble sort, insertion sort, stack, queue, and linked lists. Linear search searches sequentially through an array. Binary search divides the search space in half at each step. Bubble sort compares adjacent elements and swaps them if out of order. Insertion sort inserts elements into a sorted sublist. Common stack and queue operations like push, pop, enqueue and dequeue are covered. Linked list operations like insertion and deletion are also described.

Uploaded by

GR R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structure algorithms

Ex:1 Algorithm for Linear Search

• Linear Search ( Array A, Value x)



• Step 1: intialize i to 1
• Step 2: if i > n then go to step 7
• Step 3: if A[i] equates the value of x then go to step 6
• Step 4: Allow i to increase by 1
• Step 5: Go to Step 2
• Step 6: Print Element x Found at index i and go to step 8
• Step 7: Print element not found
• Step 8: Exit

Ex:2 Algorithm for Binary Search


Declare five variables i, data, low, mid, high. And array a[5].
Step 1 − Accept the data from the user in a ascending order
Start searching data from middle of the list like the mean position of low and
high
Step 2 − If it is the element to be searched out return it, and exit.
Step 3 − If it is not , change position of the middle such as
Step 4 − If data is greater than middle, shift low..
Low=mid+1;
Step 5 − If data is smaller than middle, shift high.
High=mid-1;
Step 6 – caluculate mid = l(ow+high)/2
Step 7 − Repeat step 3 through 6,until match.
Step8 - if search fail print data not found.

Ex:3 Algorithm for Bubble sort

Step 1-declare i, j, temp ,a[5]


Step 2- accept elements into the array using for loop to a[i].
Step 3- initialise i with 0 and j with 1,
Step 4- then compare a[i[ with a[j],
If a[i]>a[j]
Swap the values of a[i] and a[j].
Step 5- repeat step 4
Until i and j reaches 4.
Ex:4 Algorithm for Insertion Sort:

• Step 1 − If it is the first element, it is already sorted. return 1;


• Step 2 − Pick next element
• Step 3 − Compare with all elements in the sorted sub-list
• Step 4 − Shift all the elements in the sorted sub-list that is greater than the
• value to be sorted
• Step 5 − Insert the value
• Step 6 − Repeat until list is sorted

Ex:5 Algorithm for Stack Operations:


Push operations

• Step 1 − Checks if the stack is full.


• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.

Pop operations:

• Step 1 − Checks if the stack is empty.


• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at whichtop is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.

Ex:6 Algorithm for Queue using array:


Enqueue operations
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Dequeue operations
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
Ex:7 Algorithm for Single Linked list Operations

Creating a node:

Step 1: Include all the Header files which are used in the program.

Step 2: Declare all the user defined functions.

Step 3: Define a Node structure with two members data and next

Step 4: Define a Node pointer 'Start' and set it to NULL.

Step 4: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.

Insert Operations

We can use the following steps to insert a new node in the single linked list...

Step 1: Create a newNode with given value.

Step 2: Check whether list is Empty (Start == NULL)

Step 3: If it is Empty then, set newNode → next = NULL and Start = newNode.

Step 4: If it is Not Empty then, define a node pointer current and initialize with Start.

Step 5: Keep moving the Current to its next node until it reaches to the node after which
we want to insert the newNode

Step 6: Finally, Set 'Current → next = newNode' and 'Current =newNode '

Delete operation

We can use the following steps to delete a specific node from the single linked list...

Step 1: Check whether list is Empty (Start == NULL)

Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate
the function.

Step 3: If it is Not Empty then, define two Node pointers 'temp' and 'prev' and initialize
'temp' with Start.

Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to the
last node. And every time set 'prev = temp' before moving the 'temp' to its next node.

Step 5: If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
Step 6: If it is reached to the exact node which we want to delete, then check whether
list is having only one node or not

Step 7: If list has only one node and that is the node to be deleted, then
set Start = NULL and delete temp (free(temp)).

Step 8: If list contains multiple nodes, then check whether temp is the first node in the
list (temp == Start).

Step 9: If temp is the first node then move the Start to the next node (Start = Start →
next) and delete temp.

Step 10: If temp is not first node then check whether it is last node in the list (temp →
next == NULL).

Step 11: If temp is last node then set prev → next = NULL and delete temp (free(temp)).

Step 12: If temp is not first node and not last node then set prev → next = temp →
next and delete temp (free(temp)).

Ex:8Algorithm for double Linked list Operations

Insertion

We can use the following steps to insert a new node after a node in the double linked list...

Step 1: Create a newNode with given value.

Step 2: Check whether list is Empty (Start == NULL)

Step 3: If it is Empty then, assign NULL to newNode → previous & newNode →


next and newNode to Start.

Step 4: If it is not Empty then, define a node pointer last and initialize last with Start.

Step 5: Step 7: Assign Last to Newnode-> prev, newNode ->right to Null and then assign
newnode to last

Deletion

We can use the following steps to delete a node from the double linked list...

Step 1: Check whether list is Empty (Start == NULL)

Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with Start.

Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to the last
node.

Step 5: If it is reached to the last node, then display 'Given node not found in the list!
Deletion not possible!!!' and terminate the fuction.

Step 6: If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not

Step 7: If list has only one node and that is the node which is to be deleted then
set Start to NULL and delete temp (free(temp)).

Step 8: If list contains multiple nodes, then check whether temp is the first node in the list
(temp == Start).

Step 9: If temp is the first node, then move the Start to the next node (Start = Start → next),
set Start of previous to NULL (Start → previous = NULL) and delete temp.

Step 10: If temp is not the first node, then check whether it is the last node in the list (temp
→ next == NULL).

Step 11: If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp(free(temp)).

Step 12: If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp →
previous) and deletetemp (free(temp)).

You might also like