Answers for "Bubble Sort C++"

2

bubble sort in c++

void bubble_sort( int A[ ], int n ) {
    int temp;
    for(int k = 0; k< n-1; k++) {
        // (n-k-1) is for ignoring comparisons of elements which have already been compared in earlier iterations

        for(int i = 0; i < n-k-1; i++) {
            if(A[ i ] > A[ i+1] ) {
                // here swapping of positions is being done.
                temp = A[ i ];
                A[ i ] = A[ i+1 ];
                A[ i + 1] = temp;
            }
        }
    }
}
Posted by: Guest on May-04-2021
4

bubble sort in java

public static void bubbleSort(int arr[])
{
	for (int i = 0; i < arr.length; i++) //number of passes
    {
		//keeps track of positions per pass      
    	for (int j = 0; j < (arr.length - 1 - i); j++) //Think you can add a -i to remove uneeded comparisons 
        {
          	//if left value is great than right value 
        	if (arr[j] > arr[j + 1])
            {
              	//swap values
            	int temp = arr[j];
              	arr[j] = arr[j + 1];
              	arr[j + 1] = temp; 
            }
        }
    }
}
Posted by: Guest on April-15-2020
1

bubble sort in c++

/* Bubble sort code in 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]<<"\n";  
    }  
    return 0; 
}
Posted by: Guest on August-18-2021
3

bubble sort c

#include <bits/stdc++.h> 
using namespace std; 
  
void swap(int *xp, int *yp)  
{  
    int temp = *xp;  
    *xp = *yp;  
    *yp = temp;  
}  
  
// A function to implement bubble sort  
void bubbleSort(int arr[], int n)  
{  
    int i, j;  
    for (i = 0; i < n-1; i++)      
      
    // Last i elements are already in place  
    for (j = 0; j < n-i-1; j++)  
        if (arr[j] > arr[j+1])  
            swap(&arr[j], &arr[j+1]);  
}  
  
/* Function to print an array */
void printArray(int arr[], int size)  
{  
    int i;  
    for (i = 0; i < size; i++)  
        cout << arr[i] << " ";  
    cout << endl;  
}  
  
// Driver code  
int main()  
{  
    int arr[] = {64, 34, 25, 12, 22, 11, 90};  
    int n = sizeof(arr)/sizeof(arr[0]);  
    bubbleSort(arr, n);  
    cout<<"Sorted array: \n";  
    printArray(arr, n);  
    return 0;  
}
Posted by: Guest on July-02-2020
5

c++ sort

int arr[]= {2,3,5,6,1,2,3,6,10,100,200,0,-10};
    int n = sizeof(arr)/sizeof(int);  
    sort(arr,arr+n);

    for(int i: arr)
    {
        cout << i << " ";
    }
Posted by: Guest on December-09-2019
0

Bubble Sort C++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int n,temp,c=0;
    cin >> n;
   int a[n];
    for(int i=0;i<n;i++)
        {
        cin>>a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n-i-1;j++)
            {
            if(a[j]>a[j+1])
                {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                c++;
            }
        }
    
    if(c==0)
        {
        break;
    }}
    cout<<"Array is sorted in "<<c<<" swaps."<<endl;
    cout<<"First Element:"<<" "<<a[0]<<endl;
    cout<<"Last Element:"<<" "<<a[n-1]<<endl;
    return 0;
}
Posted by: Guest on April-26-2021

Browse Popular Code Answers by Language