0% found this document useful (0 votes)
22 views14 pages

Adsa Report P9

The document provides implementations of various sorting algorithms in C, including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, Shell Sort, and Heap Sort. Each algorithm is accompanied by a program code that allows the user to input a list of integers and outputs the sorted array. The document serves as a practical guide for understanding and applying different sorting techniques.

Uploaded by

Shwetha V
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)
22 views14 pages

Adsa Report P9

The document provides implementations of various sorting algorithms in C, including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, Shell Sort, and Heap Sort. Each algorithm is accompanied by a program code that allows the user to input a list of integers and outputs the sorted array. The document serves as a practical guide for understanding and applying different sorting techniques.

Uploaded by

Shwetha V
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
You are on page 1/ 14

Ex. No: 9 Date : 21.08.

2024

SORTING
PROBLEM STATEMENT:
Implement the following sorting techniques to arrange a list of integers in ascending order.
a) Bubble sort

PROGRAM CODE:

#include <stdio.h>

void bubbleSort(int array[], int n) {


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j + 1]
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int array[n];
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
bubbleSort(array, n);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
return 0;
}

OUTPUT :
b) Insertion sort

PROGRAM CODE:
#include <stdio.h>
void insertionSort(int array[], int n) {
int i, j, key;
for (i = 1; i < n; i++) {
key = array[i];
j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
}

int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int array[n];
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
insertionSort(array, n);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
return 0;
}

OUTPUT :
c) Selection sort
PROGRAM CODE:
#include <stdio.h>

void selectionSort(int array[], int n) {


int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}

int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);

int array[n];
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}

selectionSort(array, n);

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

return 0;
}

OUTPUT:
d) Quick sort
PROGRAM CODE :
#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], i = low - 1;
for (int j = low; j < high; j++)
if (arr[j] < pivot) 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);
quickSort(arr, pi + 1, high);
}
}

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

quickSort(arr, 0, n - 1);

printf("Sorted array: ");


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

return 0;
}

OUTPUT :
e) Merge sort
PROGRAM CODE:
#include <stdio.h>

void merge(int arr[], int l, int m, int r) {


int n1 = m - l + 1, n2 = r - m, L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

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);
}
}

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

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

printf("Sorted array: ");


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

return 0;
}

OUTPUT:
f) Shell Sort
PROGRAM CODE:
#include <stdio.h>

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


for (int gap = n / 2; gap > 0; gap /= 2)
for (int i = gap; i < n; i++) {
int temp = arr[i], j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}

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

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

shellSort(arr, n);

printf("Sorted array: ");


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

return 0;
}

OUTPUT :
g) Heap Sort
PROGRAM CODE:
#include <stdio.h>

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


int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


int largest = i, left = 2 * i + 1, right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) largest = left;


if (right < n && arr[right] > arr[largest]) largest = right;

if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

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


for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

int main() {
int n;

printf("Enter number of elements: ");


scanf("%d", &n);

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

heapSort(arr, n);

printf("Sorted array: ");


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

return 0;
}
OUTPUT :

You might also like