Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Algorithms > Sorting > Bubble Sort

Bubble sort works by repeatedly swapping the adjacent elements if they are in wrong order

int main() {
    int temp;
    int arr[] = {3, 2, 1, 4, 10, 6, 0};
    int n= 7;
    
    for (int i=0; i<n-1; i++){
        for (int j = 0; j < n-i-1; j++) {
            if (arr[j+1] < arr[j]) {
                temp = arr[j+1];
                arr[j+1] = arr[j];
                arr[j] = temp;
            }
        }
        
    }

    for (int i = 0; i < n; i++){
        cout << arr[i] << '\t';
    }
    
    return 0;
}
Credits: GFG, SlackEdit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment