Answers for "bubble sort complety"

1

bubble sort

/*bubble sort;timecomplexity=O(n){best case}
               time complexity=O(n^2){worst case}
               space complexity=O(n);auxiliary space commplexity=O(1)
*/
#include <iostream>

using namespace std;
void swap(int*,int*);
void bubble_sort(int arr[],int n)
{
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n-1-i;j++)
        {
            if(arr[j]>arr[j+1])
            {
                swap(&arr[j],&arr[j+1]);
            }
        }
    }
}
void display(int arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int array_of_numbers[n];
    cout<<"enter the elements of the array"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>array_of_numbers[i];
    }
    cout<<"array as it was entered:"<<endl;
    display(array_of_numbers,n);
    bubble_sort(array_of_numbers,n);
    cout<<"array after bubble sort:"<<endl;
    display(array_of_numbers,n);
    return 0;
}
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
Posted by: Guest on May-25-2021
1

bubble sort

/*bubble sort;timecomplexity=O(n){best case}
               time complexity=O(n^2){worst case}
               space complexity=O(n);auxiliary space commplexity=O(1)
*/
#include <iostream>

using namespace std;
void swap(int*,int*);
void bubble_sort(int arr[],int n)
{
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n-1-i;j++)
        {
            if(arr[j]>arr[j+1])
            {
                swap(&arr[j],&arr[j+1]);
            }
        }
    }
}
void display(int arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int array_of_numbers[n];
    cout<<"enter the elements of the array"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>array_of_numbers[i];
    }
    cout<<"array as it was entered:"<<endl;
    display(array_of_numbers,n);
    bubble_sort(array_of_numbers,n);
    cout<<"array after bubble sort:"<<endl;
    display(array_of_numbers,n);
    return 0;
}
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
Posted by: Guest on May-25-2021

Python Answers by Framework

Browse Popular Code Answers by Language