Strange Bubble sort behaviour.

Posted by user271528 on Stack Overflow See other posts from Stack Overflow or by user271528
Published on 2010-05-09T22:06:21Z Indexed on 2010/05/09 22:08 UTC
Read the original article Hit count: 342

Filed under:
|
|

Can anyone explain why this bubble sort function doesn't work and why I loose number in my output. I'm very new to C, so please forgive me if this is something very obvious I have missed.

#include <stdio.h>

#include

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; }

Output

Size of array = 5 12 234 3452 0 0

© Stack Overflow or respective owner

Related posts about c

    Related posts about bubble-sort