Bubble Sort In Data Structure Using C++

Data Structure Using C++

Array Data Structure

Sorting

Bubble Sort

void bubbleSort(int arr[], int n);
int main(){
 int arr[]={12,11,13,5,6,4,10,3,14,2};
 int n=sizeof(arr)/sizeof(arr[0]); 
 bubbleSort(arr,n);
 for(int i=0;i<n;i++){
  cout<<arr[i]<<"\t";
 }
}
void bubbleSort(int arr[], int n)
{
 int i, j;
 for (i=0; i<n-1; i++)
 {  
        for (j=0; j<n-i-1; j++)
     {
      if (arr[j] > arr[j+1])
      {
       int temp=arr[j];
       arr[j]=arr[j+1];
       arr[j+1]=temp;
   }
  }           
 }       
}

Let me know in the comment section if you have any question.

Previous Post:
Selection Sort In Data Structure Using C++


Comments