#include <stdio.h> void printArray(int *a,int n){ for(int i=0; i<n; i++){ printf("%d ",a[i]); } printf("\n"); } void bubbleSort(int*a, int n){ //for each pass int temp; int isSorted=0; for(int i=0;i<n-1;i++){ printf("number of passess = %d\n",i+1); //for comparison in each pass isSorted=1; for(int j=0;j<n-i-1;j++){ if(a[j]>a[j+1]){ // swap(a[j],a[j+1]); temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; isSorted=0; } } //making bubble sort adaptive if(isSorted) return; } } int main(){ int a[]={65,54,23,12,9,7}; int n=6; bubbleSort(a,n); printArray(a,n); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter