Я пытаюсь упорядочить значения массива, пока пользователь вводит его значения.
Дело в том, что я хочу избежать попыток использовать метод "bubble" или "quick sort".
Вот мой код и как он работает:
int i, j, k;
int number;
for (i = 0; i < size; i = i + 1) {
printf("Give me the number #%d to add to the list: ", i + 1);
while (!scanf("%d", &list[i])) {
while(getchar() != '\n');
printf("You can't use chars!.\n");
printf("Give me the number #%d to add to the list: ", i + 1);
}
number = list[i];
if (number < list[i-1]) {
list[i-1] = list[i];
list[i] = number;
}
}
for (i = 0; i < size; i = i + 1) {
printf("%d,", list[i]);
}
что я получаю:
How much numbers do you want to order? 5
Give me the number #1 to add to the list: 5
Give me the number #2 to add to the list: 4
Give me the number #3 to add to the list: 3
Give me the number #4 to add to the list: 2
Give me the number #5 to add to the list: 1
4,3,2,1,1,% // here is the problem, the expected output should be: 1,2,3,4,5 (for example, if I have 5,8,3,2,9 I should get: 2,3,5,8,9).
Буду признателен за объяснение, почему мой код не работаетработа и, если возможно, рекомендации о том, как визуализировать подобные проблемы в будущем.
Заранее спасибо за помощь.