23 thoughts on “8.3.2 Bubble Sort | Sorting in C++ | Guaranteed Placement Course

  1. can anyone explain this
    arrey- 31 11 27 2 19
    it has 5 elements and according to this video this array should be sorted in 4 steps but in this case, it gets sorted in only 3 steps.
    if this is happening then the compiler will iterate this one more time and which will increase compilation time.
    can anyone explain this
    Thank You.

  2. apart from using 'while + for' loop, one can also use double-for loops:

    for(int i=0;i<n-1;i++){

    for(int j=0;j<(n-i);j++){

    if(arr[j]>arr[j+1]){

    int temp=arr[j];

    arr[j]=arr[j+1];

    arr[j+1]=temp;

    }

    }

    }

  3. we can execute this code by using for loop also.

    #include<iostream>
    using namespace std;
    int main(){
    int n;
    cin>>n;

    int arr[n];
    for(int i=0;i<n;i++){
    cin>>arr[i];
    }

    for(int counter=1;counter<n;counter++){
    for(int i=0;i<n-counter;i++){
    if(arr[i]>arr[i+1]){
    int temp=arr[i+1];
    arr[i+1]=arr[i];
    arr[i]=temp;
    }
    }
    }
    for(int i=0;i<n;i++){
    cout<<arr[i]<<endl;

    }
    }

  4. Another way to write the same code using nested for loops:
    #include<iostream>

    using namespace std;

    int main()

    {

    int c;

    cin>>c;

    int arr[c];

    for(int i=0;i<c;i++)

    {

    cin>>arr[i];

    }

    for(int i=c-1;i>=0;i–)

    {

    for(int j=0;j<i;j++)

    {

    if(arr[j+1]<arr[j])

    {

    int temp=arr[j];

    arr[j]=arr[j+1];

    arr[j+1]=temp;

    }

    }

    }

    for(int i=0;i<c;i++)

    {

    cout<<arr[i]<<" ";

    }

    cout<<endl;

    }

  5. Bubble short ka ye bhi shi hai:
    #include<iostream>
    using namespace std;
    int main()
    {
    int temp=0;
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    {
    cin>>a[i];
    }
    for(int j=0;j<n-1;j++)
    {
    for(int k=0;k<n-j;k++)
    {
    if(a[k]>a[k+1])
    temp=a[k];
    a[k]=a[k+1];
    a[k+1]=temp;
    }
    }
    for(int i=0;i<n;i++)
    {
    cout<<a[i]<<" ";
    }
    return 0;
    }

  6. Can anyone tell me if I can go with this code for bubble sort? :")

    include<iostream>

    using namespace std;

    int main()

    {

    int n,temp;

    cout<<"Enter the number of elements : ";

    cin>>n;

    int arr[n];

    for(int i = 0; i<n; ++i)

    {

    cout<<"Enter the elemenet number "<<i+1<<" : ";

    cin>>arr[i];

    }

    for(int l = 1; l<=n; ++l)

    {

    for(int j = 0; j<n-1; ++j)

    {

    if (arr[j]>arr[j+1])

    {

    temp = arr[j];

    arr[j]=arr[j+1];

    arr[j+1]=temp;

    }

    }

    }

    for(int k = 0; k<n; ++k)

    {

    cout<<arr[k]<<" ";

    }

    }

  7. Teaching qualities are very well but speed is very high….
    Please try to reduce teaching speed…
    Please upload video on Java and data structure and algorithm

    🥰❤️🥰 love from tier 3 college ❤️🥰❤️

Leave a Reply

Your email address will not be published. Required fields are marked *