Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ria Subramanian ap cs50 selection sort
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main(void)
- {
- //gets size of array from user and creates array
- printf("Enter size of your array\n");
- int size = get_int();
- int array[size];
- //gets unsorted array from user
- printf("Enter array elements\n");
- for(int i = 0; i < size; i++)
- {
- array[i] = get_int();
- }
- //sets index of smallest number
- int minIndex = 0;
- for(int i = 0; i < size - 1; i++)
- {
- //assumes minimum is i
- minIndex = i;
- for(int j = i + 1; j < size; j++)
- {
- //checks for values smaller than array element minIndex
- if(array[j] < array[minIndex])
- {
- minIndex = j;
- }
- }
- //if the minIndex isn't the i value then array[i] and array[minIndex are swapped]
- if(minIndex != i)
- {
- int temp = 0;
- int temp2 = 0;
- temp = array[minIndex];
- for(int j = i; j < size - 1; j++)
- {
- temp2 = array[j];
- array[j] = temp;
- temp = temp2;
- }
- }
- }
- for(int i = 0; i < size; i++)
- {
- if(i != size - 1)
- {
- printf("%i, ", array[i]);
- }
- else
- {
- printf("%i", array[i]);
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement