Created
October 29, 2016 12:08
-
-
Save hmenn/0aa707da9903bc1e74f9e49e8dccd0c8 to your computer and use it in GitHub Desktop.
Bubble sort on array
This file contains hidden or 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> | |
| // 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