Bubble Sort

//there is a simple program for sorting an array in increasing order by bubble sort

language=C++

#include <iostream>

using namespace std;

int main()
{
    int n;

    cout << "how many numbers:";

    cin >>n;

    int a[n],counter;

    for(counter = 0; counter < n; counter++)
    {
        cin >> a[counter];
    }

    int k = 1;
    while(k<=n-1)
    {
        int ptr = 0;
        while( ptr <= n - k + 1 )
        {

            if( a[ptr] > a[ptr+1] )
            {
                int temp;
                temp = a[ptr];
                a[ptr] = a[ptr+1];
                a[ptr+1] = temp;
            }
            ptr += 1;
        }

        k++;

    }

    for( int i = 0;i < n; i++ )
        cout << a[i] <<" ";

    return 0;
}

No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...