Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- const int ARR_SIZE = 10;
- int main(void)
- {
- int arr[] = {1, 2, 5, 3, 4, 7, 6, 10, 8, 9};
- // Displaying data pre-selection sort
- printf("PRE SORT\n");
- for (int i = 0; i < ARR_SIZE; i++)
- {
- printf("%i\n", arr[i]);
- }
- // Sorting array via selection sort
- for (int i = 0; i < ARR_SIZE-1; i++)
- {
- // Setting current smallest values to the next number in the array
- int smallestNumber = arr[i];
- int smallIndex = i;
- // finding the smallest number out of the remaining array
- for (int j = i; j < ARR_SIZE; j++)
- {
- // set location and value variables of the smallest number if something smaller was found in the array
- if (smallestNumber > arr[j])
- {
- smallestNumber = arr[j];
- smallIndex = j;
- }
- }
- // if the current number isn't the smallest number found set values
- if (arr[i] != smallestNumber)
- {
- int tempHold = arr[i];
- arr[i] = smallestNumber;
- arr[smallIndex] = tempHold;
- smallestNumber = arr[i+1];
- }
- }
- // Displaying data post-selection sort
- printf("\nPOST SORT\n");
- for (int i = 0; i < ARR_SIZE; i++)
- {
- printf("%i\n", arr[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement