SORT3
QUESTION DESCRIPTION
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.
Mandatory declaration for function is "void printArr(int arr[], int size)"
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.
Mandatory declaration for function is "void printArr(int arr[], int size)"
TEST CASE 1
INPUT
INPUT
7
64 34 25 12 22 11 90
OUTPUT12 22 11 25 34 64 90
Sorted array:11 12 22 25 34 64 90
TEST CASE 2
INPUT
INPUT
9
64 34 25 12 22 11 90 35 26
OUTPUT12 22 11 25 34 26 35 64 90
Sorted array:11 12 22 25 26 34 35 64 90
#include <iostream>
using namespace std;
void printArr(int arr[], int size)
{
cout<<"Sorted array:";
for(int k=0;k<size;k++)
cout<<arr[k]<<" ";
}
int main()
{
int i,j,size,arr[100];
cin>>size;
for(i=0;i<size;i++)
cin>>arr[i];
for(i=0;i<size-1;i++)
{
for(j=0;j<size-i-1;j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
if(i == 2)
{
for(int a=0;a<size;a++)
cout<<arr[a]<< " ";
cout<<endl;
}
}
printArr( arr, size);
return 0;
}
Comments
Post a Comment