on-internet
← Community

Bubble does not sort the array in C

Stack Overflow ·

The array is just never sorted for some reason. Here is the code: #include <stdio.h> /* swaps 2 values */ void swap(int* a, int* b){ int temp = *a; *a = *b; *b = temp; } void bubble_sort(int arr[]){ int num_of_elements_to_be_sorted = sizeof(arr) / sizeof(arr[0]); /* self explanitory */ for (int i = 0; i < num_of_elements_to_be_sorted; i++) { /* loops through the whole array starting from index 0*/ for (int j = 0; j < num_of_elements_to_be_sorted - 1 - i; j++) { /* loops through the array starting from index 1 */ if(arr[j] > arr[j+1]){ swap(&j, &j+1); } } } } int main(void){ int arr[10] = {3121

Comments

Sign in or sign up to comment and upvote.

No comments yet.