0% found this document useful (0 votes)
8 views

Data structure Practical

The document outlines various sorting and searching algorithms including Quick Sort, Merge Sort, Linear Search, Binary Search, and Hashing using the modulo division method. It also describes operations on linked lists such as insertion, deletion, and display for singly, circular, and doubly linked lists. Each experiment includes C code snippets demonstrating the implementation of these algorithms and data structures.

Uploaded by

Shiwam Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Data structure Practical

The document outlines various sorting and searching algorithms including Quick Sort, Merge Sort, Linear Search, Binary Search, and Hashing using the modulo division method. It also describes operations on linked lists such as insertion, deletion, and display for singly, circular, and doubly linked lists. Each experiment includes C code snippets demonstrating the implementation of these algorithms and data structures.

Uploaded by

Shiwam Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

EXPERIMENT NO 1

QUICK SORT
#include <stdio.h>
void swaplint a, int b)
int t =*a,

*a=*b;
"b=t;

int partition(int ar], int low, int high){


int pivot = arr[high]:
int i=(low -1;
for (intj =low; j<= high -1;j){
if(arü] < pivot) {
tt;
swap(&arrl]. &aril:

swap(&arrli +1], &arr[highJ):


return (i + 1);

void quicksort(int arr[. int low, int high) {


if (low < high) (

int pi partition(arr, low, high);


quickSorttar, low, pi - 1):
quicksort[arr, pi + 1, high),

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

void merge(int arr], int I, int m, int r)


int i, j, k;
int n1 = m-1+1;

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++

while (j < n2){

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;

printf("Original array: "};


for (int i= 0; i<size, it+){
printf("%d ", arrlill:

int found = 0: // Flag to indicate whether the key is found or not


int index; I| Index of the key if found
I| Linear search
for (int i= 0; i< size; i++)(
if (arri] == kev) {
found = 1;
index = i;

break; // Key found, no need to continue searching

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

if (arr[mid] < key) {


low = mid + 1;

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

Hashing(modulo division method


#include <stdio.h>

#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;

int hashFunction(int key){


return key % SIZE;

void insert(int key) (

struct Node* newNode =(struct Node)malloc(sizeof(struct


Node):

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

Singy Linked List (insertion,Deletion& Display)


include<stdlib.h>
printf("\n Position not Found:\n"):
return,

)
temp->next -ptr->next;
printf("n The deleted element is:%d ".ptr->info E
freetptr):

EXPERIMENT NO 5

Circular Linked List (insertion,Deletion& Display)

#includecstdio.h>

#include<stdlib.h>

struct node

int data;

struct node "next;

struct node *head;


void beginsert ():
vold lastinsert ():
void randominsert{):
void begin_delete().
void last_ delete):
void randorm deletel):
void displayl):
void search():
void main 0

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

last\n5.Search for an element\n6.Show\n7.Exit\n"):


printf("\nEnter your choicen");
scanf("n%d",&choice):
switch(choice)

case 1:

beginsert():
break;
Doubly Linked List (insertion, Deletlon& Display)

#include<stdio.h>
Hincludecstdlib.h>
struct node

struct node *prev,


struct node "next;

int data;

struct node "head;


void insertion_beginningl):
void insertion_ last():
void insertion_specified(:
void deletion beginning):
void deletion_last():
void deletion_specified():
void displayt:
vold search():
void main ()

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:

You might also like