Bubble Sort Using Dynamic Array

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn to write an algorithm to implement bubble sort technique in C++. Bubble sort is one of the most popular and simple sorting techniques, where we compare two adjacent elements in an array and swap their positions if they aren't arranged correctly.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/ControlFlowGroup -.-> cpp/break_continue("`Break/Continue`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/variables -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/data_types -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/operators -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/conditions -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/for_loop -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/break_continue -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/arrays -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} cpp/function_parameters -.-> lab-96173{{"`Bubble Sort Using Dynamic Array`"}} end

Declare the Required Variables and Header Files

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

We first begin by declaring the variables and the header files needed for the program. In this step, we will include the header file iostream and define the namespace std.

#include<iostream>
using namespace std;

Declare a Function to Implement General Bubble Sort Algorithm

Here, we will declare a function "bubble_sort" in the C++ program that accepts the array to be sorted and the size of the array. The function will contain the implementation of the bubble sort technique with the help of for loops, swapping, and if/else statements.

int bubble_sort(int n,int array[]){
    int temp;
    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-i-1;j--){
            if(array[j]>array[j+1]){
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
        }
    }
return 0;
}

Declare a Function to Implement Optimized Bubble Sort Algorithm

In the optimized bubble sort, we will check in each pass if any elements were swapped or not. If no swaps occurred in the last pass, it implies that we have sorted the array. In this step, we will implement the "bubble_sort" function to implement an optimized bubble sort algorithm. The function accepts the array to be sorted and the size of the array.

int bubble_sort(int n,int array[]){
    int temp, flag;
    for(int i=0;i<n-1;i++){
        flag = 0;
        for(int j=0;j<n-i-1;j++){
            if(array[j]>array[j+1]){
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
                flag=1;
            }
        }
        if(flag==0){
            break;
        }
    }
    return 0;
}

Define the Main Function

Here, we will define the main function in which we will declare the array to be sorted and its size. In this step, we will also call the function "bubble_sort" to sort the array in ascending order.

int main(){

    int arr[]={5,6,9,2,3};
    int n = sizeof(arr)/(sizeof(arr[0]));
    bubble_sort(n,arr);
    cout<<"Elements after sorting of the array:- "<<endl;
    for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
    }
    return 0;
}

Compile and Run the Code

In this step, we will compile and run the code in the terminal of Ubuntu system. To compile the code, type the following command:

g++ ~/project/main.cpp -o main

To run the code, type the following command:

./main

Summary

In this lab, we have successfully learned to implement bubble sort using dynamic array in C++. We also learned about an optimized bubble sort technique that works effectively for some selected but efficient arrays. By following the above tutorial, you can implement the bubble sort technique to sort an array in ascending or descending order using dynamic array in C++.

Other C++ Tutorials you may like