Computer Science, asked by learner911, 4 months ago

Given an array of integers, sort the array in ascending order using the bubble algorithm.
Once sorted, print the following three lines:
1. Array is sorted in numSwaps swaps., where is the number of swaps that took place.
u. First Element: firstElement, where is the first element in the sorted array.
iii. Last Element: lastElement, where is the last element in the sorted array.​

Answers

Answered by Anonymous
3

Answer:

1 = Array is sorted in numSwaps swaps., where  is the number of swaps that took place.

2 = First Element: firstElement, where  is the first element in the sorted array.

3 = Last Element: lastElement, where  is the last element in the sorted array

Explanation:

Answered by karthik0704
1

Answer:

#include<stdio.h>

#define MAX 6

void bubble_sort(int arr[]);

int main(void)

{  

 

   int arr[MAX];

   

   for(int i = 0; i < MAX; i++)

   {

       printf("arr[%d] = ", i);    

       scanf("%d", &arr[i]);

   }

   printf("\nUnsorted array: \n");

   for(int i = 0; i < MAX; i++)

   {

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

   }

   bubble_sort(arr);

   printf("\n\nSorted array: \n");

   for(int i = 0; i < MAX; i++)

   {

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

   }

}

void bubble_sort(int arr[])

{

   int tmp,  

       is_swapped;

   for(int i = 0; i < MAX; i++)

   {

     

       is_swapped = 0;  

       for(int j = 0; j < MAX - 1 - i; j++)

       {            

           if(arr[j] > arr[j+1])

           {

           

               tmp = arr[j];

               arr[j] = arr[j+1];

               arr[j+1] = tmp;

             

               is_swapped = 1;              

           }    

       }        

       if (!is_swapped)

       {

           break;

       }

   }        

}

Explanation:so combine bubble algorithm with the  ascending and descending

Similar questions