Created
November 22, 2014 21:13
-
-
Save prasadwrites/b737e67d1d94a4a96b13 to your computer and use it in GitHub Desktop.
Bubblesort Algorithm in C
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 <string.h> | |
#include <stdbool.h> | |
void bubblesort(int *arr, int size) | |
{ | |
int i = 0, temp; | |
int j = 1; | |
bool swapped = true; | |
while(swapped) | |
{ | |
swapped = false; | |
i = 0; | |
for(i; i< size-j; i++) | |
{ | |
if (arr[i] > arr[i+1]) | |
{ | |
temp = arr[i]; | |
arr[i] = arr[i+1]; | |
arr[i+1] = temp; | |
swapped = true; | |
} | |
} | |
j++; | |
} | |
} | |
main() | |
{ | |
int arr[] = { 12,4,6,7,33,43,87,32,7,34,2,8,43,23,7,3}; | |
int i; | |
int size = sizeof(arr)/sizeof(arr[0]); | |
for (i=0; i<size; i++) | |
printf(" %d",arr[i]); | |
bubblesort(arr, size); | |
printf("\n"); | |
for (i=0; i<size; i++) | |
printf(" %d",arr[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment