Data structure Practical
Data structure Practical
QUICK SORT
#include <stdio.h>
void swaplint a, int b)
int t =*a,
*a=*b;
"b=t;
int main()
int arrl] = (12, 11, 13, 5, 6, 7);
int n =sizeoffarr) /sizeof(arr[o]):
printf("Original array: "):
for (int i =0; i<n; +){
printf("%d", arr[il):
quickSort{arr, 0, n-1);
printf("\nSorted array:"):
for (int i=0; i<n, it+)(
printf("%d",arr(i]):
return 0;
MERGE SORT
#include <stdio.h
int n2 = r- m;
int L[n1], R(n2]:
for (i= 0; i< n1; itt)
L] = arrll+):
for (j =0;j< n2; j++)
RÜ] =arr[m + 1+i];
i=0;
j=0;
k=l;
while (i < n1 &&j< n2){
if(L(] <= RU1) (
arr[k] = L[0:
Jelse
arr[k] = RU):
j++:
k++:
while (i <n1){
arr[k] = L[i):
44
k++
arr[k] = RI:
EXPERIMENT NO 2
Linear Search
tinclude <stdio.h>
int main()
int arr[] =12, 11, 13, 5, 6, 7}:
int size =sizeof(arr) / sizeofarrio).
int key = 6;
if (found) {
printf("\n%d found at index %d\n", key, index);
Jelse (
printf("\n%d not found\n", key):
return 0;
BINARY SEARCH
#include <stdio.h>
int main()
int arr[] = {5, 6, 7, 11, 12, 13)
int size = sizeofarr) i sizeoffarrO):
int key = 11;
printf("Sorted array: ")};
for (int i= 0; i< size; l++) {
printf("%d", arr[l);
int found =0, // Flag to indicate whether the key is found or not
int index; / index of the key if found
I/ Binary search
int low = 0, high =size - 1;
while (low <= high) (
int rmid = low + (high - low) /2;
if (arimid] =keyll
found =1:
index = mid;
break, // Key found, no need to continue searching
Jelse{
high = mid-1;
if (found) {
printf("\n%d found at index %d\n", key, index);
Jelse {
printf("n%d not foundn", keyl:
return 0,
EXPERIMENT NO 3
#incude <stdlib.h>
#define SIZE 10
struct Node{
int data,
struct Node* next,
struct Node* hashTable[sIZE]:
void initializeHashTable)
for (int i = 0, i< SIZE, I+)
hashTableli] = NULL;
newNode->data = key;
newNode->next = NULL;
int index = hashFunction(key).
if (hashTable[index]NULL)(
hashTableindex] = newNode;
J else {
| Colision handling: Add to the beginning of the linked list
newNode->next = hashTablelindex]:
hashTablelindex] = newNode;
void displayHashTable() {
printf("\nHash Table:\n");
for (int i = 0, i< SIZE; +){
printf("%d, 9:
struct Node* current = hashTable[jl:
while (current = NULL) (
printf(" %d", current->data),
current = current->next,
printf("\n"):
int main(){
initializeHashTable():
int keysl] = (12, 22, 32, 42, 52, 62, 72, 82, 92, 102);
for (int i = 0, i< sizeof(keys) / sizeoflkeys[oJI, i++){
insert(keysil):
displayHashTablel):
return 0;
EXPERIMENT NO 4
)
temp->next -ptr->next;
printf("n The deleted element is:%d ".ptr->info E
freetptr):
EXPERIMENT NO 5
#includecstdio.h>
#include<stdlib.h>
struct node
int data;
int choice 0,
while(choice =7)
printf("n****Main Menu****n"
printf("\nChoose one option from the following list.\n;
printf("\n= \n
".
printf("\n1insert in begining\n2. Insert at last\n3.Delete from
Beginning\n4.Delete from
case 1:
beginsert():
break;
Doubly Linked List (insertion, Deletlon& Display)
#include<stdio.h>
Hincludecstdlib.h>
struct node
int data;
int choice 0,
while(choice =9)
{
printf("\***"Main Menu***\n):
printf"\nChoose one option from the following list.n"):
printf("|n= \n
":
printf("\n1.Insert in begining\n2. Insertat last\n3.Insert at any
random location\n4.Delete from
Beginning\n 5.Delete from last\n6.Delete the node after the given
data\n7.Search\n8 Show\ni9. Exit\n":
printf"\nnter your choice?Nn":
scanf("\n%d", &cholce):
switch<choice)
case 1:
insertion_beginningt):
break;
case 2:
insertion_last():
break,
case 3: