Professional Documents
Culture Documents
Bubble Sort
Bubble Sort
#include <stdio.h>
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
************************************insertion sort**********************************
#include <math.h>
#include <stdio.h>
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
#include<stdlib.h>
#include<stdio.h>
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
**********************************heap sort**********************************
#include <iostream>
// Driver program
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
heapSort(arr, n);
***********************************radix sort****************************************
#include<iostream>
using namespace std;
****************************binary tree***************************************
struct node
{
int data;
struct node *left;
struct node *right;
};
/* newNode() allocates a new node with the given data and NULL left and
right pointers. */
struct node* newNode(int data)
{
// Allocate memory for new node
struct node* node = (struct node*)malloc(sizeof(struct node));
// Assign data to this node
node->data = data;
int main()
{
/*create root*/
struct node *root = newNode(1);
/* following is the tree after above statement
1
/ \
NULL NULL
*/
root->left = newNode(2);
root->right = newNode(3);
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
NULL NULL NULL NULL
*/
root->left->left = newNode(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 NULL NULL NULL
/ \
NULL NULL
*/
getchar();
return 0;
}
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;
node *create()
{
node *p;
int x;
printf("Enter data(-1 for no node):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
return 0;
}
1. /*
2. * C Program to Construct a Binary Search Tree and perform deletion, inorder
traversal on it
3. */
4. #include <stdio.h>
5. #include <stdlib.h>
6.
7. struct btnode
8. {
9. int value;
10. struct btnode *l;
11. struct btnode *r;
12. }*root = NULL, *temp = NULL, *t2, *t1;
13.
14. void delete1();
15. void insert();
16. void delete();
17. void inorder(struct btnode *t);
18. void create();
19. void search(struct btnode *t);
20. void preorder(struct btnode *t);
21. void postorder(struct btnode *t);
22. void search1(struct btnode *t,int data);
23. int smallest(struct btnode *t);
24. int largest(struct btnode *t);
25.
26. int flag = 1;
27.
28. void main()
29. {
30. int ch;
31.
32. printf("\nOPERATIONS ---");
33. printf("\n1 - Insert an element into tree\n");
34. printf("2 - Delete an element from the tree\n");
35. printf("3 - Inorder Traversal\n");
36. printf("4 - Preorder Traversal\n");
37. printf("5 - Postorder Traversal\n");
38. printf("6 - Exit\n");
39. while(1)
40. {
41. printf("\nEnter your choice : ");
42. scanf("%d", &ch);
43. switch (ch)
44. {
45. case 1:
46. insert();
47. break;
48. case 2:
49. delete();
50. break;
51. case 3:
52. inorder(root);
53. break;
54. case 4:
55. preorder(root);
56. break;
57. case 5:
58. postorder(root);
59. break;
60. case 6:
61. exit(0);
62. default :
63. printf("Wrong choice, Please enter correct choice ");
64. break;
65. }
66. }
67. }
68.
69. /* To insert a node in the tree */
70. void insert()
71. {
72. create();
73. if (root == NULL)
74. root = temp;
75. else
76. search(root);
77. }
78.
79. /* To create a node */
80. void create()
81. {
82. int data;
83.
84. printf("Enter data of node to be inserted : ");
85. scanf("%d", &data);
86. temp = (struct btnode *)malloc(1*sizeof(struct btnode));
87. temp->value = data;
88. temp->l = temp->r = NULL;
89. }
90.
91. /* Function to search the appropriate position to insert the new node */
92. void search(struct btnode *t)
93. {
94. if ((temp->value > t->value) && (t->r != NULL)) /* value more than
root node value insert at right */
95. search(t->r);
96. else if ((temp->value > t->value) && (t->r == NULL))
97. t->r = temp;
98. else if ((temp->value < t->value) && (t->l != NULL)) /* value less t
han root node value insert at left */
99. search(t->l);
100. else if ((temp->value < t->value) && (t->l == NULL))
101. t->l = temp;
102. }
103.
104. /* recursive function to perform inorder traversal of tree */
105. void inorder(struct btnode *t)
106. {
107. if (root == NULL)
108. {
109. printf("No elements in a tree to display");
110. return;
111. }
112. if (t->l != NULL)
113. inorder(t->l);
114. printf("%d -> ", t->value);
115. if (t->r != NULL)
116. inorder(t->r);
117. }
118.
119. /* To check for the deleted node */
120. void delete()
121. {
122. int data;
123.
124. if (root == NULL)
125. {
126. printf("No elements in a tree to delete");
127. return;
128. }
129. printf("Enter the data to be deleted : ");
130. scanf("%d", &data);
131. t1 = root;
132. t2 = root;
133. search1(root, data);
134. }
135.
136. /* To find the preorder traversal */
137. void preorder(struct btnode *t)
138. {
139. if (root == NULL)
140. {
141. printf("No elements in a tree to display");
142. return;
143. }
144. printf("%d -> ", t->value);
145. if (t->l != NULL)
146. preorder(t->l);
147. if (t->r != NULL)
148. preorder(t->r);
149. }
150.
151. /* To find the postorder traversal */
152. void postorder(struct btnode *t)
153. {
154. if (root == NULL)
155. {
156. printf("No elements in a tree to display ");
157. return;
158. }
159. if (t->l != NULL)
160. postorder(t->l);
161. if (t->r != NULL)
162. postorder(t->r);
163. printf("%d -> ", t->value);
164. }
165.
166. /* Search for the appropriate position to insert the new node */
167. void search1(struct btnode *t, int data)
168. {
169. if ((data>t->value))
170. {
171. t1 = t;
172. search1(t->r, data);
173. }
174. else if ((data < t->value))
175. {
176. t1 = t;
177. search1(t->l, data);
178. }
179. else if ((data==t->value))
180. {
181. delete1(t);
182. }
183. }
184.
185. /* To delete a node */
186. void delete1(struct btnode *t)
187. {
188. int k;
189.
190. /* To delete leaf node */
191. if ((t->l == NULL) && (t->r == NULL))
192. {
193. if (t1->l == t)
194. {
195. t1->l = NULL;
196. }
197. else
198. {
199. t1->r = NULL;
200. }
201. t = NULL;
202. free(t);
203. return;
204. }
205.
206. /* To delete node having one left hand child */
207. else if ((t->r == NULL))
208. {
209. if (t1 == t)
210. {
211. root = t->l;
212. t1 = root;
213. }
214. else if (t1->l == t)
215. {
216. t1->l = t->l;
217.
218. }
219. else
220. {
221. t1->r = t->l;
222. }
223. t = NULL;
224. free(t);
225. return;
226. }
227.
228. /* To delete node having right hand child */
229. else if (t->l == NULL)
230. {
231. if (t1 == t)
232. {
233. root = t->r;
234. t1 = root;
235. }
236. else if (t1->r == t)
237. t1->r = t->r;
238. else
239. t1->l = t->r;
240. t == NULL;
241. free(t);
242. return;
243. }
244.
245. /* To delete node having two child */
246. else if ((t->l != NULL) && (t->r != NULL))
247. {
248. t2 = root;
249. if (t->r != NULL)
250. {
251. k = smallest(t->r);
252. flag = 1;
253. }
254. else
255. {
256. k =largest(t->l);
257. flag = 2;
258. }
259. search1(root, k);
260. t->value = k;
261. }
262.
263. }
264.
265. /* To find the smallest element in the right sub tree */
266. int smallest(struct btnode *t)
267. {
268. t2 = t;
269. if (t->l != NULL)
270. {
271. t2 = t;
272. return(smallest(t->l));
273. }
274. else
275. return (t->value);
276. }
277.
278. /* To find the largest element in the left sub tree */
279. int largest(struct btnode *t)
280. {
281. if (t->r != NULL)
282. {
283. t2 = t;
284. return(largest(t->r));
285. }
286. else
287. return(t->value);
288. }
************************************splay tree***************************************
#include<stdio.h>
int main()
{
node *root=NULL;
int x,n,i,op;
do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
break;
return 0;
}
T->ht=height(T);
return(T);
}
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left!= NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
return(T);
}
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
*********************************graph**********************************************\
DFS(Adjaency Matrix)
#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);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
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);
}
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
*******************************bfs***************************************
#include<stdio.h>
#include<stdlib.h>
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int main()
{
create_graph();
BF_Traversal();
return 0;
}
void BF_Traversal()
{
int v;
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
*************************************basic operation*********************************
#include <stdio.h>
#include <stdlib.h>
return graph;
}
return 0;
}
*******************************queue***************************************
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
return 0;
}
********************************stack****************************************
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == MAXSIZE)
return 1;
else
return 0;
}
int peek() {
return stack[top];
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}
int main() {
// push items on to the stack
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);
return 0;
}
***********************************linked list*******************
1. #include<stdio.h>
2. #include<stdlib.h>
3.
4. struct node
5. {
6. int data;
7. struct node *next;
8. };
9.
10. void display(struct node* head)
11. {
12. struct node *temp = head;
13. printf("\n\nList elements are - \n");
14. while(temp != NULL)
15. {
16. printf("%d --->",temp->data);
17. temp = temp->next;
18. }
19. }
20.
21. void insertAtMiddle(struct node *head, int position, int value) {
22. struct node *temp = head;
23. struct node *newNode;
24. newNode = malloc(sizeof(struct node));
25. newNode->data = value;
26.
27. int i;
28.
29. for(i=2; inext != NULL) {
30. temp = temp->next;
31. }
32. }
33. newNode->next = temp->next;
34. temp->next = newNode;
35. }
36.
37. void insertAtFront(struct node** headRef, int value) {
38. struct node* head = *headRef;
39.
40. struct node *newNode;
41. newNode = malloc(sizeof(struct node));
42. newNode->data = value;
43. newNode->next = head;
44. head = newNode;
45.
46. *headRef = head;
47. }
48.
49. void insertAtEnd(struct node* head, int value){
50. struct node *newNode;
51. newNode = malloc(sizeof(struct node));
52. newNode->data = value;
53. newNode->next = NULL;
54.
55. struct node *temp = head;
56. while(temp->next != NULL){
57. temp = temp->next;
58. }
59. temp->next = newNode;
60. }
61.
62. void deleteFromFront(struct node** headRef){
63. struct node* head = *headRef;
64. head = head->next;
65. *headRef = head;
66. }
67.
68. void deleteFromEnd(struct node* head){
69. struct node* temp = head;
70. while(temp->next->next!=NULL){
71. temp = temp->next;
72. }
73. temp->next = NULL;
74. }
75.
76. void deleteFromMiddle(struct node* head, int position){
77. struct node* temp = head;
78. int i;
79. for(i=2; inext != NULL) {
80. temp = temp->next;
81. }
82. }
83.
84. temp->next = temp->next->next;
85. }
86.
87. int main() {
88. /* Initialize nodes */
89. struct node *head;
90. struct node *one = NULL;
91. struct node *two = NULL;
92. struct node *three = NULL;
93.
94. /* Allocate memory */
95. one = malloc(sizeof(struct node));
96. two = malloc(sizeof(struct node));
97. three = malloc(sizeof(struct node));
98.
99. /* Assign data values */
100. one->data = 1;
101. two->data = 2;
102. three->data = 3;
103.
104. /* Connect nodes */
105. one->next = two;
106. two->next = three;
107. three->next = NULL;
108.
109. /* Save address of first node in head */
110. head = one;
111.
112. display(head); // 1 --->2 --->3 --->
113.
114. insertAtFront(&head, 4);
115. display(head); // 4 --->1 --->2 --->3 --->
116.
117. deleteFromFront(&head);
118. display(head); // 1 --->2 --->3 --->
119.
120. insertAtEnd(head, 5);
121. display(head); // 1 --->2 --->3 --->5 --->
122.
123. deleteFromEnd(head);
124. display(head); // 1 --->2 --->3 --->
125.
126. int position = 3;
127. insertAtMiddle(head, position, 10);
128. display(head); // 1 --->2 --->10 --->3 --->
129.
130. deleteFromMiddle(head, position);
131. display(head); // 1 --->2 --->3 --->
132. }