Created
October 11, 2019 03:50
-
-
Save easleyschool/727eafbeb36b2702db0324b50fd17f83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C program implementing Selection Sort | |
# include <stdio.h> | |
// function to swap elements at the given index values | |
void swap(int arr[], int firstIndex, int secondIndex) | |
{ | |
int temp; | |
temp = arr[firstIndex]; | |
arr[firstIndex] = arr[secondIndex]; | |
arr[secondIndex] = temp; | |
} | |
// function to look for the smallest element in the given subarray | |
int indexOfMinimum(int arr[], int startIndex, int n) | |
{ | |
int minValue = arr[startIndex]; | |
int minIndex = startIndex; | |
for(int i = minIndex + 1; i < n; i++) { | |
if(arr[i] < minValue) | |
{ | |
minIndex = i; | |
minValue = arr[i]; | |
} | |
} | |
return minIndex; | |
} | |
void selectionSort(int arr[], int n) | |
{ | |
for(int i = 0; i < n; i++) | |
{ | |
int index = indexOfMinimum(arr, i, n); | |
swap(arr, i, index); | |
} | |
} | |
void printArray(int arr[], int size) | |
{ | |
int i; | |
for(i = 0; i < size; i++) | |
{ | |
printf("%d ", arr[i]); | |
} | |
printf("\n"); | |
} | |
int main() | |
{ | |
int arr[] = {46, 52, 21, 22, 11}; | |
int n = sizeof(arr)/sizeof(arr[0]); | |
selectionSort(arr, n); | |
printf("Sorted array: \n"); | |
printArray(arr, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment