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

C Program For Insertion and Selection

The document contains C programs that implement two sorting algorithms: selection sort and insertion sort. Each program includes a function to sort an array and a function to print the array before and after sorting. Sample arrays are provided to demonstrate the functionality of both sorting methods.

Uploaded by

ruchita.wagh24
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)
6 views4 pages

C Program For Insertion and Selection

The document contains C programs that implement two sorting algorithms: selection sort and insertion sort. Each program includes a function to sort an array and a function to print the array before and after sorting. Sample arrays are provided to demonstrate the functionality of both sorting methods.

Uploaded by

ruchita.wagh24
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/ 4

// C program for implementation of selection sort

#include<stdio.>

void selectionSort(int arr[], int n)

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

// Assume the current position holds

// the minimum element

int min_idx = i;

// Iterate through the unsorted portion

// to find the actual minimum

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

if (arr[j] < arr[min_idx])

// Update min_idx if a smaller element is found

min_idx = j;

// Move minimum element to its

// correct position

int temp = arr[i];

arr[i] = arr[min_idx];

arr[min_idx] = temp;

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

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

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

printf("\n"); }
int main()

int arr[] = {64, 25, 12, 22, 11};

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

printf("Original array: ");

printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

}
// C program for implementation of Insertion Sort

#include<stdio.h>

/* Function to sort array using insertion sort */

void insertionSort(int arr[], int n)

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

int key = arr[i];

int j = i - 1;

/* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
*/

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

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

j = j - 1;

arr[j + 1] = key;

} /* A utility function to print array of size n */

void printArray(int arr[], int n)

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

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

printf("\n");

} // Driver method

int main()

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

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

insertionSort(arr, n);

printArray(arr, n);
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy