#include <stdio.h>
// *** find the smallest index in an int array ***
int smallestIndex(int arr[], int len, int searchFrom){
if (len < searchFrom - 1)
return -1; // search out of range.
int ans = searchFrom;
for (int i = searchFrom; i < len ; i=i+1)
if (arr[ans] > arr[i])
ans = i;
return ans;
}
// *** swap two values in int array ***
void swap (int arr[], int len , int ind1, int ind2) {
if (ind1 < 0 || ind2 < 0 || ind1 > len-1 || ind2 > len-1)
return ; // index out of range.
int tmp = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = tmp;
}
// *** sort an int array ***
void sort(int arr[], int len) {
for (int i=0; i< len; i = i+1) {
int minInd = smallestIndex(arr, len, i);
swap(arr, len, i, minInd);
}
}
// *** prints an int array ***
void printArray (char Sentance[], int arr[], int len) {
printf("%s ", Sentance);
for(int i=0; i < len; i = i+1)
printf("%d, ", arr[i]);
printf("\n");
}
void main (){
int arr[] = {16,3,56,3,1,444,-12,29,13};
int arrLen = sizeof(arr)/sizeof(int);
printArray ("The original elements are: ", arr, arrLen);
sort(arr, arrLen);
printArray("The sorted elements are: ", arr, arrLen);
}