Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created April 1, 2019 11:14
Show Gist options
  • Select an option

  • Save jatinsharrma/334bd15f87f4909618bc219acfab4755 to your computer and use it in GitHub Desktop.

Select an option

Save jatinsharrma/334bd15f87f4909618bc219acfab4755 to your computer and use it in GitHub Desktop.
Sorting an array - Bubble Sort
#include <stdio.h>
int swap(int* i, int* j){
int temp = *i;
*i = *j;
*j = temp;
}
int BubbleSort(int array[], int size){
for (int i =0 ; i < size-1 ; i++){
for (int j = 0 ; j<size-i-1 ; j++){
if (array[j]>array[j+1]){
swap(&array[j+1],&array[j]);
}
}
}
}
void print(int array[], int size){
for (int i = 0 ; i<size ; i++){
printf("%d\n",array[i]);
}
}
void main() {
int array[] = {6,8,7,6,5,9,3,2,1,0};
int size = sizeof(array)/sizeof(array[0]);
BubbleSort(array,size);
print(array,size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment