Computer Programming Lab-I
Computer Programming Lab-I
Communication Engineering
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
Introduction of sorting methods Implementation of bubble, selection and insertion sort.
EXPERIMENT NO:-1
Object:- Introduction of sorting methods Implementation of bubble, selection and insertion sort.
Description:
Let A be an array of n elements. Sorting A refers to the operation of rearranging the elements of A so they are in
increasing order. i.e. so that,
A[1] < A[2] < A[3] < A[4] < ……. < A[N]
Bubble Sort:
Algorithm:
BUBBLESORT (A [MAX], N)
[BUBBLESORT is a procedure to arrange the elements of array A (0: N-1) in sorted order. Where N is the
number of elements insert in the array and MAX is the size of an array. ]
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[50],temp,n;
}
cout<<"\nThe sorted array is : \n";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
getch();
}
Selection Sort:
Algorithm:
min = a[i]
Swap (min,a[j])
2. Exit
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[50],temp,n;
cout<<"Enter the size of array : \n";
cin>>n;
cout<<"Enter the elements of array : \n";
for(int i=0;i<n;i++)
cin>>x[i];
for(i=0;i<n-1;i++)
{
int pos=i,min=x[i];
for(int j=i+1;j<n;j++)
{
if(min>x[j])
{
min=x[j];
pos=j;
}
}
temp=x[pos];
x[pos]=x[i];
x[i]=temp;
cout<<"\nArray after "<<i+1<<" phase is : \n";
for(int k=0;k<n;k++)
cout<<x[k]<<" ";
}
cout<<"\nThe sorted array is : \n";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
getch();
}
Insertion Sort:
Algorithm:
INSERTIONSORT (A [MAX], N)
[The procedure INSERTIONSORT is used to arrange the elements of array A (0: N-1) in sorted order.
Where N is the number of elements insert in the array and MAX is the size of an array.]
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[50],temp,n;
cout<<"Enter the size of array : \n";
cin>>n;
cout<<"Enter the elements of array : \n";
for(int i=0;i<n;i++)
cin>>x[i];
for(i=0;i<n-1;i++)
{
int j=i,y=x[i+1];
while(j>=0&&y<x[j])
{
x[j+1]=x[j];
j=j-1;
}
x[j+1]=y;
}
cout<<"\nThe sorted array is : \n";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
getch();
}
Output:
Given array is
12, 11, 13, 5, 6, 7
Sorted array is
5 6 7 11 12 13
Viva Questions:
1. what are the needs of bubble, selection and insertion sort
2. complexity of bubble sort, selection and insertion sort
3. In a selection sort of n elements, how many times is the swap function called in the complete execution of
the algorithm?
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
To search an element using Binary Search
EXPERIMENT NO:-2
Object:- To search an element using Binary Search
Description:
In Binary Search we use Divide & Conquer principle.
General principle of Divide & Conquer : If a problem is given we divide it into no. of sub-problems if we get
solution of each part then we stop at that point. Otherwise we still divide the problem as we solve all individual sub-
problems at last combine all these solution which gives solution of main problem.
Algorithm:
Step 1: if (low > high) then return -1
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
int a[10],n,i,j,temp;
int beg,end,mid,target;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
beg=0;
end=n;
mid=(beg+end)/2;
scanf("%d",&target);
if(target<a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
if(a[mid]==target)
else
getch();
OutPut:
Enter the total numbers:5
1. Define array.
2. If there is more than one position of item is exist, then what will be the position of item?
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
Write a program to search an element using linear search
EXPERIMENT NO:-3
Object: Write a program to search an element using linear search
1. Set k: = 1 & loc: = 0
2. Repeat step 3 & 4 while loc: = 0 &k < = n
3. If (item = data[k])
loc: = k
Else
K=k+1
4. If loc: = 0, then
Print “no. not found” Else
Print “loc is the location of item”
5.Exit
Source Code:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
Output:
Enter the no of elements: 6
Position of an element: 3
Viva questions:
1. Define array.
2. If there is more than one position of item is exist, then what will be the position of item?
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
Write a program for addition, multiplication and transpose of matrices.
EXPERIMENT NO:-4
Algorithm
Matmul(a,b,m,n,p)
1 for(i=1 to m)
2 for(j = 1 to p)
3 c[i][j] =0;
4 for(k= 1to n)
5 c[i][j] = c[i][j]+a[i][j]*b[i][j]
6 exit
Output:
Enter the first matrix
356
111
456
789
42 51 60
64 79 120
11 15 18
Algorithm
Matadd(a,b,m,n)
1 for (i=1 to m
2 for(j= 1 to n)
3c[i][j] = a[i][j]+b[i][j]
4 exit
Output:
Enter the first matrix
356
111
456
789
3 5 7
7 10 12
8 9 10
Algorithm
Transpose(a,m,n)
1 for(i= 1 to m) for(j= 1 to n) b[i][j]= a[j]
[i]
2 for (i=1to m) for (j= 1to n) a[i][j]= b[i]
[j]
Exit
Output:
Enter the first matrix
356
111
231
351
461
Viva Questions:
1. What is the condition for multiplication of two matrices?
2. Explain the matrix representation in memory?
3. What is the condition for addition of two matrices?
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
To sort an array using quick sort.
EXPERIMENT NO:-5
Description:
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given
array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways:
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot,
put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater
elements (greater than x) after x. All this should be done in linear time.
Partition Algorithm
There can be many ways to do partition, following code adopts the method given in CLRS book. The logic is simple,
we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if
we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.
Algorithm:
1. low =l, high = h, key a[(l+h)/2]
4. low = low +1
6. high = high – 1
a) temp = a[low]
b) a[low] = a[high]
e) high = high + 1
10. Exit
1. Top:= NULL
2. If N>1, then Top:= Top+1, Lower[1]:=N
3. Repeat steps 4 to 7 while Top!=NULL
4. [pop sublist from stacks]
Set low= Lower[Top], high:= Upper[Top], Top:= Top-1
5. return
Source Code:
#include<stdio.h>
int main(){
int x[20],size,i;
quicksort(x,0,size-1);
return 0;
}
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
Enter the no of elements 5
Sorted array 3 5 7 12 15
Viva Questions:
i. What is the average case, best case and worst case complexity for quick sort algorithm complexity of quick
sort?
ii. Difference between merge sort and quick sort.
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
Write a program to implement singly linked list
EXPERIMENT NO:-6
INSERTION BEGIN
1. t Æ next = start
2. start = t
Return
MIDDLE
LAST
1. p = start
2. Repeat step 3 until p Æ next NULL
3. p = p Æ next
4. t Æ next = NULL
5. p Æ next = t
6. Return
DELETION BEGIN
1. x = start
2. start = start Æ next
3. delnode(x)
MIDDLE
1. Enter the info of node to be deleted
2. Read n
3. p = start
4. c = start
5. while (c Æ info < > NULL)
p=c
c = c Æ next
6. p Æ next = c Æ next
7. delnode ( c )
8. Return
LAST
1. p = start c = start
2. while (cÆnext < > NULL)
p=c
c = cÆnext
3. p Æ next = c Æ next
4. delnode ( c)
5. Return
TRAVERSAL
1. p = start
Source code:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node *pointer, int data)
{
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int find(node *pointer, int key)
{
pointer = pointer -> next; //First node is dummy node.
/* Iterate through the entire linked list and search for the key. */
while(pointer!=NULL)
{
if(pointer->data == key) //key is found.
{
return 1;
}
printf("%d ",pointer->data);
print(pointer->next);
}
int main()
{
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Print\n");
printf("4. Find\n");
while(1)
{
int query;
scanf("%d",&query);
if(query==1)
{
int data;
scanf("%d",&data);
insert(start,data);
}
else if(query==2)
{
int data;
scanf("%d",&data);
delete(start,data);
}
else if(query==3)
{
printf("The list is ");
print(start->next);
printf("\n");
}
else if(query==4)
{
int data;
scanf("%d",&data);
int status = find(start,data);
if(status)
{
printf("Element Found\n");
}
else
{
printf("Element Not Found\n");
}
}
}
}
Output:
Enter the list: 12->100 13->101 14->104 15->NULL
Insert item at first position: 11
New list: 11->1000 12->100 13->101 14->104 15->NULL
Delete an item from last position:
Delete item: 15
New list: 11->1000 12->100 13->101 14->NULL
Viva Questions:
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
EXPERIMENT NO:-7
INSERTION BEGIN
1. If start = NULL
start = t
2. else
t Æ next = NULL
t Æ next Æ prev = t start = t
Return
MIDDLE
1. Print “ enter info of the node after which you want to insert”
2. Read x
3. p = start
4. Repeat while p< > NULL If (pÆ info =
n)
tÆnext = pÆ next pÆnext = t
t Æ prev = p
p Æ nextÆ prev = t
Return
Else
P = pÆ next
5. Print x not found
tÆnext = NULL
pÆnext = t
DELETION BEGIN
1. p = start
2. pÆnextÆprev = NULL
3. start = pÆnext
4. start = pÆnext
5. delnode(p)
6. Return
MIDDLE
1. Enter “info of the node to be deleted”
2. Read x
3. p = start
4. Repeat until p< > NULL
If(pÆinfo = x) pÆprevÆnext = pÆnext pÆ next
Æ prev = pÆprev delnode(p)
Return
Else
P = pÆ next
5. Print “x not found”
LAST
1. P = start
2. Repeat while p< > NULL If(pÆnext =
NULL)
Delnode(p)
3. Return
DISPLAY
1. p = start
2. Repeat while p < > NULL Print pÆinfo
P = p Æ next
Source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
return length;
}
//display the list in from first to last
void displayForward(){
//start from the beginning
struct node *ptr = head;
while(ptr != NULL){
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
//display the list from last to first
void displayBackward(){
//start from the last
struct node *ptr = last;
while(ptr != NULL){
//print data
printf("(%d,%d) ",ptr->key,ptr->data);
printf(" ]");
}
if(isEmpty()){
//make it the last link
last = link;
}else {
//update first prev link
head->prev = link;
}
//point it to old first link
link->next = head;
if(isEmpty()){
//make it the last link
last = link;
}else {
//make link a new last link
last->next = link;
//mark old last node as prev of new link
link->prev = last;
}
//point last to new last node
last = link;
}
//delete first item
struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;
last = NULL;
}else {
head->next->prev = NULL;
}
head = head->next;
//return the deleted link
return tempLink;
}
//delete link at the last location
struct node* deleteLast(){
//save reference to last link
struct node *tempLink = last;
last = last->prev;
if(current->next == NULL){
return NULL;
}else {
//store reference to current link
previous = current;
head = head->next;
}else {
//bypass the current link
current->prev->next = current->next;
}
if(current == last){
//change last to point to prev link
last = current->prev;
}else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data){
//start from the first link
struct node *current = head;
//create a link
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = key;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
}else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
Viva Questions:
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
EXPERIMENT NO:-8
Object: - write a program to implement queue using array and linked list
CREATE
1. t = new node
2. Enter info to be inserted
3. Read n
4. t Æ info = n
5. t Æ next = front
6. front = t
INSERTION
1. r Æ next = t
2. t Æ next = NULL
3. Return
DELETION
1. x = front
2. front = front Æ next
3. delnode(x)
4. Return
DISPLAY
1. If (front = NULL)
Print “ empty queue” Return
Else
P = start
Repeat until (p< > NULL) Print p Æ info
P = pÆ next
Return
Output:
Enter the list: 107<-12->100 1000<-13->101 100<-14->104 101<-15->NULL
Insert item at first position: 11
New list: 108<-11->1000 107<-12->100 1000<-13->101 100<-14->104
101<-15->NULL
Delete an item from last position:
Delete item: 15
New list: 108<-11->1000 107<-12->100 1000<-13->101 100<-14-> NULL
Viva Questions:
1. What is queue?
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
Write a program to implement stack using array and linked list
EXPERIMENT NO:-10
Object: - Write a program to implement stack using array and linked list
Using array:
INSERTION PUSH(item)
1. If (item = max of stack) Print
“overflow”
Return
2. top = top + 1
3. stack[top] = item
4. Return
DELETION POP(item)
1. If (top = - 1)
Print “underflow” Return
2. Item = stack[top]
3. top = top – 1
4. Return
DISPLAY
1. If top = - 1
Print “underflow”
PUSH( )
1. t = newnode( )
2. Enter info to be inserted
3. Read n
4. tÆinfo = n
5. tÆnext = top
6. top = t
7. Return
POP( )
1. If (top = NULL) Print “
underflow” Return
2. x = top
3. top = top Æ next
4. delnode(x)
5. Return
Output:
Enter the no of elements: 5
Enter the elements in stack: 5 10 15 16 17
New element: 9
New stack: 5 10 15 16 17
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create empty stack */
void create()
{
top = NULL;
}
/* Count stack elements */
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
/* Display stack elements */
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
/* Return top element */
int topelement()
{
return(top->info);
}
/* Check if stack is empty or not */
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
/* Destroy entire stack */
void destroy()
{
top1 = top;
while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
}
Output
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
No. of elements in stack : 3
Enter choice : 8
Viva Questions:
1. What is stack?
2. What are the difference between stack and queue?
LAB-MANUAL
(II Year III SEM ECE)
Experiment Object:-
Write a program to merge two sorted array
EXPERIMENT NO:-11
ENTER (a[10],n)
1. Repeat step 2 for i = 0 to (n-1)
2. Input a[i]
3. Return
DISPLAY(c[20],p)
1. Repeat step 2 for k = 0 to p-1
2. Print c[k]
3. Return
MAIN( )
1. Start
st nd
2. Input no. of elements in 1 & 2 array as ‘n’ & ‘m’
3. Enter (a.n)
4. Enter (b,m)
5. i = j = k = 0
6. Repeat step 7 to 12 while ((i < n)&&(j < m))
7. If (a[i] >= b[j]),goto step 9
8. c[k+1] = a[i+1]
9. If a[i] = b[j] ,goto step 11
10. c[k++] = b[j++]
goto step 7
11. c[k++] = a[i++]
12. j++
13. Repeat step 14 while (i<n)
14. c[k++] = a[i++]
15. Repeat step 16 while m > j
16. c[k++] = b[j++]
17. Display merged arrays as display(c;k)
18. Exit
#include <stdio.h>
int main() {
int a[100], b[100], m, n, c, sorted[200];
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
return 0;
}
j = k = 0;
Output:
Enter first array: 5 15 20 25
Enter first array: 3 10 11 45
After merging:
3 5 10 11 15 20 25 45
Viva Question:
1. What is an array?