0% found this document useful (0 votes)
4 views3 pages

Exp1 Aoa

The document contains implementations of two sorting algorithms: Insertion Sort and Selection Sort in C. Each algorithm is accompanied by a main function that demonstrates sorting an array and printing the original and sorted arrays. The code snippets are complete with necessary functions for sorting and displaying the arrays.

Uploaded by

pfake580
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)
4 views3 pages

Exp1 Aoa

The document contains implementations of two sorting algorithms: Insertion Sort and Selection Sort in C. Each algorithm is accompanied by a main function that demonstrates sorting an array and printing the original and sorted arrays. The code snippets are complete with necessary functions for sorting and displaying the arrays.

Uploaded by

pfake580
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/ 3

NAME : VANSH MISTARI

ROLL NO.: 39 (DS)

EXP 1 : IMPLEMENT A PROGRAM FOR INSERTION SORT AND SELECTION SORT


➢ INSERTION SORT
#include <stdio.h>

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;

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

int i;

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

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

printf("\n");

int main() {

int arr[] = {56,68,90,57,24};

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

printf("Original array: \n");

printArray(arr, n);

insertionSort(arr, n);

printf("Sorted array: \n");


printArray(arr, n);

return 0;

OUTPUT :

➢ SELECTION SORT

#include <stdio.h>

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

int i, j, minIndex, temp;

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

minIndex = i;

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

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

minIndex = j;

if (minIndex != i) {

temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

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

int i;

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


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

printf("\n");

int main() {

int arr[] = {49,56,89,23,42};

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

printf("Original array: \n");

printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

OUTPUT :

You might also like