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

Lab No 04

Data structure lab no 04

Uploaded by

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

Lab No 04

Data structure lab no 04

Uploaded by

2023msee7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>

// Function prototypes
void insertionSort(int arr[], int n);
void bubbleSort(int arr[], int n);
void selectionSort(int arr[], int n);
void mergeSort(int arr[], int l, int r);
void merge(int arr[], int l, int m, int r);
void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);

// Function to print an array


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

// Main function
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

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

printf("\nOriginal array: ");


printArray(arr, n);

// Sorting using insertion sort


printf("\nUsing Insertion Sort: ");
insertionSort(arr, n);
printArray(arr, n);

// Reset array
int arr_reset[n];
for (int i = 0; i < n; i++)
arr_reset[i] = arr[i];

// Sorting using bubble sort


printf("\nUsing Bubble Sort: ");
bubbleSort(arr_reset, n);
printArray(arr_reset, n);

// Reset array
int arr_reset_2[n];
for (int i = 0; i < n; i++)
arr_reset_2[i] = arr[i];

// Sorting using selection sort


printf("\nUsing Selection Sort: ");
selectionSort(arr_reset_2, n);
printArray(arr_reset_2, n);

// Reset array
int arr_reset_3[n];
for (int i = 0; i < n; i++)
arr_reset_3[i] = arr[i];

// Sorting using merge sort


printf("\nUsing Merge Sort: ");
mergeSort(arr_reset_3, 0, n - 1);
printArray(arr_reset_3, n);

// Reset array
int arr_reset_4[n];
for (int i = 0; i < n; i++)
arr_reset_4[i] = arr[i];

// Sorting using quick sort


printf("\nUsing Quick Sort: ");
quickSort(arr_reset_4, 0, n - 1);
printArray(arr_reset_4, n);

return 0;
}

// Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

// Selection Sort
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

// Merge Sort
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

// Quick Sort
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

You might also like