Computer Science, asked by priyabadiger100, 1 year ago

The number of swapping required to sort the array [8, 22, 7, 9, 31, 19, 5, 13] in ascending order using Bubble sort is


Abhiraj1928: Whats the language needs? I guess it it java (just confirming)

Answers

Answered by dhananjaykashyap
0

//C Code  for bubble sort find answer in attachment

#include<stdlib.h>

#include<stdio.h>

int bubblesort(int *arr){

int loop1,loop0,temp,count=0;

for(loop1=1;loop1<=8;loop1++){

for(loop0=0;loop0<loop1;loop0++){

   if(arr[loop0]>arr[loop1]){

       temp=arr[loop0];

       arr[loop0]=arr[loop1];

       arr[loop1]=temp;

       printf("%d:%d\n",++count,arr[loop0]);

   }

}

printf("%d:%d\n",++count,arr[loop0]);

}

return count;

}

int main(){

int loop,count,arr[]={8,22,7,9,31,19,5,13};/*noi*/

count=bubblesort(arr);

for(loop=0;loop<8;loop++){

       printf("%d ",arr[loop]);

   }

printf("\nCount=%d",count);

return 0;

}

Attachments:
Similar questions