WHAT IS BUBBLE SORT ARRAY IN C++

 Que- what is bubble sort technique?

Ans- Bubble Sort is the simplest sorting technique that works by repeatedly swapping the adjacent elements if they are in the wrong order. This technique is not suitable for large data sets as its average and worst case time complexity is quite high.

Worst condition in bubble sort array? 

The worst-case condition for bubble sort occurs when elements of the array are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort a given array is (n-1). where ‘n’ is a number of elements present in the array.

Can sorting happens in place in Bubble sort?

Yes, Bubble sort performs swapping of adjacent pairs without use of any major data structure. Hence Bubble sort algorithm is an in-place algorithm.

Simple example of bubble sort array is:-

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
    int num1;
    cout<<"enter how many numbers in array:- ";
    cin>>num1;
    int arr [num1];
    for (int i = 0; i < num1; i++)
    {
        cout<<"enter numbers in array:- ";
        cin>>arr[i];
    }
    int count=1;
    while (count<num1)
    {
        for(int i=0; i<num1; i++)
            if (arr[i]>arr[i+1])
            {
                int temp;
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        count++;
    }
    for (int i = 0; i < num1; i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
}

Result:-




Post a Comment

Previous Post Next Post