Problem 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 89

Problem 1(a)

//Wap to implement the linear search algorithm.

#include<stdio.h>

int main()

int i,num, item,flag=0;

int a[num];

printf("enter the number of elements");

scanf("%d",&num);

printf("enter array elements\n");

for(i=0;i<num;i++)

scanf("%d",&a[i]);

printf("enter the elements to be searched\n");

scanf("%d",&item);

for(i=0;i<num;i++)

if(item == a[i])

flag=1;

break;

}
}

if(flag==1)

printf("element is present in the location %d", i+1);

else

printf("element is not present in the array\n");

OUTPUT: 1)

enter the number of elements 4

enter array elements

enter the element to be searched

element is present in the location 3

OUTPUT 2):

enter the number of elements 4

enter the elements

4
enter the elements to be searched

element is not present in the array


PROBLEM – 1(b)

//wap to implement binary search algorithm.

#include<stdio.h>

int main()

int i, first, last, mid, n, search, a[n];

printf("enter the number of elements\n");

scanf("%d", &n);

printf("enter array elements\n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("enter the value to be searched\n");

scanf("%d",&search);

first =0;

last= n-1;

mid = (first +last)/2;

while(first<=last)

if(a[mid]<search)

first = mid +1;

else if(a[mid] == search)


{

printf("element is found at location %d\n", search, mid+1);

break;

else

last = mid-1;

mid = (first +last)/2;

if(first>last)

printf("element is not found in the array", search);

return 0;

OUTPUT (1):

enter the number of elements

enter array elements

enter element to be searched

element is found at location 3


OUTPUT (2):

enter the number of elements

enter the array elements

enter element to be searched

element is not found in the array


PROBLEM – 2(a)

//wap to create a linked list and serach an element in the linked list.

#include<stdio.h>

#include<stdlib.h>

struct node

int num;

struct node *next;

};

void create(struct node **);

int search(struct node *head, int item);

void display(struct node *);

int main()

struct node *p= NULL;

int item, result;

printf("enter data into the list\n");

create(&p);

printf("displaying the nodes in the list\n");

display(p);

printf("enter item to be searched\n");

scanf("%d", &item);
result = search(p,item);

if(result)

printf("%d element is found in the list\n", item);

else

printf("%d element is not found in the list\n", item);

return 0;

int search(struct node *head, int item)

while(head!=NULL)

if(head->num== item)

return 1;

head = head->next;
}

return 0;

int create(struct node *head)

int c, ch;

struct node *temp, *rear;

do

printf("enter number");

scanf("%d", &c);

temp = (struct node *)malloc(sizeof(struct node));

temp->num = c;

temp->next = NULL;

if(head == NULL)

head = temp;

else

rear->next = temp;

rear = temp;
printf("do you wish to continue [1/0]");

scanf("%d",&ch);

}while(ch!=0);

printf("\n");

return 0;

void display(struct node *p)

while(p!=NULL)

printf("%d\t", p->num);

p = p->next;

printf("\n");

OUTPUT:

Enter data into the list

Enter number 12

Do you wish to continue [1/0]: 1

Enter number: 4

Do you wish to continue [1/0]: 1


Enter number 24

Do you wish to continue [1/0]: 1

Enter number 36

Do you wish to continue [1/0]: 0

Displaying the nodes in the list:

12 4 24 36

Enter item to search in the list: 36

36 found in the list


PROBLEM – 2(b)

//wap to create a linked list and reverse linked list.

#include<stdio.h>

#include<stdlib.h>

struct node

int num;

struct node *next;

};

void create(struct node **);

void reverse(struct node **);

void display(struct node *);

int main()

struct node *p = NULL;

int n;

printf("enter data into the list\n");

create(&p);

printf("displaying the nodes in the list\n");

display(p);

printf("reversing the list\n");

reverse(&p);

printf("displaying the reversing list\n");

display(p);
return 0;

void reverse(struct node **head)

struct node *p, *q, *r;

p = q= r = *head;

p = p->next->next;

q = q->next;

r->next = NULL;

q->next = r;

while(p!=NULL)

r = q;

q = p;

p = p->next;

q->next = r;

}
head = q;

void create(struct node **head)

int c, ch;

struct node *temp, *rear;

do

