Can someone explain why this bubble sorting function doesn't work and why am I losing numbers in my output? I am very new to C, so please forgive me if this is something very obvious I missed.
#include <stdio.h> #include <stdlib.h> int bubble(int array[],int length) { int i, j; int temp; for(i = 0; i < (length); ++i) { for(j = 0; j < (length - 1); ++j) { if(array[i] > array[i+1]) { temp = array[i+1]; array[i+1] = array[i]; array[i] = temp; } } } return 0; } int main() { int array[] = {12,234,3452,5643,0}; int i; int length; length = (sizeof(array)/sizeof(int)); printf("Size of array = %d\n", length); bubble(array, length); for (i = 0; i < (length); ++i) { printf("%d\n", array[i]); } return 0; }
Exit
Size of array = 5 12 234 3452 0 0
user271528
source share