Check out example codes for "Write a program to sort an array 100,200,20, 75,89.198, 345,56,34,35 using Bubble Sort. The program should be able to display total number of passes used for sorted data in given data set.". It will help you in understanding the concepts better.

Code Example 1

void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 
  
// An optimized version of Bubble Sort 
void bubbleSort(int arr[], int n) 
{ 
   int i, j; 
   bool swapped; 
   for (i = 0; i < n-1; i++) 
   { 
     swapped = false; 
     for (j = 0; j < n-i-1; j++) 
     { 
        if (arr[j] > arr[j+1]) 
        { 
           swap(&arr[j], &arr[j+1]); 
           swapped = true; 
        } 
     } 
  
     // IF no two elements were swapped by inner loop, then break 
     if (swapped == false) 
        break; 
   } 
}

Code Example 2

void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 
  
// An optimized version of Bubble Sort 
void bubbleSort(int arr[], int n) 
{ 
   int i, j; 
   bool swapped; 
   for (i = 0; i < n-1; i++) 
   { 
     swapped = false; 
     for (j = 0; j < n-i-1; j++) 
     { 
        if (arr[j] > arr[j+1]) 
        { 
           swap(&arr[j], &arr[j+1]); 
           swapped = true; 
        } 
     } 
  
     // IF no two elements were swapped by inner loop, then break 
     if (swapped == false) 
        break; 
   } 
}

Code Example 3

INTUTION: 
Tha idea of bubble sort is inspired by Bubbles,
as soon as you heated Water in bicker the larger 
bubble moves towards the end in the same way. Just move
the larger bubble towards the end!!

Code: 

#include <bits/stdc++.h> 
using namespace std; 
  
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]); 
            
      int i;  
    for (i = 0; i < size; i++)  
        cout << arr[i] << " ";  
    cout << endl;  
}  
  
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;  
}

Code Example 4

// Por ter uma complexidade alta,
// não é recomendado para um conjunto de dados muito grande.
// Complexidade: O(n²) / O(n**2) / O(n^2)
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function selectionSort(vetor) {
    let minor;
    for (let i = 0; i < vetor.length - 1; i += 1) {
        minor = i;
        for (let j = i + 1; j < vetor.length; j += 1) {
            if (vetor[j] < vetor[minor]) {
                minor = j;
            }
        }
        if (i !== minor) {
            [vetor[i], vetor[minor]] = [vetor[minor], vetor[i]];
        }
    }
    return vetor;
}

selectionSort([1, 2, 5, 8, 3, 4]);

Learn ReactJs, React Native from akashmittal.com