{
printf("enter number");

scanf("%d",&c);

temp = (struct node *)malloc(sizeof(struct node));

temp->num = c;

temp->next = NULL;

if(head == NULL)

head = temp;

else

rear->next = temp;

rear = temp;

printf("do you wish to continue [1/0]:");

scanf(%d", &ch);

}while(ch!=0);

printf("\n");

void display(struct node **head)

struct node *temp = *head;

head = head->next;

while(head!= NULL)
{

free(temp);

temp = head;

head = head->next;

OUTPUT:

enter data into the list

enter number: 1

do you wish to continue [1/0]: 1

enter number: 2

do you wish to continue [1/0]: 1

enter number: 3

do you wish to continue [1/0]: 1

enter number: 4

do you wish to continue [1/0]: 0

displaying the nodes in the list:

1 2 3 4

reversing the list......

displaying the reversed list:

4 3 2 1
PROBLEM – 3(a)
//wap to insert node at the begining of singly linked list.

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

} *head;

void createlist(int n);

void insertatbeg(int data);

void display();

int main()

int n, data;

printf("enter the total number of nodes");

scanf("%d", &n);

createlist(n);

printf("\n data in the list\n");

displaylist();

printf("\n enter data to insert at the beginning of the list");

scanf("%d", %data);
insertatbeg(data);

printf("\ndata in the list\n");

displaylist();

return 0;

void createlist(int n)

struct node *newnode, *temp;

int data, i;

head = (struct node *)malloc(sizeof(struct node));

if(head == NULL)

printf("unable to allocate memory");

else

printf("enter the data of node 1:");

scanf("%d", &data);

head->data = data;

head->next = NULL;

temp = head;

for(i=2;i<n;i++)
{

newnode = (struct node *)malloc(sizeof(struct node));

if(newnode = NULL)

printf("unable to allocate memory");

break;

else

printf(enter the data of node %d:", i);

scanf("%d", &data);

newnode->data = data;

newnode->next = NULL;

temp->next = newnode;

temp = temp->next;

printf("singly linked list created successfully\n");

void insertatbeg(int data)

{
struct node *newnode;

newnode = (struct node*)malloc(sizeof(struct node));

if(newnode == NULL)

printf("unable to allocate memory");

else

newnode->data = data;

newnode->next = head;

head = newnode;

printf("data inserted successfully");

void displaylist()

struct node *temp;

if(head == NULL)

printf("list is empty");

else
{

temp = head;

while(temp!=NULL)

printf("data %d\n", temp->data);

temp = temp->next;

OUTPUT:

enter the total number of nodes: 4

enter the data of node 1: 20

enter the data of node 2: 30

enter the data of node 3: 40

enter the data of node 4: 50

singly linked list created successfully

data in the list

data = 20

data = 30

data = 40

data = 50

enter data to insert at beginning of the list: 10


data inserted successfully

data in the list

data = 10
PROBLEM – 3(b)
//wap to insert element at the end of linked list.

#include <stdio.h>

#include <stdlib.h>

/* Structure of a node */

struct node {

int data; // Data

struct node *next; // Address

}*head;

void createList(int n);

void insertNodeAtEnd(int data);

void displayList();

int main()

int n, data;

/*

* Create a singly linked list of n nodes

*/

printf("Enter the total number of nodes: ");

scanf("%d", &n);

createList(n);
printf("\nData in the list \n");

displayList();

/*

* Insert data at the end of the singly linked list

*/

printf("\nEnter data to insert at end of the list: ");

scanf("%d", &data);

insertNodeAtEnd(data);

printf("\nData in the list \n");

displayList();

return 0;

/*

* Create a list of n nodes

*/

void createList(int n)

struct node *newNode, *temp;


int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*

* If unable to allocate memory for head node

*/

if(head == NULL)

printf("Unable to allocate memory.");

else

/*

* Reads data of node from the user

*/

printf("Enter the data of node 1: ");

scanf("%d", &data);

head->data = data; // Link the data field with data

head->next = NULL; // Link the address field to NULL


temp = head;

/*

* Create n nodes and adds to linked list

*/

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data

newNode->next = NULL; // Link the address field of newNode with NULL


temp->next = newNode; // Link previous node i.e. temp to the newNode

temp = temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

/*

* Create a new node and inserts at the end of the linked list.

*/

void insertNodeAtEnd(int data)

struct node *newNode, *temp;

newNode = (struct node*)malloc(sizeof(struct node));

if(newNode == NULL)

{
printf("Unable to allocate memory.");

else

newNode->data = data; // Link the data part

newNode->next = NULL;

temp = head;

// Traverse to the last node

while(temp->next != NULL)

temp = temp->next;

temp->next = newNode; // Link address part

printf("DATA INSERTED SUCCESSFULLY\n");

/*

* Display entire list


*/

void displayList()

struct node *temp;

/*

* If the list is empty i.e. head = NULL

*/

if(head == NULL)

printf("List is empty.");

else

temp = head;

while(temp != NULL)

printf("Data = %d\n", temp->data); // Print data of current node

temp = temp->next; // Move to next node

}
OUTPUT:

Enter the total number of nodes: 3

Enter the data of node 1: 10

Enter the data of node 2: 20

Enter the data of node 3: 30

SINGLY LINKED LIST CREATED SUCCESSFULLY

Data in the list

Data = 10

Data = 20

Data = 30

Enter data to insert at end of the list: 40

DATA INSERTED SUCCESSFULLY

Data in the list

Data = 10

Data = 20

Data = 30

Data = 40
PROBLEM – 3(c)
//wap to insert an element after a given node in the linked list.

#include<stdio.h>

#include<stdlib.h>

void randominsert(int);

void create(int);

struct node

int data;

struct node *next;

};

struct node *head;

void main ()

int choice,item,loc;

do

printf("\nEnter the item which you want to insert?\n");

scanf("%d",&item);

if(head == NULL)

create(item);
}

else

randominsert(item);

printf("\nPress 0 to insert more ?\n");

scanf("%d",&choice);

}while(choice == 0);

void create(int item)

struct node *ptr = (struct node *)malloc(sizeof(struct node *));

if(ptr == NULL)

printf("\nOVERFLOW\n");

else

ptr->data = item;

ptr->next = head;

head = ptr;
printf("\nNode inserted\n");

void randominsert(int item)

struct node *ptr = (struct node *) malloc (sizeof(struct node));

struct node *temp;

int i,loc;

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("Enter the location");

scanf("%d",&loc);

ptr->data = item;

temp=head;

for(i=0;i<loc;i++)

temp = temp->next;
if(temp == NULL)

printf("\ncan't insert\n");

return;

ptr ->next = temp ->next;

temp ->next = ptr;

printf("\nNode inserted");

OUTPUT:

Enter the item which you want to insert?

12

Node inserted

Press 0 to insert more ?

2
PROBLEM – 4(a)
//write a program to deleter an element at the start of the linked list.

#include <stdio.h>

#include <stdlib.h>

/* Structure of a node */

struct node {

int data; // Data

struct node *next; // Address

}*head;

void createList(int n);

void deleteFirstNode();

void displayList();

int main()

int n, choice;

/*

* Create a singly linked list of n nodes

*/

printf("Enter the total number of nodes: ");

scanf("%d", &n);

createList(n);
printf("\nData in the list \n");

displayList();

printf("\nPress 1 to delete first node: ");

scanf("%d", &choice);

/* Delete first node from list */

if(choice == 1)

deleteFirstNode();

printf("\nData in the list \n");

displayList();

return 0;

/*

* Create a list of n nodes

*/

void createList(int n)
{

struct node *newNode, *temp;

int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*

* If unable to allocate memory for head node

*/

if(head == NULL)

printf("Unable to allocate memory.");

else

/*

* In data of node from the user

*/

printf("Enter the data of node 1: ");

scanf("%d", &data);

head->data = data; // Link the data field with data


head->next = NULL; // Link the address field to NULL

temp = head;

/*

* Create n nodes and adds to linked list

*/

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);
newNode->data = data; // Link the data field of newNode with data

newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode

temp = temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

/*

* Deletes the first node of the linked list

*/

void deleteFirstNode()

struct node *toDelete;

if(head == NULL)

{
printf("List is already empty.");

else

toDelete = head;

head = head->next;

printf("\nData deleted = %d\n", toDelete->data);

/* Clears the memory occupied by first node*/

free(toDelete);

printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");

/*

* Displays the entire list

*/

void displayList()

{
struct node *temp;

/*

* If the list is empty i.e. head = NULL

*/

if(head == NULL)

printf("List is empty.");

else

temp = head;

while(temp != NULL)

printf("Data = %d\n", temp->data); // Print data of current node

temp = temp->next; // Move to next node

OUTPUT:

Enter the total number of nodes: 5

Enter the data of node 1: 10


Enter the data of node 2: 20

Enter the data of node 3: 30

Enter the data of node 4: 40

Enter the data of node 5: 50

SINGLY LINKED LIST CREATED SUCCESSFULLY

Data in the list

Data = 10

Data = 20

Data = 30

Data = 40

Data = 50

Press 1 to delete first node: 1

Data deleted = 10

SUCCESSFULLY DELETED FIRST NODE FROM LIST

Data in the list

Data = 20

Data = 30

Data = 40

Data = 50
PROBLEM- 4(b)

//wap to delete last element in the linked list.


#include <stdio.h>

#include <stdlib.h>

/* Structure of a node */

struct node {

int data; // Data

struct node *next; // Address

}*head;

void createList(int n);

void deleteLastNode();

void displayList();

int main()

int n, choice;

/*

* Create a singly linked list of n nodes

*/

printf("Enter the total number of nodes: ");

scanf("%d", &n);

createList(n);
printf("\nData in the list \n");

displayList();

printf("\nPress 1 to delete last node: ");

scanf("%d", &choice);

/* Delete last node from list */

if(choice == 1)

deleteLastNode();

printf("\nData in the list \n");

displayList();

return 0;

/*

* Create a list of n nodes

*/

void createList(int n)
{

struct node *newNode, *temp;

int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*

* If unable to allocate memory for head node

*/

if(head == NULL)

printf("Unable to allocate memory.");

else

/*

* Input data of node from the user

*/

printf("Enter the data of node 1: ");

scanf("%d", &data);

head->data = data; // Link the data field with data


head->next = NULL; // Link the address field to NULL

temp = head;

/*

* Create n nodes and adds to linked list

*/

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);
newNode->data = data; // Link the data field of newNode with data

newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode

temp = temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

/*

* Delete last node of the linked list

*/

void deleteLastNode()

struct node *toDelete, *secondLastNode;

if(head == NULL)

{
printf("List is already empty.");

else

toDelete = head;

secondLastNode = head;

/* Traverse to the last node of the list */

while(toDelete->next != NULL)

secondLastNode = toDelete;

toDelete = toDelete->next;

if(toDelete == head)

head = NULL;

else

/* Disconnect link of second last node with last node */

secondLastNode->next = NULL;
}

/* Delete the last node */

free(toDelete);

printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");

/*

* Display entire list

*/

void displayList()

struct node *temp;

/*

* If the list is empty i.e. head = NULL

*/

if(head == NULL)

{
printf("List is empty.");

else

temp = head;

while(temp != NULL)

printf("Data = %d\n", temp->data); // Print the data of current node

temp = temp->next; // Move to next node

OUTPUT:

Enter the total number of nodes: 5

Enter the data of node 1: 10

Enter the data of node 2: 20

Enter the data of node 3: 30

Enter the data of node 4: 40

Enter the data of node 5: 50

SINGLY LINKED LIST CREATED SUCCESSFULLY

Data in the list


Data = 10

Data = 20

Data = 30

Data = 40

Data = 50

Press 1 to delete last node: 1

SUCCESSFULLY DELETED LAST NODE OF LIST

Data in the list

Data = 10

Data = 20

Data = 30

Data = 40
PROBLEM - 4(C)

//write a program to delete an element at a specific position in the


linked list.
#include <stdio.h>

#include <stdlib.h>

/* Structure of a node */

struct node {

int data; // Data

struct node *next; // Address

} *head;

/* Functions used in program */

void createList(int n);

void deleteMiddleNode(int position);

void displayList();

int main()

int n, position;

/*

* Create a singly linked list of n nodes

*/

printf("Enter the total number of nodes: ");


scanf("%d", &n);

createList(n);

printf("\nData in the list \n");

displayList();

printf("\nEnter the node position you want to delete: ");

scanf("%d", &position);

/* Delete middle node from list */

deleteMiddleNode(position);

printf("\nData in the list \n");

displayList();

return 0;

/*

* Create a list of n nodes

*/

void createList(int n)

{
struct node *newNode, *temp;

int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*

* If unable to allocate memory for head node

*/

if(head == NULL)

printf("Unable to allocate memory.");

else

/*

* Read data of node from the user

*/

printf("Enter the data of node 1: ");

scanf("%d", &data);

head->data = data; // Link the data field with data

head->next = NULL; // Link the address field to NULL


temp = head;

/*

* Create n nodes and adds to linked list

*/

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */

if(newNode == NULL)

printf("Unable to allocate memory.");

break;

else

printf("Enter the data of node %d: ", i);

scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode

temp = temp->next;

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");

/*

* Delete middle node of the linked list

*/

void deleteMiddleNode(int position)

int i;

struct node *toDelete, *prevNode;

if(head == NULL)

printf("List is already empty.");

}
else

toDelete = head;

prevNode = head;

for(i=2; i<=position; i++)

prevNode = toDelete;

toDelete = toDelete->next;

if(toDelete == NULL)

break;

if(toDelete != NULL)

if(toDelete == head)

head = head->next;

prevNode->next = toDelete->next;

toDelete->next = NULL;
/* Delete nth node */

free(toDelete);

printf("SUCCESSFULLY DELETED NODE FROM MIDDLE OF LIST\n");

else

printf("Invalid position unable to delete.");

/*

* Display entire list

*/

void displayList()

struct node *temp;

/*

* If the list is empty i.e. head = NULL


*/

if(head == NULL)

printf("List is empty.");

else

temp = head;

while(temp != NULL)

printf("Data = %d\n", temp->data); // Print the data of current node

temp = temp->next; // Move to next node

OUTPUT:

Enter the total number of nodes: 5

Enter the data of node 1: 10

Enter the data of node 2: 20

Enter the data of node 3: 30

Enter the data of node 4: 40


Enter the data of node 5: 50

SINGLY LINKED LIST CREATED SUCCESSFULLY

Data in the list

Data = 10

Data = 20

Data = 30

Data = 40

Data = 50

Enter the node position you want to delete: 4

SUCCESSFULLY DELETED NODE FROM MIDDLE OF LIST

Data in the list

Data = 10

Data = 20

Data = 30

Data = 50
PROBLEM-5

//wap to create a stack using linked list and perform the following
operations
a) PUSH

b) POP

C) PEEK (DISPLAY)

#include <iostream>

using namespace std;

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

void push(int val) {

struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));

newnode->data = val;

newnode->next = top;

top = newnode;

void pop() {

if(top==NULL)

cout<<"Stack Underflow"<<endl;
else {

cout<<"The popped element is "<< top->data <<endl;

top = top->next;

void display() {

struct Node* ptr;

if(top==NULL)

cout<<"stack is empty";

else {

ptr = top;

cout<<"Stack elements are: ";

while (ptr != NULL) {

cout<< ptr->data <<" ";

ptr = ptr->next;

cout<<endl;

int main() {

int ch, val;

cout<<"1) Push in stack"<<endl;


cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;

switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(val);

break;

case 2: {

pop();

break;

case 3: {

display();

break;

case 4: {
cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;

}while(ch!=4);

return 0;

OUTPUT:

1) Push in stack

2) Pop from stack

3) Display stack

4) Exit

Enter choice: 1

Enter value to be pushed: 2

Enter choice: 1

Enter value to be pushed: 6

Enter choice: 1
Enter value to be pushed: 8

Enter choice: 1

Enter value to be pushed: 7

Enter choice: 2

The popped element is 7

Enter choice: 3

Stack elements are:8 6 2

Enter choice: 5

Invalid Choice

Enter choice: 4

Exit
PROBLEM -6(a)
//wap to implement the merge sort algorithm,

#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };

int b[10];

void merging(int low, int mid, int high) {

int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {

if(a[l1] <= a[l2])

b[i] = a[l1++];

else

b[i] = a[l2++];

while(l1 <= mid)

b[i++] = a[l1++];

while(l2 <= high)

b[i++] = a[l2++];
for(i = low; i <= high; i++)

a[i] = b[i];

void sort(int low, int high) {

int mid;

if(low < high) {

mid = (low + high) / 2;

sort(low, mid);

sort(mid+1, high);

merging(low, mid, high);

} else {

return;

int main() {

int i;

printf("List before sorting\n");


for(i = 0; i <= max; i++)

printf("%d ", a[i]);

sort(0, max);

printf("\nList after sorting\n");

for(i = 0; i <= max; i++)

printf("%d ", a[i]);

OUTPUT:

List before sorting

10 14 19 26 27 31 33 35 42 44 0

List after sorting

0 10 14 19 26 27 31 33 35 42 44
PROBLEM -6(b)
//wap to implement the quick sort algorithm,

#include <stdio.h>

void quicksort (int [], int, int);

int main()

int list[50];

int size, i;

printf("Enter the number of elements: ");

scanf("%d", &size);

printf("Enter the elements to be sorted:\n");

for (i = 0; i < size; i++)

scanf("%d", &list[i]);

quicksort(list, 0, size - 1);

printf("After applying quick sort\n");

for (i = 0; i < size; i++)

printf("%d ", list[i]);

printf("\n");
return 0;

void quicksort(int list[], int low, int high)

int pivot, i, j, temp;

if (low < high)

pivot = low;

i = low;

j = high;

while (i < j)

while (list[i] <= list[pivot] && i <= high)

i++;

while (list[j] > list[pivot] && j >= low)

j--;

if (i < j)
{

temp = list[i];

list[i] = list[j];

list[j] = temp;

temp = list[j];

list[j] = list[pivot];

list[pivot] = temp;

quicksort(list, low, j - 1);

quicksort(list, j + 1, high);

OUTPUT:

Enter the number of elements: 6

Enter the elements to be sorted:

67

45

24

98

12
38

After applying quick sort

12 24 38 45 67 98
PROBLEM - 7(a)
//wap to implement insertion sort algorithm.

#include <stdio.h>

int main()

int n, i, j, temp;

int arr[64];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

for (i = 1 ; i <= n - 1; i++)

j = i;

while ( j > 0 && arr[j-1] > arr[j])

temp = arr[j];
arr[j] = arr[j-1];

arr[j-1] = temp;

j--;

printf("Sorted list in ascending order:\n");

for (i = 0; i <= n - 1; i++)

printf("%d\n", arr[i]);

return 0;

OUTPUT:

Enter number of elements

Enter 6 integers

461253

Sorted list in ascending order:

4
5

6
PROBLEM - 7(b)
//wap to implement bubble sort algorithm.

#include <stdio.h>

/*

* Main Function

*/

int main()

int n, j, i, swap;

printf("Enter number of elements\n");

scanf("%d", &n);

int array[n];

printf("Enter %d integers\n", n);

for (i= 0; i < n; i++)

scanf("%d", &array[i]);

for (i = 0 ; i < n - 1; i++)

for (j = 0 ; j < n - i- 1; j++)

if (array[j] > array[j+1])


{

swap = array[j];

array[j] = array[j+1];

array[j+1] = swap;

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)

printf("%d\n", array[i]);

return 0;

OUTPUT:

1. Enter number of elements

Enter 6 integers

2
5

Sorted list in ascending order:

2. Enter number of elements

Enter 3 integers

-3

31

66

Sorted list in ascending order:

-3

31

66

3. Enter number of elements


5

Enter 5 integers

Sorted list in ascending order:

9
PROBLEM - 7(c)
//wap to implement selection sort algorithm

#include <stdio.h>

void selectionSort(int arr[], int size);

void swap(int *a, int *b);

/*

* Selection sort function

*/

void selectionSort(int arr[], int size)

int i, j;

for (i = 0 ; i < size;i++)

for (j = i ; j < size; j++)

if (arr[i] > arr[j])

swap(&arr[i], &arr[j]);

/* Function to swap two variables */

void swap(int *a, int *b)


{

int temp;

temp = *a;

*a = *b;

*b = temp;

/*

* Main Function

*/

int main()

int array[10], i, size;

printf("How many numbers you want to sort: ");

scanf("%d", &size);

printf("\nEnter %d numbers\t", size);

printf("\n");

for (i = 0; i < size; i++)

scanf("%d", &array[i]);

selectionSort(array, size);

printf("\nSorted array is ");

for (i = 0; i < size;i++)

printf(" %d ", array[i]);


return 0;

OUTPUT:

1. How many numbers you want to sort: 6

Enter 6 numbers

Sorted array is 1 2 3 4 5 6

2. How many numbers you want to sort: 3

Enter 3 numbers

-3

31

66

Sorted array is -3 31 66
3. How many numbers you want to sort: 5

Enter 5 numbers

Sorted array is 1 3 6 8 9
PROBLEM - 8
//wap a c program that uses a stack operations to convert a given infix
expression into its postfix equivalent.

#include<stdio.h>

char stack[20];

int top = -1;

void push(char x)

stack[++top] = x;

char pop()

if(top == -1)

return -1;

else

return stack[top--];

int priority(char x)

if(x == '(')
return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

main()

char exp[20];

char *e, x;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

if(isalnum(*e))

printf("%c",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

{
while((x = pop()) != '(')

printf("%c", x);

else

while(priority(stack[top]) >= priority(*e))

printf("%c",pop());

push(*e);

e++;

while(top != -1)

printf("%c",pop());

OUTPUT:

Enter the expression :: (a+b)*c+(d-a)

ab+c*da-+

You might also like