0% found this document useful (0 votes)
3 views15 pages

ADITYA dsa lab file

The document contains implementations of various sorting algorithms in C, including Selection Sort, Quick Sort, Merge Sort, Insertion Sort, and Bubble Sort. Each algorithm is accompanied by a main function that prompts the user for input, sorts the array, and prints the sorted output. The author of the programs is Dr. Paras Jain, and the document includes placeholders for experiment and submission dates.
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)
3 views15 pages

ADITYA dsa lab file

The document contains implementations of various sorting algorithms in C, including Selection Sort, Quick Sort, Merge Sort, Insertion Sort, and Bubble Sort. Each algorithm is accompanied by a main function that prompts the user for input, sorts the array, and prints the sorted output. The author of the programs is Dr. Paras Jain, and the document includes placeholders for experiment and submission dates.
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/ 15

Selection sort

#include <stdio.h>

void selectionSort(int arr[], int size);

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

int main() {

int arr[100], n, i;

printf("Program: Brute-force Based Selection Sort\n");

printf("Author: Dr. Paras Jain\n");

printf("Experiment Date: \n");

printf("Submission Date: \n");

printf("------------------------------------------------------------\n");

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

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

printf("Enter element [%d]: ", i + 1);

scanf("%d", &arr[i]);

printf("\nOriginal Array: [ ");

printArray(arr, n);

printf("]\n");
selectionSort(arr, n);

printf("\nSorted Array (Ascending Order): [ ");

printArray(arr, n);

printf("]\n");

return 0;

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

int i, j, temp;

for (i = 0; i < size - 1; i++) {

for (j = i + 1; j < size; j++) {

if (arr[j] < arr[i]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

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

int i;

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


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

Quick sort
#include <stdio.h>

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


int hoarePartition(int arr[], int low, int high);
void swap(int *a, int *b);
void printArray(int arr[], int size);

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

printf("Program: Quick Sort using Hoare's Partition Scheme\n");


printf("Author: Dr. Paras Jain\n");
printf("Experiment Date: \n");
printf("Submission Date: \n");
printf("------------------------------------------------------------\n");

printf("Enter the number of elements in the array: ");


scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter element [%d]: ", i + 1);
scanf("%d", &arr[i]);
}

printf("\nOriginal Array: [ ");


printArray(arr, n);
printf("]\n");

quickSort(arr, 0, n - 1);

printf("\nSorted Array (Ascending Order): [ ");


printArray(arr, n);
printf("]\n");

return 0;
}

// Quick sort with Hoare partition


void quickSort(int arr[], int low, int high) {
if (low < high) {
int p = hoarePartition(arr, low, high);
quickSort(arr, low, p);
quickSort(arr, p + 1, high);
}
}

// Hoare's partition scheme (uses middle element as pivot)


int hoarePartition(int arr[], int low, int high) {
int pivot = arr[(low + high) / 2];
int i = low - 1;
int j = high + 1;

while (1) {
do {
i++;
} while (arr[i] < pivot);

do {
j--;
} while (arr[j] > pivot);

if (i >= j)
return j;

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

// Swap function
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Print function
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}

Merge sort

#include <stdio.h>

void Mergesort(int A[], int n);


void Merge(int B[], int C[], int A[], int p, int q);
void printArray(int A[], int n);
int main() {
int A[20], n, i;
printf("Program for sorting an array using Divide-and-Conquer based Merge
sort\n");
printf("Author : Dr. Paras Jain\n");
printf("Experiment Date : \n"); // self details
printf("Submission Date : \n");

printf("---------------------------------------------------------------------------------------------------
------------\n");

printf("Enter the array size : ");


scanf("%d", &n);
printf("\n");

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


printf("Enter element number %d: ", i + 1);
scanf("%d", &A[i]);
}

printf("\nThe array before sorting is : [ ");


printArray(A, n);
printf("]\n\n");
Mergesort(A, n);

printf("The array after sorting is : [ ");


printArray(A, n);
printf("]\n\n");

return 0;
}

// Sorts the array using Merge Sort (Divide-and-Conquer)


void Mergesort(int A[], int n) {
int i, j = 0, k, B[20], C[20], p, q;
if (n > 1) {
for (i = 0; i < n / 2; i++) {
B[i] = A[i];
}
p = i;

for (k = n / 2; k < n; k++) {


C[j] = A[k];
j++;
}
q = j;

Mergesort(B, p);
Mergesort(C, q);
Merge(B, C, A, p, q);
}
}

// Merges two sorted arrays into one


void Merge(int B[], int C[], int A[], int p, int q) {
int i = 0, j = 0, k = 0, ind1, ind2;

while (i < p && j < q) {


if (B[i] <= C[j]) {
A[k++] = B[i++];
} else {
A[k++] = C[j++];
}
}

ind2 = k;

if (i == p) {
for (ind1 = j; ind1 < q; ind1++) {
A[ind2++] = C[ind1];
}
} else {
for (ind1 = i; ind1 < p; ind1++) {
A[ind2++] = B[ind1];
}
}
}

void printArray(int A[], int n) {


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

Insertion sort

#include <stdio.h>

void insertion_sort(int A[], int n);


void printArray(int A[], int n);

int main() {
int A[20], n, i;
printf("Program for sorting an array using Brute Force based Insertion sort\n");
printf("Author : Dr. Paras Jain\n");
printf("Experiment Date : \n"); // self details
printf("Submission Date : \n");

printf("---------------------------------------------------------------------------------------------------
------------\n");

printf("Enter the array size : ");


scanf("%d", &n);
printf("\n");

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


printf("Enter element number %d: ", i + 1);
scanf("%d", &A[i]);
}

printf("\nThe array before sorting is : [ ");


printArray(A, n);
printf("]\n\n");

insertion_sort(A, n);

printf("The array after sorting is : [ ");


printArray(A, n);
printf("]\n\n");

return 0;
}

void insertion_sort(int A[], int n) {


for (int j = 1; j < n; j++) {
int key = A[j];
int i;

for (i = j - 1; i >= 0 && A[i] > key; i--) {


A[i + 1] = A[i];
}
A[i + 1] = key;
}
}

void printArray(int A[], int n) {


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

Bubble sort
#include <stdio.h>

void Bubble_sort(int A[], int n);


void printArray(int A[], int n);

int main() {
int A[20], n, i;
printf("Program for sorting an array using Brute Force based Bubble sort\n");
printf("Author : Dr. Paras Jain\n");
printf("Experiment Date : \n"); // self details
printf("Submission Date : \n");

printf("---------------------------------------------------------------------------------------------------
------------\n");

printf("Enter the array size : ");


scanf("%d", &n);
printf("\n");

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


printf("Enter element number %d: ", i + 1);
scanf("%d", &A[i]);
}

printf("\nThe array before sorting is : [ ");


printArray(A, n);
printf("]\n\n");

Bubble_sort(A, n);

printf("The array after sorting is : [ ");


printArray(A, n);
printf("]\n\n");

return 0;
}

void Bubble_sort(int A[], int n) {


int i = 0, j, temp;
while (i < n - 1) {
j = 0;
while (j < n - 1 - i) {
if (A[j] > A[j + 1]) {
A[j] = A[j] + A[j + 1];
A[j + 1] = A[j] - A[j + 1];
A[j] = A[j] - A[j + 1];
}
j++;
}
i++;
}
}

void printArray(int A[], int n) {


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

You might also like