#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
int main(void){
int s = 5;
int size[s];
size[0] = 1;
size[1] = 321;
size[2] = 2;
size[3] = 42;
size[4] = 21;
for(int i = 0; i <= s - 2; i++){
int min = i;
for(int j = i + 1; j <= s; j++){
if(size[j] < size[min]){
min = j;
}
}
if(min != i){
swap(&size[min], &size[i]);
}
}
printf("[");
for(int i = 0; i <= s - 1; i++){
printf("%i,", size[i]);
}
printf("]\n");
}
When I run selection, it doesnt sort my code, it replaces one of my list values with some garbage value and when I ran it through gdb I noticed that my minimum keeps going up even though its not supposed to. Help?
[1,2,21,42,321,]
is the print result.for
loop,j
is not valid index for the arraysize
.int size[] = {1,321,2,42,21}