// 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;