0% found this document useful (0 votes)
9 views10 pages

algos of everything

The document provides a series of C programming tasks, including initializing and printing 1D and 2D arrays, finding the largest number in a 2D array, calculating transposes, and summing elements. It also covers operations on linked lists, queues, and binary trees, including insertion, deletion, and searching algorithms. Each task is presented with a structured algorithm outlining the steps to implement the functionality.

Uploaded by

vanadita sharma
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)
9 views10 pages

algos of everything

The document provides a series of C programming tasks, including initializing and printing 1D and 2D arrays, finding the largest number in a 2D array, calculating transposes, and summing elements. It also covers operations on linked lists, queues, and binary trees, including insertion, deletion, and searching algorithms. Each task is presented with a structured algorithm outlining the steps to implement the functionality.

Uploaded by

vanadita sharma
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/ 10

Write a C program to initialize and print 1D array.

Start
Input the size of the array, n.
Declare an empty array, arr[].
For i = 0 to n-1, do:
● Input the value for arr[i].
● Assign the value to arr[i].
For i = 0 to n-1, do:
● Display arr[i].
End

Write a C program to initialize and print 2D array.


Start
Input the number of rows m and columns n for the array.
Declare an empty 2D array, arr[m][n].
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
i. Input the value for arr[i][j].
ii. Assign the value to arr[i][j].
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
i. Display arr[i][j].
End

Write a C program to find largest number among the given 2D array.


Start
Input the number of rows m and columns n for the array.
Declare a 2D array, arr[m][n].
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
○ Input the value for arr[i][j].
○ Assign the value to arr[i][j].

Initialize a variable max_element to store the largest element, set it to arr[0][0].


For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
○ If arr[i][j] > max_element, update
max_element = arr[i][j].
Display the largest element, max_element.
End
Write a C program to find transpose of given 2D array.
Start
Input the number of rows m and columns n for the array.
Declare a 2D array, arr[m][n].
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
○ Input the value for arr[i][j].
○ Assign the value to arr[i][j].
Declare a new 2D array, transpose[n][m].
For i = 0 to m-1 (for each row of arr):
● For j = 0 to n-1 (for each column of arr):
○ Assign transpose[j][i] = arr[i][j] (swap rows and columns).
Display the transpose array transpose[n][m].
End

Write a C program to find sum of elements of 2D array


Start
Input the number of rows m and columns n for the array.
Declare a 2D array, arr[m][n].
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
○ Input the value for arr[i][j].
○ Assign the value to arr[i][j].
Initialize a variable sum to 0.
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
○ Add arr[i][j] to sum.
Display the value of sum.
End

Write a C program to calculate the sum of lower left elements of a 2D array


Start
Input the number of rows m and columns n for the array (assume a square matrix, m = n).
Declare a 2D array, arr[m][n].
For i = 0 to m-1 (for each row):
● For j = 0 to n-1 (for each column):
○ Input the value for arr[i][j].
○ Assign the value to arr[i][j].
Initialize a variable sum to 0.
For i = 0 to m-1 (for each row):
●For j = 0 to i (for columns from the start to the diagonal):
○ Add arr[i][j] to sum.
Display the value of sum (sum of the lower left triangle).
End

Write a C program to insert an element in 1D array in required position.


Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
● Assign the value to arr[i].
Input the position pos where the new element needs to be inserted (1-based index).
Input the value of the new element element.
If pos is invalid (i.e., pos < 1 or pos > n+1):
● Display an error message and exit.
Shift elements from position pos-1 (0-based index) to the right:
● For i = n-1 down to pos-1:
○ Set arr[i+1] = arr[i].
Insert the new element at position pos-1: arr[pos-1] = element.
Increment the size of the array by 1.
Display the new array.
End

Write a C program to delete an element in 1D array in required position.

Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
● Assign the value to arr[i].
Input the position pos of the element to be deleted (1-based index).
If pos is invalid (i.e., pos < 1 or pos > n):
● Display an error message and exit.
Shift elements to the left to delete the element:
● For i = pos-1 to n-2:
○ Set arr[i] = arr[i+1].
Decrement the size of the array by 1.
Display the new array.
End

Write a C program to linear search an element in 1D array.


Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
● Assign the value to arr[i].
Input the element target to search for.
Initialize a flag found as False.
For i = 0 to n-1:
● If arr[i] == target, then:
○ Set found to True.
○ Display the position i+1 (1-based index) where the element is found.
○ Break the loop.
If found == False, display "Element not found."
End

Write a C program to Binary search an element in 1D array.


Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
● Assign the value to arr[i].
Sort the array arr[] in ascending order.
Input the target element target to search for.
Initialize low to 0, high to n-1.
While low is less than or equal to high:
● Set mid to (low + high) // 2.
● If arr[mid] == target, then:
○ Display the position mid + 1 (1-based index)where the element is found.
○ Break the loop.
● Else If arr[mid] < target:
○ Set low to mid + 1.
● Else:
○ Set high to mid - 1.
If the element is not found, display "Element not found."
End

Write a C program to sort an array using Bubble Sort.

Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
For i = 0 to n-1:
● For j = 0 to n-i-2:
○ If arr[j] > arr[j+1], then:
■ Swap arr[j] and arr[j+1].
Display the sorted array.
End

Write a C program to sort an array using Selection Sort.


Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
For i = 0 to n-2:
● Set min_index = i.
● For j = i + 1 to n-1:
○ If arr[j] < arr[min_index], then:
■ Set min_index = j.
● If min_index is not equal to i, then:
○ Swap arr[i] and arr[min_index].
Display the sorted array.
End

Write a C program to sort an array using Insertion Sort.


Start
Input the size of the array n.
Declare an array arr[] of size n.
For i = 0 to n-1:
● Input the value for arr[i].
● Assign the value to arr[i].
For i = 1 to n-1:
● Set key = arr[i].
● Set j = i - 1.
● While j >= 0 and arr[j] > key:
○ Set arr[j + 1] = arr[j].
○ Decrement j by 1.
● Set arr[j + 1] = key.
Display the sorted array.
End

Write a C program to sort an array using Quick Sort.


Write a C program to sort an array using Merge Sort.
Write a C program to create and traverse a Singly Linked List.
⦁ [Initialize] Set PTR = HEAD
⦁ Repeat until PTR != NULL
⦁ Apply to PTR Data
⦁ Set PTR = PTR NEXT
⦁ Stop

Write a C program to search for an element in a Singly Linked List.

⦁ Set PTR = HEAD


⦁ Repeat until PTR != NULL
⦁ If ( PTR Data = = Value) { Display PTR Data and PTR and Stop}
⦁ Else { PTR = PTR NEXT}
⦁ If ( PTR = = NULL) { Display not found }
⦁ Stop

Write a C program to insert a node at the beginning of a Singly linked list.


⦁ If AVAIL = = NULL ( Display Overflow )
⦁ Set New Node = AVAIL
⦁ Set AVAIL = AVAIL → NEXT
⦁ Set New Node → Data = Value
⦁ Set New Node → NEXT = HEAD
⦁ Set HEAD = New Node
⦁ EXIT

Write a C program to delete a node from the end of a Singly linked list.
⦁ If START = = NULL ( Display Underflow )
⦁ Set PTR = START
⦁ Repeat Until PTR → NEXT != NULL
⦁ Set PREPTR = PTR
⦁ Set PTR = PTR → NEXT
⦁ Set PREPTR → NEXT = NULL
⦁ FREE PTR
⦁ EXIT
Write a C program to insert a node in the beginning of a Circular linked list.

⦁ If AVAIL = = NULL ( Display Overflow )


⦁ Set New Node = AVAIL
⦁ Set AVAIL = AVAIL NEXT
⦁ Set New Node Data = Value
⦁ Set PTR = HEAD
⦁ Repeat until PTR NEXT != START
⦁ PTR = PTR NEXT
⦁ Set New Node NEXT = HEAD
⦁ Set PTR NEXT = New Node
⦁ Set HEAD = New Node
⦁ EXIT

Write a C program to delete a node from the end of a Circular linked list.
⦁ If START = = NULL ( Display Underflow )
⦁ Set PTR = START
⦁ Repeat until PTR NEXT != START
⦁ Set PREPTR = PTR
⦁ Set PTR = PTR NEXT
⦁ Set PREPTR NEXT = START
⦁ FREE PTR
⦁ EXIT

Write a C program to insert a node at the beginning of a Doubly linked list.

⦁ If AVAIL = = NULL ( Display Overflow )


⦁ Set New Node = AVAIL
⦁ Set AVAIL = AVAIL → NEXT
⦁ Set New Node → Data = Value
⦁ Set New Node PREV = NULL
⦁ Set New Node → NEXT = HEAD
⦁ Set HEAD PREV = New Node
⦁ Set HEAD = New Node
⦁ EXIT

Write a C program to delete a node from the beginning of a Doubly linked list.
⦁ If START = = NULL ( Display Underflow )
⦁ Set PTR = START
⦁ Set START = START PREV
⦁ Set START PREV = NULL
⦁ FREE PTR
⦁ EXIT

Write a C program to enqueue and dequeue an element from a queue.

⦁ If REAR = MAX – 1
Write Overflow
go to step 4
⦁ If FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
⦁ SET QUEUE[REAR] = NUM
⦁ EXIT
Dequeue:-
⦁ If FRONT = -1
Write Underflow go to step 2
ELSE If FRONT = REAR
SET VAL = QUEUE[FRONT]
SET FRONT = REAR = -1
ELSE
SET VALUE = QUEUE[FRONT]
SET FRONT = FRONT + 1
⦁ EXIT

Write a C program to enqueue and dequeue an element from a circular queue.

Enqueue:-
⦁ If (REAR+1)% MAX = FRONT
Write Overflow go to step 4
⦁ If FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE SET REAR = (REAR + 1)%MAX
⦁ SET QUEUE[REAR] = VALUE
⦁ EXIT
Dequeue:-
⦁ If FRONT = -1
Write Underflow go to step 4
⦁ SET VALUE = OUEUE[FRONT]
⦁ If FRONT = REAR
SET FRONT = REAR = -1
ELSE
SET FRONT = (FRONT + 1)%MAX
⦁ EXIT

Write a C program for insertion in Binary tree


Step 1: Set PTR = TREE
Step 2: Repeat while correct position is not acquired
If VAL < PTR-> DATA
PTR =PTR->LEFT
Else PTR =PTR->RIGHT
Step 3: Create an NEW_NODE
Step 4: Set NEW_NODE-> LEFT = NULL
Set NEW_NODE-> RIGHT =NULL
Step 5: If VAL < PARENT NODE-> DATA
Insert as Left node
Else
Insert as Right node
Step 6: Exit

Write a C program for deletion in Binary Search tree

Input: Root node of the BST, key to be deleted (k)

Output: Updated BST with the node deleted

Step 1: Search for the node with key k in the BST.

If the node is not found, return an error or do nothing.


If the node is a leaf node (has no children), proceed to Step 2.
Step 2: Handle leaf node deletion:

If the node to be deleted is a leaf node, simply remove it from the tree.
Step 3: Handle node with one child deletion:

If the node to be deleted has only one child (either left or right), replace the node with its child.
Update the child’s parent pointer to point to the node’s original parent.
Step 4: Handle node with two children deletion:

Find the node’s in-order successor (the smallest node in the right subtree).
Replace the node’s key with the in-order successor’s key.
Recursively delete the in-order successor from the right subtree.
Step 5: Update the node’s children:

If the node’s left child exists, update its parent pointer to point to the node’s original parent.
If the node’s right child exists, update its parent pointer to point to the node’s original parent.
Step 6: Return the updated BST.

You might also like