Advertisement
IlanZiin

Bubble sort C

Mar 23rd, 2024
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void sort(int array[], int size) {
  5.     for(int i = 0; i < size - 1; i++){
  6.         for(int j = 0; j < size - i - 1; j++){
  7.             if(array[j] > array[j + 1]) {
  8.                 int temp = array[j];
  9.                 array[j] = array[j + 1];
  10.                 array[j + 1] = temp;
  11.             }
  12.         }
  13.     }
  14. }
  15.  
  16. void printArray(int array[], int size) {
  17.     for(int i = 0; i < size; i++) {
  18.         printf("%d ", array[i]);
  19.     }
  20. }
  21.  
  22. int main() {
  23.    
  24.     int array[] = {7, 4, 2, 3, 1, 5, 9};
  25.     int size = sizeof(array)/sizeof(array[0]);
  26.  
  27.     sort(array, size);
  28.     printArray(array, size);
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement