Я продолжаю получать одинаковые числа 166 и 74. Я пытаюсь получить 74 166 250 273 441 545 659 710 808 879 924 931. Я действительно понятия не имею, где найти эту ошибку.Я знаю, что основная функция верна, но я не уверен, где найти ошибку, которая просто дает мне 166 и 74.
#include <stdio.h>
// Swap the values pointed to by a and b.
void swap( int *a, int *b )
{
int temp = *a;
*a = *b;
*b = temp;
}
// Return a pointer to the element of the given array with the smallest value.
int *findSmallest( int *list, int len )
{
int *smallest = list;
for ( int i = 1; i < len; i++ ) {
if(*smallest > *(list + i)) {
*smallest = *(list + i);
}
}
return smallest;
}
// Print the contents of the given list.
void printList( int *list, int len )
{
while ( len ) {
len--;
printf("%d ", *(list + --len));
}
printf( "\n" );
}
int main()
{
// A list of random-ish values.
int list[] = { 808, 250, 74, 659, 931, 273, 545, 879, 924, 710, 441, 166 };
int len = sizeof( list ) / sizeof( list[ 0 ] );
// For each index, find the smallest item from the remaining
// (unsorted) portion of the list.
for ( int i = 0; i < len; i++ ) {
int *p = findSmallest( list + i, len - i );
// Swap the smallest item into the first position in the unsorted part of the
// list.
swap( list + i, p );
}
printList( list, len );
}
}