c - runtime error after running bubble sort -


i'm trying write program bubble sorts array, every time run error pops up: runtime error: index 4 out of bounds type 'int [e]'

what issue here?

int main(void) {     printf("how many elements there in array?: ");     int e = get_int();      int array[e];      (int = 0; < e; i++) {         printf("insert element #%i: ", + 1);         array[i] = get_int();     }      (int = 0; < e - 1; e++) {         (int j = 0; j < e - - 1; j++) {             if (array[j] > array[j + 1]) {                 int swap = array[j];                 array[j] = array[j + 1];                 array[j + 1] = swap;             }         }     }     printf("\n"); } 

besides error noted chqrlie, don't overcomplicate bubble sort (and it's buggy, it's considering contiguous indices). write (note inner loop optimized avoid processing "lower triangle" items twice):

for (int = 0; < e; i++) {     (int j = i+1; j < e; j++) {         if (array[j] > array[i]) {             int swap = array[j];             array[j] = array[i];             array[i] = swap;         }     } } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -