Bubble Sort-1

QUESTION

Sort the given set of numbers using Bubble Sort. The first line of the input contains the number of elements, the second line of the input contains the numbers to be sorted. In the output print the status of the array at the 3rd iteration and the final sorted array in the given format.

“TESTCASE_1”: “7\n64 34 25 12 22 11 90\n###—###SEPERATOR—###—\n12 22 11 25 34 64 90 \nSorted array:11 12 22 25 34 64 90”, “TESTCASE_2”: “9\n64 34 25 12 22 11 90 35 26\n###—###SEPERATOR—###—\n12 22 11 25 34 26 35 64 90 \nSorted array:11 12 22 25 26 34 35 64 90”, “TESTCASE_3”: “8\n14 83 25 47 9 77 1 0\n###—###SEPERATOR—###—\n14 9 25 1 0 47 77 83 \nSorted array:0 1 9 14 25 47 77 83”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{int n,arr[100];
 cin>>n;
 for(int i=0;i<n;i++)
   cin>>arr[i];
 for(int i=0;i<n-1;i++)
 {for(int j=0;j<n-i-1;j++)
  {if(arr[j]>arr[j+1])
   {int emp=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=emp;
   }
  }
  if(i==2)
  {for(int k=0;k<n;k++)
     cout<<arr[k]<<' ';
  }
  
 }
 cout<<"\nSorted array:";
 for(int k=0;k<n;k++)
   cout<<arr[k]<<' ';
 

	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.