bubble sort

 

Bubble sort 






OURSHOPKEEPER














#include <iostream>
using namespace std;

void bubble_sort(int a[], int n);
void swap(int &a, int &b);
int main()
{
    int a[50];
    int i, j, n;
    cout << "Enter the number if elements in an array: " << endl;
    cin >> n;
    cout << "Enter the elements in array: " << endl;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    cout << "array after sorting" << endl;
    bubble_sort(a, n);
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
}
void bubble_sort(int a[], int n)
{
    int i, j;
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (a[j] > a[j + 1])
                swap(a[j], a[j + 1]);
        }
    }
}
void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}



    


Click on the above button to download the code.



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post