1. Bubble Sort

Idea:

Repeatedly swap adjacent elements if they are in wrong order → Largest element “bubbles” to the end each pass

Complexity:

void bubbleSort(int arr[], int n)
{
    for(int i=n-1;i>0;i--){
        bool swapped = false;
        for(int j=0;j<i;j++){
            if(arr[j]>arr[j+1]){
                swap(arr[j],arr[j+1]);
                swapped = true;
            }
        }
        if(!swapped) break;
    }
}

⚡ Optimization (IMPORTANT):

If no swaps in a pass → array already sorted → break early

👉 This is what theswapped flag does

2. Selection Sort

Idea:

Pick the smallest element from unsorted part → place it at correct position (i)

Complexity: