Bubble sort

PHOTO EMBED

Thu May 22 2025 03:24:50 GMT+0000 (Coordinated Universal Time)

Saved by @RohitChanchar #c++

// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    cout<<"after sorted using bubble sort: ";
    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],arr[j+1]);
            //   int temp=arr[j];
            //   arr[j]=arr[j+1];
            //   arr[j+1]=temp;
            }
        }
    }
        for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
        }
    }
content_copyCOPY

This is the code for bubble sort, which places the greatest element at the end in each iteration, then reduces the number of iterations and repeats the process to place the next greatest element in the second-last position. By continuing this way, the array gets sorted. The code includes both an implementation using built-in functions and one without using them, so either can be used.