Search
 
SCRIPT & CODE EXAMPLE
 

CPP

bubble sort c++

void bubbleSort(int arr[], int n) {
    for (int i=1; i<n; i++) {
        for (int j=i; j<n; j++) {
            if (arr[j] < arr[j-1]) {
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;
            }
        }
    }
}
Comment

bubble sort algorithm c++ step by step

#include<iostream>
using namespace std;
int main()
{
    int i, arr[10], j, temp;
    cout<<"Enter 10 Elements: ";
    for(i=0; i<10; i++)
        cin>>arr[i];
    cout<<endl;
    for(i=0; i<9; i++)
    {
        for(j=0; j<(10-i-1); j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
        cout<<"Step "<<i+1<<": ";
        for(j=0; j<10; j++)
            cout<<arr[j]<<" ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
Comment

cpp bubble sort

//HOW ABOUT MORE EFFICIENT SOLUTION
//  BASIC IDEA- REPEATEDLY SWAP TWO ADJACENT ELEMENTS IF THEY ARE IN A WRONG ORDER.
#include<bits/stdc++.h>
using namespace std;
void bubblesort(int a[], int n)
{
	bool swapped=false;
	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<n-i-1;j++)
		{
			if(a[j]>a[j+1]) // check if ADJACENT elements are in a wrong order.
			{
				swap(a[j],a[j+1]); //if they are swap them.
				swapped=true;
			}
		}
		if(swapped==false) break; // if for any particular iteration our array doesn't swap--
        // -- any numbers then we may conclude that our array has already been sorted. :)
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++) cin>>a[i];
	bubblesort(a,n);
	for(int i=0;i<n;i++) cout<<a[i]<<" ";
	return 0;
}
	
Comment

bubble sort in c+

#include <iostream>
#include <iomanip>

using namespace std;

void sort (int array[],int size){
    for(int i=0; i<size-1; i++){           
      for(int j=i+1; j<size; j++){      
        if(array[i] > array[j]){      
            int temp = array[j];
            array[j] = array[i];
            array[i] = temp;
        }
      }
    }
    cout<<"Sorted Array: 
";
    for(int i=0;i<5;i++){
        cout<<setw(5)<<array[i];
    }
}

int main()
{
    int array [5];
    for(int i=0;i<5;i++){
        cout<<"Element "<<i<<": ";
        cin>>array[i];
    }
    sort(array,5);
  
}
Comment

c++ code for bubble sort

#include<bits/stdc++.h>

using namespace std;
int main()
{
    //bubble sort;
    int a[5]= {81,56,2,12,9};
    bool flag = false;
    int i,j;
    //if(n>1)
    while(!flag)
    {
        flag = true;
        i=0;
        j=1;
        
        while(j<5)
        {
            if(a[i]>a[j])
            {
                swap(a[i],a[j]);
                 flag = false;
            }
            i++;
            j++;
           
        }
    }
    for(i=0;i<5;i++)
    {
        cout<<a[i];
    }

    return 0;
}
Comment

bubblesort c++

#include<iostream>
using namespace std;
int main ()
{
   int i, j,temp,pass=0;
   int a[10] = {10,2,0,14,43,25,18,1,5,45};
   cout <<"Input list ...
";
   for(i = 0; i<10; i++) {
      cout <<a[i]<<"	";
   }
cout<<endl;
for(i = 0; i<10; i++) {
   for(j = i+1; j<10; j++)
   {
      if(a[j] < a[i]) {
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
      }
   }
pass++;
}
cout <<"Sorted Element List ...
";
for(i = 0; i<10; i++) {
   cout <<a[i]<<"	";
}
cout<<"
Number of passes taken to sort the list:"<<pass<<endl;
return 0;
}
Comment

bubble sort c++

#include <bits/stdc++.h>
using namespace std;
int main (void) {
    int a[] = {5, 4, 3, 2, 1}, tempArr, i, j;
    for (i = 0; i < 5; i++) {
        for (j = i + 1; j < 5; j++) {
            if (a[j] < a[i]) {
                tempArr = a[i];
                a[i] = a[j];
                a[j] = tempArr;
            }
        }
    }
    for(i = 0; i < 5; i++) {
        cout<<a[i]<<"
";  
    }  
    return 0; 
}
Comment

bubble sort algorithm in c++

// Bubble Sort algorithm -> jump to line 21

...

#include <iostream>      // Necessary for input output functionality
#include <bits/stdc++.h> // To simplify swapping process

...

...

/**
* Sort array of integers with Bubble Sort Algorithm
*
* @param arr   Array, which we should sort using this function
* @param arrSZ The size of the array
* @param order In which order array should be sort
*
* @return Sorted array of integers
*/
void bubbleSortInt(double arr[], int arrSz, string order = "ascending")
{
    for (int i = 0; i < arrSz; ++i)
    {
        for (int j = 0; j < (arrSz - i - 1); ++j)
        {
            // Swapping process
            if ((order == "descending") ? arr[j] < arr[j + 1] : arr[j] > arr[j + 1])
            {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
    return; // Optional because it's a void function
} // end bubbleSortInt

...

int main()
{

...

    return 0; // The program executed successfully.

} // end main
Comment

bubble sort function in c++

void bubbleSort(int arr[], int n)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(arr[i]>arr[j])
                {
                    int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
    }
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ nested switch statements 
Cpp :: vector of strings initialization c++ 
Cpp :: define unicode c++ 
Cpp :: memcpy c++ usage 
Cpp :: int_max cpp 
Cpp :: max of a vector c++ 
Cpp :: height of bst cpp 
Cpp :: fstring from float c++ ue4 
Cpp :: doubly linked list c++ code 
Cpp :: C++ string initialization 
Cpp :: sieve of eratosthenes algorithm in c++ 
Cpp :: how to do sets in cpp 
Cpp :: memcpy library cpp 
Cpp :: double to int c++ 
Cpp :: count bits c++ 
Cpp :: array max and minimum element c++ 
Cpp :: how to print in cpp 
Cpp :: c++ get string between two characters 
Cpp :: console colors in C++ 
Cpp :: c++ pi float 
Cpp :: vector::insert 
Cpp :: cannot jump from switch statement to this case label c++ 
Cpp :: Reverse Level Order Traversal cpp 
Cpp :: bubblesort c++ 
Cpp :: priority queue smallest first 
Cpp :: remove element from vector c++ 
Cpp :: c++ string split 
Cpp :: c++ base constructor 
Cpp :: c++ thread 
Cpp :: c ++ splitlines 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =