DS ExternalLab
DS ExternalLab
Bubble Sort:
a = [64, 34, 25, 12, 22, 11, 90]
print("Before sorting: ", a)
for i in range(len(a)):
for j in range(0, len(a)-i-1):
if a[j] > a[j+1]:
a[j], a[j+1] = a[j+1], a[j]
selection sort:
a = [16, 10, -1, 369, 5]
for i in range(len(a)):
# Find the index of the minimum value in the unsorted part of the list
min_index = i
for j in range(i + 1, len(a)):
if a[j] < a[min_index]:
min_index = j
print(a)
Insertion sort:
a = [12, 111, 13, 5, 6]
print(a)
merge sort:
def mergesort(a):
mid = len(a) // 2
left = a[:mid]
right = a[mid:]
if len(left) > 1:
left = mergesort(left)
if len(right) > 1:
right = mergesort(right)
c = []
while left and right:
if left[-1] >= right[-1]:
c.append(left.pop())
else:
c.append(right.pop())
c.reverse()
return (left or right) + c
linear search:
if search in a:
print("Element found at index:", a.index(search))
else:
print("Not found")
Binary search:
if search == a[mid]:
print(search, "element found at the position", mid)
break
elif search < a[mid]:
stop = mid - 1
else:
start = mid + 1
else:
print("Element not found")
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct Stack {
int top;
int array[MAX];
};
void initialize(struct Stack* stack) {
stack->top = -1;
}
int isEmpty(struct Stack* stack) {
return (stack->top == -1);
}
int isFull(struct Stack* stack) {
return (stack->top == MAX - 1);
}
void push(struct Stack* stack, int item) {
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->array[stack->top--];
}
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->array[stack->top];
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("%d popped from stack\n", pop(&stack));
printf("Top element is %d\n", peek(&stack));
return 0;
}
while stack:
output=output+stack.pop()
return output
exp=input("Expression ?")
print('postfix expression: ',infix_to_postfix(exp))
#Expression ?"a+b*(c^d-e)^(f+g*h)-i";
Postfix evaluation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
void push(Stack *stack, int item) {
if (stack->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
exit(1);
}
stack->items[++stack->top] = item;
}
int pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack Underflow\n");
exit(1);
}
return stack->items[stack->top--];
}
int isOperand(char ch) {
return (ch >= '0' && ch <= '9');
}
int evaluatePostfix(char postfix[]) {
Stack stack;
stack.top = -1;
char *token = strtok(postfix, " ");
while (token != NULL) {
if (isOperand(token[0]))
push(&stack, atoi(token));
else {
int operand2 = pop(&stack);
int operand1 = pop(&stack);
switch (token[0]) {
case '+':
push(&stack, operand1 + operand2);
break;
case '-':
push(&stack, operand1 - operand2);
break;
case '*':
push(&stack, operand1 * operand2);
break;
case '/':
push(&stack, operand1 / operand2);
break;
}
}
token = strtok(NULL, " ");
}
return pop(&stack);
}
int main() {
// Example usage:
char postfix[] = "2 3 1 * + 9 -";
int result = evaluatePostfix(postfix);
printf("Postfix Evaluation Result: %d\n", result);
return 0;
}
#include<stdio.h>
int v,a[50][50],visited[20],n;
void dfs(int v)
{
int i;
visited[v]=1;
for(i=0;i<n;i++)
{
if(a[v][i]!=0 && visited[i]==0) // to check adjacent and not visited
vertex
{
printf("%d ",i);
dfs(i);
}
}
}
int main()
{
int i,j;
printf("\n Enter the number of vertices :");
scanf("%d",&n);
f=r=0;
q[r]=v;
visited[v]=1;
printf("%d ",v);
bfs(v);
}
temp=start;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("%d",count);
if(start==NULL)
start=newnode;
else
{
printf("\n Enter the position to insert ");
scanf("%d",&pos);
temp=start;
if(pos>=count)
{
printf("\n Invalid Position");
}
else
{
for(i=2;i<pos-1;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
}
void del_at_beg()
{
//temp=start;
start=start->next;
//free(temp);
}
void del_at_mid()
{
int key;
printf("\n Enter element to delete..");
scanf("%d",&key);
temp=start;
while(temp!=NULL)
{
if(temp->data==key) /Element deleted in between/
{
p->next=temp->next;
}
p=temp;
temp=temp->next;
}
display();
}
void del_at_end()
{
temp=start;
while(temp->next->next!= NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
void search()
{
int flag = 0,search;
printf("\n Enter data to search:");
scanf("%d",&search);
temp = start;
while(temp!=NULL)
{
if(temp->data == search)
{
flag=1;
break;
}
temp=temp->next;
}
if(flag == 1)
printf("\n %d Element is Present in the List...",search);
else
printf("\n Not found..");
}
void count()
{
int length = 0;
temp = start;
while(temp!=NULL)
{
length++;
temp=temp->next;
}
printf("\n Length of Linked List : %d",length);
//return;
}
int main()
{
int choice;
printf("\n ***Singly Linear Linked list***\n");
do
{
printf("\n 1.Create \n 2.Display \n 3.Insert begin \n 4.Insert Last \n
5.Insert middle \n 6.Deletion at beg \n 7.Deletion at middle \n 8.Deletion at
end\n 9.Search \n 10.Number of nodes \n 11.Exit \n");
switch(choice)
{
case 1: create(); break;
case 2: display(); break;
case 3: insert_at_beg(); break;
case 4: insert_at_end(); break;
case 5: insert_mid(); break;
case 6: del_at_beg(); break;
case 7: del_at_mid(); break;
case 8: del_at_end(); break;
case 9: search(); break;
case 10: count(); break;
case 11: exit(0);
}
}while(choice<12);
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
struct Node *prev;
} Node;
Node *insertAtEnd(Node *head, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
return newNode;
}
Node *current = head;
while (current->next != NULL)
current = current->next;
current->next = newNode;
newNode->prev = current;
return head;
}
Node *deleteAtBegin(Node *head) {
if (head == NULL) {
printf("List is empty.\n");
return NULL;
}
Node *temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
free(temp);
return head;
}
void displayList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
head = insertAtEnd(head, 10);
head = insertAtEnd(head, 20);
head = insertAtEnd(head, 30);
printf("Original List: ");
displayList(head);
head = deleteAtBegin(head);
printf("List after deleting from the beginning: ");
displayList(head);
return 0;
}
// Example usage:
// Node *head = NULL;
// head = insertAtEnd(head, 10);
// head = insertAtEnd(head, 20);
// head = insertAtEnd(head, 30);
// head = deleteAtBegin(head);
***THE END***