This program for selection sort gives unfavorable output, I tried a lot but unable to find my mistake, The output, I'm getting through this is not sorted, one or more elements are at wrong position(or they are not sorted)...Please help me to find my mistake.
#include <stdio.h>
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int *get_least(int *p, int i, int count)
{
int temp = *(p + i), key = 0;
int *index;
for (i; i < count; i++)
{
if (temp > *(p + i))
{
temp = *(p + i);
index = (p + i);
key++;
}
}
if (key == 0)
{
return (p + 1);
}
return (index);
}
void sel_sort(int *p, int count)
{
for (int i = 0; i < count - 1; i++)
{
swap((p + i), get_least(p, i, count));
}
}
int main()
{
int num[10], count;
printf("ENTER INPUT LIMIT:\n");
scanf("%d", &count);
printf("ENTER YOUR NUMBERS:\n");
for (int i = 0; i < count; i++)
{
scanf("%d", &num[i]);
}
sel_sort(num, count);
printf("OUTPUT AFTER SORTING:\n");
for (int i = 0; i < count; i++)
{
printf("%d ", num[i]);
}
return (0);
}

key, but returningp+1is almost certainly wrong. Try removingkeyand all the code related tokey.keythere in case if loop doesn't find any lesser value than the first element(i.etempor*(p+i)) then, it just return address of that element...But unfortunetly there I write (p+1) instead of (p+i)...But I also tried with removing whole code related to key, But I got same unsorted output.index, e.g.int *index = p + i;. That way, if no smaller value is found, the code will returnp + i.