Professional Documents
Culture Documents
DSAmerged
DSAmerged
EX NO:1(a)
IMPLEMENTATION OF BINARY TREE TRAVERSALS -
PREORDER TRAVERSAL
DATE:14.09.2022
AIM:
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root
of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-
separated values
*Input Format
Our test code passes the root node of a binary tree to the preorder function.
*Constraints
1 <= Nodes in the tree <=500
*Output Format
Print the tree's preorder traversal as a single line of space-separated values
*Sample Input
1
\
2
\
5
/ \
3 6
\
4
*Sample Output
125346
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
if(root == NULL)
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
else
root->left = cur;
else
root->right = cur;
return root;
if(root==NULL)
return root;
else
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
int main()
int t;
int data;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
preOrder(root);
return 0;
OUTPUT 1:
OUTPUT 2:
RESULT:
The program to find the preorder of the binary tree is compiled and executed successfully.
EX NO:1(b)
IMPLEMENTATION OF BINARY TREE TRAVERSALS –
INORDER TRAVERSAL
DATE:14.09.2022
AIM:
Complete theinorder function in your editor below, which has 1 parameter: a pointer to the root
of a binary tree. It must print the values in the tree's inorder traversal as a single line ofspace-
separated values.
*Input Format
Our hidden tester code passes the root node of a binary tree to your inorder function
*Constraints
1 <= Nodes in the tree <=500
*Output Format
Print the tree's inorder traversal as a single line of space-separated values.
*Sample Input
1
\
2
\
5
/ \
3 6
\
4
*Sample Output
123456
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
if(root == NULL)
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
else
root->left = cur;
else
root->right = cur;
return root;
if(root==NULL)
return root;
else
inOrder(root->left);
printf("%d ",root->data);
inOrder(root->right);
int main()
int t;
int data;
scanf("%d", &t);
scanf("%d", &data);
inOrder(root);
return 0;}
OUTPUT 1:
OUTPUT 2:
RESULT:
The program to find the inorder of the binary tree is compiled and executed successfully.
EX NO:1(c)
IMPLEMENTATION OF BINARY TREE TRAVERSALS –
POSTORDER TRAVERSAL
DATE:14.09.2022
AIM:
Complete the postorder function in your editor below, which has 1 parameter: a pointer to the
root of a binary tree. It must print the values in the tree's postorder traversal as a single line of
space-separated values.
*Input Format
Our hidden tester code passes the root node of a binary tree to your postorder function
*Constraints
1 <= Nodes in the tree <=500
*Output Format
Print the tree's postorder traversal as a single line of space-separated values.
*Sample Input
1
\
2
\
5
/ \
3 6
\
4
*Sample Output
436521
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
if(root == NULL)
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
else
root->left = cur;
else
root->right = cur;
return root;
if(root==NULL)
return root;
else
postOrder(root->left);
postOrder(root->right);
printf("%d ",root->data);
int main()
int t;
int data;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
postOrder(root);
return 0;
OUTPUT 1:
OUTPUT 2:
RESULT:
The program to find the postorder of the binary tree is compiled and executed successfully.
EX NO:1(d)
IMPLEMENTATION OF BINARY TREE TRAVERSALS –
HEIGHT OF A BINARY TREE
DATE:14.09.2022
AIM:
Complete the getHeight or height function in the editor. It must return the height of a binary tree
as an integer.
Note -The Height of binary tree with single node is taken as zero.
*Input Format
The first line contains an integer n, the number of nodes in the tree.
Next line contains n space-separated integer where 𝑖 𝑡ℎ integer denotes node[i].data.
*Constraints
1<=nodedata[i]<=20
1<=n<=20
*Output Format
Your function should return a single integer denoting the height of the binary tree.
*Sample Input
*Sample Output
3
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
if(root == NULL)
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
else
root->left = cur;
else
root->right = cur;
return root;
if(root==NULL)
return -1;
else
int lht=getHeight(root->left);
int rht=getHeight(root->right);
if(lht>rht)
return (lht+1);
else
return (rht+1);
int main()
int t;
int data;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
printf("%d",getHeight(root));
return 0;
OUTPUT 1:
OUTPUT 2:
RESULT
The program to find the height of the binary tree is compiled and executed successfully
EX NO:1(e)
IMPLEMENTATION OF BINARY TREE TRAVERSALS –
LEVEL ORDER TRAVERSAL
DATE:14.09.2022
AIM:
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree.
In level-order traversal, nodes are visited level by level from left to right. Complete the function
level-order and print the values in a single line separated by a space.
*Input Format
}
*Constraints
1<=Nodes in the tree <=500
*Output Format
Print the values in a single line separated by a space.
*Sample Input
1
\
2
\
5
/ \
3 6
\
4
*Sample Output
125364
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
if(root == NULL)
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
else
root->left = cur;
else
root->right = cur;
return root;
if(root==NULL)
return 0;
else
int lht=getHeight(root->left);
int rht=getHeight(root->right);
if(lht>=rht)
return(lht+1);
else
return(rht+1);
if(root==NULL)
return ;
if(lvl==1)
printf("%d ",root->data);
else if(lvl>1)
currentlvl(root->left,lvl-1);
currentlvl(root->right,lvl-1);
int h=getHeight(root);
int i;
for(i=1;i<=h;i++)
currentlvl(root,i);
int main()
int t;
int data;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
levelOrder(root);
return 0;
OUTPUT :
RESULT:
The program to print the level-order of the binary tree is compiled and executed successfully.
EX NO:2(a)
IMPLEMENTATION OF BINARY SEARCH TREE –
INSERTION ON BINARY SEARCH TREE
DATE:21.09.2022
AIM:
You are given a pointer to the root of a binary search tree and values to be inserted into the tree.
Insert the values into their appropriate position in the binary search tree and return the root of the
updated binary tree. You just have to complete the function.
*Input Format
You are given a function,
Node * insert (Node * root ,int data) {
}
*Constraints
*Output Format
Return the root of the binary search tree after inserting the value into the tree.
*Sample Input
4
/\
2 7
/\
1 3
The value to be inserted is 6
*Sample Output
4
/ \
2 7
/\ /
1 36
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
return;
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
if(root==NULL)
root->data=data;
root->left=NULL;
root->right=NULL;
return root;
else
if(data<root->data)
cur=insert(root->left,data);
root->left=cur;
else
cur=insert(root->right,data);
root->right=cur;
return root;
int main()
int t;
int data;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
preOrder(root);
return 0;
OUTPUT:
RESULT:
The program to insert the values in binary search tree is compiled and executed successfully.
EX NO:2(b)
IMPLEMENTATION OF BINARY SEARCH TREE –
LOWEST COMMON ANCESTORS
DATE:21.09.2022
AIM:
You are given pointer to the root of the binary search tree and two values V1 and V2. You need
to return the lowest common ancestor (LCA) of V1 and V2 in the binary search tree
In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3.Node 3 is
the lowest node which has nodes 4 and 6 as descendants.
*Function Description
Complete the function lca in the editor below. It should return a pointer to the lowest common
ancestor node of the two values given.
*Input Format
The first line contains an integer, n, the number of nodes in the tree.
The second line contains n space-separated integers representing node.data values.
The third line contains two space-separated integers, V1 and V2.
To use the test data, you will have to create the binary search tree yourself. Here on the platform,
the tree will be created for you
*Constraints
V1!= V2
*Output Format
Return the a pointer to the node that is the lowest common ancestor of V1 and V2.
Sample Input
6
423176
17
Sample Output
[reference to node 4]
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
if(root == NULL)
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
else
root->left = cur;
else
root->right = cur;
return root;
while(root!= NULL)
root = root->right;
else
break;
return root;
int main()
int t;
int data;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
int v1;
int v2;
printf("%d", ans->data);
return 0;
OUTPUT:
RESULT:
The program to find the lowest common ancestors of the nodes are compiled and executed
successfully.
EX NO:2(c)
IMPLEMENTATION OF BINARY SEARCH TREE –
DELETION ON BINARY SEARCH TREE
DATE:21.09.2022
AIM:
Given a root node reference of a BST and a key, delete the node with the given key in the BST.
Return the root node reference (possibly updated) of the BST.
Search for a node to remove. If the node is found, delete the node.
*Input Format
*Constraints
The number of nodes in the tree is in the range [0, 104]. -105 <= Node->val <= 105 Each node
has a unique value. root is a valid binary search tree. -105 <= key <= 105
*Output Format
Return the root of the binary search tree after inserting the value into the tree.
*Sample Input
6
536247
3
*Sample Output
54267
PROGRAM:
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node
int data;
};
return 0;
else{
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
if (!root)
n->data=data;
n->left=NULL;
n->right=NULL;
root=n;
return root;
else {
return root;
current = current->left;
return current;
if (root == NULL)
return root;
else {
if (root->left == NULL)
free(root);
return temp;
free(root);
return temp;
root->data= temp->data;
return root;
int main()
int t;
int data,key;
scanf("%d", &t);
while(t-- > 0)
scanf("%d", &data);
scanf("%d",&key);
deleteNode(root,key);
preOrder(root);
return 0;
OUTPUT:
RESULT:
The program to delete a node from the binary search tree is compiled and executed successfully.
EX NO:
IMPLEMENTATION OF EXPRESSION TREE
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree *left;
struct tree *right;
};
int top=-1;
struct tree *stack[20];
void operand(char a)
{
struct tree *n=(struct tree*)malloc(sizeof(struct tree));
n->data=a;
n->right=NULL;
n->left=NULL;
push(n);
}
void operator1(char a)
{
struct tree *n1=(struct tree*)malloc(sizeof(struct tree));
n1->data=a;
n1->right=pop();
n1->left=pop();
push(n1);
void main()
{
int i;
char s[20];
printf("Enter postfix expression:");
//scanf("%s",&s);
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')
operator1(s[i]);
else
operand(s[i]);
}
inorder(stack[top]);
}
OUTPUT:
RESULT:
EX NO:
MAX-HEAP IMPLEMENTATION USING PRIORITY QUEUE
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int size = 0;
void swap(int *a, int *b) {
int temp = *b;
*b = *a;
*a = temp;
}
if (largest != i)
{
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
}
if (size == 0)
{
array[0] = newNum;
size += 1;
}
else
{
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
}
int main()
{
int array[10];
int n,x,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&x);
insert(array,x);
}
printf("Max-Heap array: ");
printArray(array, size);
OUTPUT:
RESULT:
EX NO:
MIN-HEAP IMPLEMENTATION USING PRIORITY QUEUE
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int size = 0;
void swap(int *a, int *b) {
int temp = *b;
*b = *a;
*a = temp;
}
if (largest != i)
{
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
printf("\n");
}
int main()
{
int array[10];
int n,x,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&x);
insert(array,x);
}
printf("Min-Heap array: ");
printArray(array, size);
OUTPUT:
RESULT:
EX NO:
IMPLEMENTATION OF AVL TREE
DATE:
AIM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
// Create Node
struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};
int max(int a, int b);
// Calculate height
int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
}
// Create a node
struct Node *newNode(int key) {
struct Node *node = (struct Node *)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
}
// Right rotate
struct Node *rightRotate(struct Node *y) {
x->right = y;
y->left = T2;
return x;
}
// Left rotate
struct Node *leftRotate(struct Node *x) {
struct Node *y = x->right;
struct Node *T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
// Insert node
struct Node *insertNode(struct Node *node, int key) {
// Find the correct position to insertNode the node and insertNode it
if (node == NULL)
return (newNode(key));
return node;
}
return current;
}
// Delete a nodes
struct Node *deleteNode(struct Node *root, int key) {
// Find the node and delete it
if (root == NULL)
return root;
else {
if ((root->left == NULL) || (root->right == NULL)) {
struct Node *temp = root->left ? root->left : root->right;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->right);
root->key = temp->key;
if (root == NULL)
return root;
return root;
}
int main()
{
struct Node *root = NULL,x;
for(int i=0;i<n;i++)
{
Scanf(“%d”,&x);
Root=insertNode(root,x);
}
printPreOrder(root);
return 0;
}
OUTPUT:
RESULT:
The program to implement AVL Tree insertion and deletion is compiled and executed
successfully.
EX NO:
IMPLEMENTATION OF HASHING-LINEAR PROBING
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m,n,i;
printf("Enter the Table size:");
scanf("%d",&m);
printf("Enter the number of elements less than or equal to %d:",m);
scanf("%d",&n);
int keys[n];
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&keys[i]);
}
int table[m],prob[m];
for (i=0;i<m;i++)
{
table[i]=0;
prob[i]=0;
}
int size_of_key=sizeof(keys)/sizeof(keys[0]);
for (i=0;i<size_of_key;i++)
{
int factor=keys[i]%m;
int fact=1;
int inc=0;
int p=1;
while(fact)
{
int f=(factor+inc)%m;
if (table[f]==0)
{
table[f]=keys[i];
prob[f]=p;
fact=0;
}
else
{
inc++;
p++;
}
}
}
printf("Index\tKey\tProbing\n");
for (i=0;i<m;i++)
{
printf("%d\t%d\t%d\n",i,table[i],prob[i]);
}
}
OUTPUT:
RESULT:
The program to implement hashing using linear probing is compiled and executed successfully.
EX NO:
IMPLEMENTATION OF HASHING-QUADRATIC PROBING
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m,n,i;
printf("Enter the Table size:");
scanf("%d",&m);
printf("Enter the number of elements less than or equal to %d:",m);
scanf("%d",&n);
int keys[n];
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&keys[i]);
}
int table[m],prob[m];
for (int i=0;i<m;i++)
{
table[i]=0;
prob[i]=0;
}
int size_of_key=sizeof(keys)/sizeof(keys[0]);
for (int i=0;i<size_of_key;i++)
{
int factor=keys[i]%m;
int fact=1;
int inc=0;
int p=1;
while(fact)
{
int f=(factor+(inc*inc))%m;
if (table[f]==0)
{
table[f]=keys[i];
prob[f]=p;
fact=0;
}
else
inc++;
p++;
}
}
printf("Index\tKey\tProbing\n");
for (int i=0;i<m;i++)
{
printf("%d\t%d\t%d\n",i,table[i],prob[i]);
}
}
OUTPUT:
RESULT:
The program to implement hashing using quadratic probing is compiled and executed
successfully.
EX NO:
IMPLEMENTATION OF HASHING-SEPARATE CHAINING
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
temp->next = newNode;
}
}
void main()
{
int key,m,n,i;
printf("Enter the Table size:");
scanf("%d",&m);
struct node *chain[m];
//init array of list to NULL
init(m,chain);
printf("Enter the number of elements less than or equal to %d:",m);
scanf("%d",&n,chain);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&key);
insert(key,m,chain);
}
print(m,chain);
}
OUTPUT:
RESULT:
The program to implement hashing using separate chaining is compiled and executed
successfully.
EX NO:
IMPLEMENTATION OF GRAPH TRAVERSAL - DEPTH FIRST SEARCH
DATE:
AIM:
PROGRAM:
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
OUTPUT:
RESULT:
The program to implement graph traversal using breath first search is compiled and executed
successfully.
EX NO:
IMPLEMENTATION OF GRAPH TRAVERSAL – BREATH FIRST SEARCH
DATE:
AIM:
PROGRAM:
#include <stdio.h>
void bfs(int v)
{
for (i = 1; i <= n; i++)
if (adj[v][i] && !visited[i])
queue[++rear] = i;
if (front <= rear)
{
visited[queue[front]] = 1;
bfs(queue[front++]);
}
}
void main()
{
int v;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
queue[i] = 0;
visited[i] = 0;
}
printf("Enter graph data in matrix form: \n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &adj[i][j]);
printf("Enter the starting vertex: ");
scanf("%d", &v);
bfs(v);
printf("The node which are reachable are: \n");
for (i = 1; i <= n; i++)
if (visited[i])
printf("%d\t", i);
else
printf("BFS is not possible. Not all nodes are reachable");
return 0;
}
OUTPUT:
RESULT:
The program to implement graph traversal using depth first search is compiled and executed
successfully.
EX NO:
IMPLEMENTATION OF KRUSKAL’S ALGORITHM
DATE:
AIM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
printf("\n\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
OUTPUT:
RESULT:
EX NO:
IMPLEMENTATION OF FLOYD-WARSHALL ALGORITHM
DATE:
AIM:
PROGRAM:
#include <stdio.h>
#define INF 99999
int V;
void main()
{
printf("Enter the number of vertices:");
scanf("%d",&V);
int i,j,k,graph[V][V];
printf("ENTER THE COST FOR THE GRAPH");
printf("\nIF THERE IS NO PATH ENTER 9999:\n");
for (int i=0;i<V;i++){
for(int j=0;j<V;j++){
scanf("%d",&graph[i][j]);
}
}
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (graph[i][k] + graph[k][j] < graph[i][j])
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
printf("The following matrix shows the shortest distances between every pair of vertices \n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (graph[i][j] == INF)
printf("\tINF");
else
printf("\t%d",graph[i][j]);
}
printf("\n");
}
}
OUTPUT:
RESULT:
EX NO:
IMPLEMENTATION OF MERGE SORT
DATE:
AIM:
PROGRAM:
#include <stdio.h>
{
arr[k] = M[j];
j++;
}
k++;
}
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Driver program
int main()
{
int n,i;
printf("Enter the number of elements:");
scanf("%d",&n);
int arr[n];
printf("Enter the %d elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
OUTPUT:
RESULT:
EX NO:
IMPLEMENTATION OF QUICK SORT
DATE:
AIM:
PROGRAM:
#include<stdio.h>
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("Enter some elements (Max. - 25): ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("The Sorted Order is: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT:
RESULT: