I am trying to create a reverse bubble sort algorithm. It works but the first output is a big crazy number that i dont understand. The remaining outputs seem to be sorted in descending order. Where is my code wrong?
#include <stdio.h> void ft_rev_int_tab(int *tab, int size) { int i; int j; int k; i = 0; while (i < size) { j = 0; while (j < size -i) { if (tab[j] < tab[j+1]) { k = tab[j]; tab[j] = tab[j+1]; tab[j + 1] = k; } j++; } i++; } } int main(void) { int tab[] = {9, 320, 0, 113, 15}; int size = sizeof(tab) / sizeof(*tab); ft_rev_int_tab(tab, size); for (int i = 0; i < size; i++) { printf ("%d\n", tab[i]); } }
https://stackoverflow.com/questions/67238710/reverse-bubble-sort-algorithm-in-c-using-only-while-loop April 24, 2021 at 10:06AM
没有评论:
发表评论