Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,790 answers

573 users

How to implement the bubble sort algorithm in C

1 Answer

0 votes
#include <stdio.h>

void bubbleSort(int arr[], int len) {
    for (int i = 0; i < len - 1; i++) {
        for (int j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
            }
        }
    }
}

int main(void) {
    int arr[] = {6, 8, 0, 3, 1, 6, 4};
    int length = sizeof(arr)/sizeof(arr[0]);  
          
    bubbleSort(arr, length);
   
    for (int i = 0; i < length; i++) {   
        printf("%d ", arr[i]);  
    }  
    
    return 0;
}
 
 
 
 
 
/*
run:
 
0 1 3 4 6 6 8 
 
*/

 





answered Sep 14, 2021 by avibootz

Related questions

1 answer 55 views
1 answer 37 views
1 answer 48 views
1 answer 44 views
2 answers 132 views
...