Monday, October 26, 2020

C++ Programming - Bubble Sort Function

    C++ is a very popular programming language. Bubble Sorting is a technic to sort data in array. In this code you can see, how to do Bubble Sort by using C++ code.  




#include<iostream>
using namespace std;


void sortArr(int Arr[], int c); //to declare the function
void printArr();
int *x; //declare the pointer variable
int main(){
int realArr[4]={4,2,3,1}; //array we need to sort
sortArr(realArr,4); //call the function
printArr(); //to print the sorted array

}
//function to sort array elements
void sortArr(int Arr[], int c){

for(int i=0; i<c-1; i++){
for(int j=0; j<c-1-i; j++){
if(Arr[j]>Arr[j+1]){
int temp = Arr[j];
Arr[j]=Arr[j+1];
Arr[j+1]=temp;
}
}
}
x = &Arr[0]; // store the address in pointer variable
}
//function for print sorted array values
void printArr(){
for(int i=0; i<4; i++){
cout<<*(x+i)<<" ";
}

}



    If you need to check this code without installing an IDE of C++, you can refer the below link and copy paste the code, Click Here


No comments:

Post a Comment

C++ Programming - Bubble Sort Function

    C++ is a very popular programming language. Bubble Sorting is a technic to sort data in array. In this code you can see, how to do Bubb...