Last active
March 16, 2022 23:35
-
-
Save ardrabczyk/034755783ac50d276063 to your computer and use it in GitHub Desktop.
Bubble sort implementation in C using do...while loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void bubble(int arr[], size_t size) | |
{ | |
int i = 0; | |
int temp; | |
int again = 1; | |
int iter = size - 1; | |
do { | |
again = 0; | |
i = 0; | |
for(; i < iter; ++i) | |
{ | |
if (arr[i + 1] < arr[i]) | |
{ | |
again = 1; | |
temp = arr[i]; | |
arr[i] = arr[i + 1]; | |
arr[i + 1] = temp; | |
} | |
} | |
--iter; | |
} while (again); | |
} | |
int main(void) | |
{ | |
int a[] = {5, 1, 4, 2, 8, 0, -11}; | |
bubble(a, sizeof a / sizeof (int)); | |
unsigned i = 0; | |
for(; i < (sizeof a / sizeof (int)); ++i) | |
{ | |
printf("%d\n", a[i]); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment