Answers for "bubble sort c++ program"

2

bubble sort in c++

void bubble_sort( int A[ ], int n ) {
    int temp;
    for(int k = 0; k< n-1; k++) {
        // (n-k-1) is for ignoring comparisons of elements which have already been compared in earlier iterations

        for(int i = 0; i < n-k-1; i++) {
            if(A[ i ] > A[ i+1] ) {
                // here swapping of positions is being done.
                temp = A[ i ];
                A[ i ] = A[ i+1 ];
                A[ i + 1] = temp;
            }
        }
    }
}
Posted by: Guest on May-04-2021
1

bubble sort c++ template

// template <class t>
// void bubble <t>::sort(int n)
template < typename T > void bubble_sort( T a[], int n )
{
    int i,j;
    //t temp;
    T temp ;
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(a[i]>a[j]) //bubble sort algo
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
}
Posted by: Guest on January-04-2021
0

bubble sort integers

for (int i = 0; i < n-1; i++) 
            for (int j = 0; j < n-i-1; j++) 
                if (arr[j] > arr[j+1]) 
                { 
                    // swap arr[j+1] and arr[i] 
                    int temp = arr[j]; 
                    arr[j] = arr[j+1]; 
                    arr[j+1] = temp; 
                }
Posted by: Guest on September-01-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language