0% found this document useful (0 votes)
6 views6 pages

DS - 5 827

The document contains C code implementations for two sorting algorithms: quick sort and merge sort. Each algorithm includes functions for sorting an array and printing the sorted results. The quick sort implementation uses a partitioning method, while the merge sort implementation combines two sorted subarrays.

Uploaded by

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

DS - 5 827

The document contains C code implementations for two sorting algorithms: quick sort and merge sort. Each algorithm includes functions for sorting an array and printing the sorted results. The quick sort implementation uses a partitioning method, while the merge sort implementation combines two sorted subarrays.

Uploaded by

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

Practical Assignment 5

NAME: VIDIT MANIK KHUNE

PRN: 20240802827

DIV: F

1.Write C code to implement quick sort.


#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = low - 1;

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

if (arr[j] < pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

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); // Sort elements before partition

quickSort(arr, pi + 1, high); // Sort elements after partition

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {10, 7, 8, 9, 1, 5};

int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;
}

2. write c code to implement merge sort


#include <stdio.h>

#include <stdlib.h>

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

int i, j, k;

int n1 = mid - left + 1;

int n2 = right - mid;

int *L = (int *)malloc(n1 * sizeof(int));

int *R = (int *)malloc(n2 * sizeof(int));

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

L[i] = arr[left + i];

for (j = 0; j < n2; j++)

R[j] = arr[mid + 1 + j];

i = 0; // Initial index of first subarray


j = 0; // Initial index of second subarray

k = left; // Initial index of merged subarray

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

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

} else {

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

// Copy any remaining elements of L[]

while (i < n1) {

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

// Copy any remaining elements of R[]

while (j < n2) {

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

free(L);

free(R);

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

if (left < right) {


int mid = left + (right - left) / 2;

mergeSort(arr, left, mid

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");

printArray(arr, arr_size);

return 0;
}

You might also like