DS Lab MANUAL
DS Lab MANUAL
DS Lab MANUAL
EC8381
FUNDAMENTALS OF DATA STRUCTURES IN C
LABORATARY
Prepared By
Dr.A.SOLAIRAJ, ASP/CSE
1
SYLLABUS
1
Basic C Programs – looping, data manipulations, arrays
2
Programs using strings – string function implementation
3
Programs using structures and pointers
4
Programs involving dynamic memory allocations
5
Array implementation of stacks and queues
6
Linked list implementation of stacks and queues
7
Application of Stacks and Queues
8
Implementation of Trees, Tree Traversals
9
Implementation of Binary Search trees
10
Implementation of Linear search and binary search
2
EX.NO:1 Basic C Programs – looping, data manipulations, arrays
FIND GREATEST OR BIGGEST NUMBER
AIM
Develop a ‘C’ program to find the greatest of ’N’ numbers or biggest number stored in an array
PROGRAM
#include <stdio.h>
int main()
{
int i, n;
int arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
printf("Enter number%d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 1; i < n; ++i)
{
if (arr[0] < arr[i])
arr[0] = arr[i];
}
return 0;
}
SAMPLE OUTPUT
3
CALCULATOR USING SWITCH CASE
AIM
Develop a ‘C’ program to implement a simple calculator using switch case statement
PROGRAM
#include <stdio.h>
int main()
{
char operator;
int first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%d %d", &first, &second);
switch (operator)
{
case '+':
printf("%d + %d = %d", first, second, first + second);
break;
case '-':
printf("%d + %d = %d", first, second, first - second);
break;
case '*':
printf("%d + %d = %d", first, second, first * second);
break;
case '/':
printf("%d + %d = %d", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
SAMPLE OUTPUT
Enter an operator (+, -, *,): +
Enter two operands: 2
6
2+6=8
4
SUM OF DIGITS USING WHILE
AIM
Develop a ‘C’ program to find the sum of the digits of a given number using while statement
PROGRAM
#include<conio.h>
void main()
{
intn,digit, n1,sum=0;
clrscr();
printf("Enter the Any Integer No: ");
scanf("%d",&n);
n1=n;
while(n!=0)
{
digit = n%10;
sum+= digit;
n/=10;
}
printf("number is = %d \n",n1);
printf("Sum of digit = %d ",sum);
getch();
}
SAMPLE OUTPUT
Enter the Any Integer No: 234
number is = 234
Sum of digit = 9
AIM
Develop a ‘C’ program using function to compute the factorial of a given number
PROGRAM
#include<stdio.h>
int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %d", n, multiplyNumbers(n));
5
return 0;
}
int multiplyNumbers(int n)
{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
SAMPLE OUTPUT
Factorial of 6 = 720
AIM
PROGRAM
#include<stdio.h>
int main()
{
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %d\n", first);
printf("After swapping, secondNumber = %d", second);
return 0;
}
SAMPLE OUTPUT
6
PROGRAM FOR ASCENDING ORDER
AIM
Write a program in C to sort the numbers in ascending order
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,t,a[10];
clrscr();
printf("enter the n values\n");
scanf("%d",&n);
printf("enter the %d number\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if (a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("the ascending order is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
SAMPLE OUTPUT
enter the max values limit
5
enter the 5 number
25634
the ascending order is
23456
7
ADDITION OF TWO MATRICES
AIM
Write a program in C to find the addition of two matrices
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],sum[5][5],i,j,r,c;
clrscr();
printf("ENTER THE ROW SIZE\n");
scanf("%d",&r);
printf("ENTER THE COLOUMN SIZE\n");
scanf("%d",&c);
printf("ENTER THE VLAUES FOR %d X %d FIRST MATRIX \n",r,c);
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
scanf("%d",&a[i][j]);
}
printf("ENTER THE VLAUES FOR %d X %d SECOND MATRIX \n",r,c);
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
scanf("%d",&b[i][j]);
}
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("THE RESULTANT %d X %d MATRIX ADDITION IS\n\n",r,c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n\n");
}
8
getch();
}
SAMPLE OUTPUT
2 4
6 8
RESULT
Thus the C program for basic concepts isexecuted successfully and the output is verified.
9
EX.NO:2 Programs using strings – string function implementation
STRING PALINDROME
AIM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[15],b[15];
clrscr();
printf("enter the string\n");
scanf("%s",&a);
strcpy(b,a);
strrev(a);
if(strcmp(a,b)==0)
{
printf("the given string is palindrome\n");
}
else
{
printf("the given string is not a palindrome\n");
}
getch();
}
SAMPLE OUTPUT
Thus the C program for string manipulation is executed successfully and the output is verified.
10
EX.NO:3 Programs using structures and pointers
AIM
PROGRAM
#include<stdio.h>
struct complex
{
float real;
float imaginary;
};
int main()
{
struct complex cnum1, cnum2, sum;
printf("Enter real and imaginary part of first complex number:\n");
scanf("%f%f", &cnum1.real, &cnum1.imaginary);
printf("Enter real and imaginary part of second complex number:\n");
scanf("%f%f", &cnum2.real, &cnum2.imaginary);
sum.real = cnum1.real + cnum2.real;
sum.imaginary = cnum1.imaginary + cnum2.imaginary;
return 0;
}
SAMPLE OUTPUT
11
CALL BY REFERENCE
AIM
Write a program in C to swap two numbers using call by reference
PROGRAM
#include <stdio.h>
void swap(int *n1, int *n2);
int main()
{
int num1,num2;
clrscr();
printf("Enter the first number\n");
scanf("%d",&num1);
printf("Enter the second number\n");
scanf("%d",&num2);
printf("\n\nTHE NUMBERS LOCATION BEFORE SWAPPING\n\n");
printf("Number1 = %d\n", num1);
printf("Number2 = %d", num2);
swap(&num1, &num2);
printf("\n\nTHE NUMBERS LOCATION AFTER SWAPPING\n\n");
printf("Number1 = %d\n", num1);
printf("Number2 = %d", num2);
getch();
return 0;
}
void swap(int * n1, int * n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
SAMPLE OUTPUT
Enter the first number
25
Enter the second number
75
THE NUMBERS LOCATION BEFORE SWAPPING
Number1 = 25
Number2 = 75
THE NUMBERS LOCATION AFTER SWAPPING
Number1 = 75
Number2 = 25
RESULT
Thus the C program for structures and pointers are executed successfully and the output is
verified.
12
EX.NO:4 Programs involving dynamic memory allocations
FIND THE LARGEST NUMBER
AIM
Write a program in C to find the largest number using dynamic memory allocation.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
data = (float *)calloc(num, sizeof(float));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < num; ++i)
{
printf("Enter Number %d: ", i + 1);
scanf("%d", data + i);
}
for (int i = 1; i < num; ++i) {
if (*data < *(data + i))
*data = *(data + i);
}
printf("Largest number = %d", *data);
return 0;
}
SAMPLE OUTPUT
13
FIND THE SMALLEST NUMBER
AIM
Write a program in C to find the smallest number using dynamic memory allocation
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
data = (float *)calloc(num, sizeof(float));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < num; ++i)
{
printf("Enter Number %d: ", i + 1);
scanf("%d", data + i);
}
for (int i = 1; i < num; ++i) {
if (*data > *(data + i))
*data = *(data + i);
}
printf("Smallest number = %d", *data);
return 0;
}
SAMPLE OUTPUT
14
SUM OF ELEMENTS
AIM
Write a C program to read a one dimensional array, print sum of all elements along with input as array
elements using Dynamic Memory Allocation.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num, i, *ptr, sum = 0;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: \n");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
getch();
}
SAMPLE OUTPUT
Thus the dynamic memory allocation is successfully executed and the output is verified
15
EX.NO:5 Array implementation of stacks and queues
AIM
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int Data;
struct Node *next;
}
*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
16
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main()//int argc, char *argv[])
{
int i=0;
top=NULL;
clrscr();
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Stack: ");
scanf("%d",&value); push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
17
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
SAMPLE OUTPUT
AIM
Develop a ‘C’ program for implementing a Queue using arrays. Illustrate the operations that can
be performed on the same.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
}
case 2:
{
if(front==rear)
19
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
}
case 3:
{
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
}
case 4:
exit(0);
default:
{
printf("Wrong Choice: please see the options");
}
}
}
}
getch();
}
SAMPLE OUTPUT
21
EX.NO:6 Linked list implementation of stacks and queues
LINKED LIST IMPLEMENTATION OF STACK
AIM
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int Data;
struct Node *next;
}
*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
22
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main()//int argc, char *argv[])
{
int i=0;
top=NULL;
clrscr();
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Stack: ");
scanf("%d",&value); push(value);
display();
break;
23
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
SAMPLE OUTPUT
1. Push to stack
2. Pop from Stack
3. Display data of Stack
4. Exit
Choose Option: 1
Enter a value to push into Stack: 5
Elements are as:
5
Choose Option: 1
Enter a value to push into Stack: 3
24
Elements are as:
3
5
Choose Option: 1
Enter a value to push into Stack: 2
Elements are as:
2
3
5
Choose Option: 3
Elements are as:
2
3
5
Choose Option: 2
Elements are as:
3
5
Choose Option: 4
AIM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}
*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
25
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data); var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
front=NULL;
clrscr();
26
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
SAMPLE OUTPUT
27
1. Push to Queue
2. Pop from Queue
3. Display Data of Queue
4. Exit
Choose Option: 1
Enter a value to push into Queue: 5
Elements are as: 5
Choose Option: 1
Enter a value to push into Queue: 3
Elements are as: 5 3
Choose Option: 1
Enter a value to push into Queue: 2
Elements are as: 5 3 2
Choose Option: 2
Elements are as: 3 2
Choose Option: 4
RESULT
Thus the C program for linked list implementation of stack and queue is executed successfully
and the output is verified.
28
EX.NO:7 APPLICATIONS OF STACK AND QUEUE
PROGRAM
#include<stdio.h>
#include<conio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
void main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
clrscr();
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
31
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
getch();
}
SAMPLE OUTPUT
Enter the expression: 2 3 4 + * 5 *
The result of expression 2 3 4 + * 5 * = 70
Enter the expression: 4 5 + 7 2 - *
The result of expression 4 5 + 7 2 - * = 45
32
EX.NO:8 IMPLEMENTATION OF TREES, TREE TRAVERSALS
INORDER TRAVERSAL
AIM
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printInorder(struct node* node)
{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
int main()
{
struct node *root = newNode(5);
root->left = newNode(2);
root->right = newNode(4);
root->left->left = newNode(3);
root->left->right = newNode(6);
root->right->left = newNode(8);
root->right->right= newNode(10);
clrscr();
33
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
getch();
return 0;
}
SAMPLE OUTPUT
34
EX.NO:9 IMPLEMENTATION OF BINARY SEARCH TREES
AIM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;
}treeNode;
return NULL;
}
if(node->left)
return FindMin(node->left);
else
return node;
}
treeNode * Insert(treeNode *node,int data)
{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
temp -> left = temp -> right = NULL;
return temp;
}
if(data >(node->data))
{
node->right = Insert(node->right,data);
}
35
else if(data < (node->data))
{
node->left = Insert(node->left,data);
}
return node;
void main()
{
SAMPLE OUTPUT
-14->-1->3->5->6->8->9->10->
Value after deletion:
-14->3->6->8->9->10->
37
IMPLEMENTATION OF LINEAR SEARCH
EX.NO:10
AND BINARY SEARCH
LINEAR SEARCH
AIM
Develop a ‘C’ program for implementing linear search on a sorted array of ‘N’ numbers
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=1;i<=n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=1;i<=n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
getch ();
}
SAMPLE OUTPUT
38
BINARY SEARCH
AIM
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
clrscr();
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for(i=0;i<n;++i)
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
getch();
}
39
SAMPLE OUTPUT
40
EX.NO:11 IMPLEMENTATION OF SORTING
INSERTION SORT
AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using insertion sort.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,temp,a[30];
clrscr();
printf("Enter the max number of elements: ");
scanf("%d",&n);
printf("\nEnter the %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nSorted list is as follows\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
SAMPLE OUTPUT
BUBBLE SORT
AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using bubble sort
PROGRAM
#include<stdio.h>
#include<conio.h>
void bubble_sort(int [], int);
void main()
{
int array[30], n, c, d, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
}
void bubble_sort(int list[], int n)
{
int c, d, t;
for (c = 0 ; c < n - 1; c++)
{ for (d = 0 ; d < n - c - 1; d++)
{ if (list[d] > list[d+1])
{ t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
SAMPLE OUTPUT
42
Enter the number of elements: 4
Enter 4 integers
34
12
67
8
Sorted list in ascending order:
8
12
34
67
QUICK SORT
AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using quick sort
PROGRAM
#include<stdio.h>
#include<conio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
void main()
{
int a[50],n,i;
clrscr();
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void quick_sort(int a[],int p,int u)
{
int j;
if(p<u)
{
j=partition(a,p,u);
quick_sort(a,p,j-1);
quick_sort(a,j+1,u);
}
}
int partition(int a[],int p,int u)
{
int v,i,j,temp;
43
v=a[p];
i=p;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
while(i<j);
a[p]=a[j];
a[j]=v;
return(j);
}
SAMPLE OUTPUT
MERGE SORT
AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using merge sort.
PROGRAM
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
44
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[30],n,i;
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
45
SAMPLE OUTPUT
Enter no of elements: 4
Enter array elements:
12
89
45
37
Sorted array is: 12 37 45 89
46
IMPLEMENTATION OF HASH FUNCTIONS AND COLLISION
EX.NO:12
RESOLUTION TECHNIQUE
1-> Insert
2-> Delete
3-> Display
4-> Searching
5-> Exit
//-------LINEAR PROBING-------
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
void main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
clrscr() ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
50
printf ("Enter Elements: ");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
{
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
getch() ;
}
SAMPLE OUTPUT
51