0% found this document useful (0 votes)
10 views13 pages

Practical Sorting and Queue Operations

The document contains multiple practical implementations of data structures and sorting algorithms in C, including a queue, bubble sort, quick sort, merge sort, and radix sort. Each section provides the code for the respective algorithm along with a main function to demonstrate its functionality. The document serves as a comprehensive guide for understanding and implementing these algorithms.

Uploaded by

joshidevanshi360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Practical Sorting and Queue Operations

The document contains multiple practical implementations of data structures and sorting algorithms in C, including a queue, bubble sort, quick sort, merge sort, and radix sort. Each section provides the code for the respective algorithm along with a main function to demonstrate its functionality. The document serves as a comprehensive guide for understanding and implementing these algorithms.

Uploaded by

joshidevanshi360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL 1:

//QUEUE

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

int queue[SIZE]; enque


int front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Enqueued %d\n", value);
}
}

int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n"); deque
return -1;
} else {
return queue[front++];
}
}

int peek() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
return -1;
} else {
return queue[front];
}
}

void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue elements are:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
int choice, value;

while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
value = dequeue();
if (value != -1)
printf("Dequeued: %d\n", value);
break;
case 3:
value = peek();
if (value != -1)
printf("Front element: %d\n", value);
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}

return 0;
}

//BUBBLE SORT

#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void displayArray(int arr[], int n) {
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[100], n;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

bubbleSort(arr, n);
displayArray(arr, n);

return 0;
}

PRACTICAL 2:
// Quick Sort

#include <stdio.h>
// Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1), temp;

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}

temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;


int pi = i + 1;

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

// Function to display array


void displayArray(int arr[], int size) {
printf("Sorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Main function
int main() {
int arr[100], n;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

quickSort(arr, 0, n - 1);
displayArray(arr, n);

return 0;
}

PRACTICAL 3:
// Quick Sort

#include <stdio.h>

// Quick Sort function


void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1), temp;

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}

temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;


int pi = i + 1;

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

// Function to display array


void displayArray(int arr[], int size) {
printf("Sorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Main function
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

quickSort(arr, 0, n - 1);
displayArray(arr, n);

return 0;
}

PRACTICAL 4:

//Merge Sort

#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {


int i = left, j = mid + 1, k = 0, temp[100];
while (i <= mid && j <= right) {
temp[k++] = (arr[i] <= arr[j]) ? arr[i++] : arr[j++];
}

while (i <= mid) temp[k++] = arr[i++];


while (j <= right) temp[k++] = arr[j++];

for (i = left, k = 0; i <= right; i++, k++) arr[i] = temp[k];


}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = (left + right) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

void displayArray(int arr[], int n) {


printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

mergeSort(arr, 0, n - 1);
displayArray(arr, n);

return 0;
}

PRACTICAL 5:

#include <stdio.h>

int getMax(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];
return max;
}

void countingSort(int arr[], int n, int exp) {


int output[100], count[10] = {0};

for (int i = 0; i < n; i++)


count[(arr[i] / exp) % 10]++;

for (int i = 1; i < 10; i++)


count[i] += count[i - 1];

for (int i = n - 1; i >= 0; i--) {


int digit = (arr[i] / exp) % 10;
output[--count[digit]] = arr[i];
}

for (int i = 0; i < n; i++)


arr[i] = output[i];
}

void radixSort(int arr[], int n) {


int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, n, exp);
}

void displayArray(int arr[], int n) {


printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100], n;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

radixSort(arr, n);
displayArray(arr, n);

return 0;
}

You might also like