Skip to content

Instantly share code, notes, and snippets.

@hmenn
Created October 29, 2016 12:08
Show Gist options
  • Select an option

  • Save hmenn/0aa707da9903bc1e74f9e49e8dccd0c8 to your computer and use it in GitHub Desktop.

Select an option

Save hmenn/0aa707da9903bc1e74f9e49e8dccd0c8 to your computer and use it in GitHub Desktop.
Bubble sort on array
#include <stdio.h>
// Best case : Omega(n)
// worst case : O(n^2)
// awerage : Theta(n^2)
void bubbleSort(int arr[],int size);
void swap(int arr[],int x,int y);
int main(){
int size=5;
int arr[size];
int i=0;
for(i=0;i<5;++i)
arr[i]=size-i;
bubbleSort(arr,size);
for(i=0;i<size;++i)
printf("arr[%d]:%d ",i,arr[i]);
printf("\n");
}
void bubbleSort(int arr[],int size){
int counter= size-1;
int change=1;
int j=0;
while(change==1){
change=0;
for(j=0;j<counter;++j){
if(arr[j]>arr[j+1]){
swap(arr,j+1,j);
change=1;
}
}
--counter;
}
}
void swap(int arr[],int x,int y){
int temp = arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment