C++ program for implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;
void swap(int *x, int *y) /*Defining Swap function of void return type*/
{
int temp = *x; //Swaping the values
*x = *y;
*y = temp;
}
void bubbleSort(int array[], int n) /*Defining function to implement bubbleSort */
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++) /*The last i components are already in location */
if (array[j] > array[j+1])
swap(&array[j], &array[j+1]); //Calling swap function
}
int main() //driver function
{
int array[] = {3, 16, 7, 2, 56, 67, 8}; //Input array
int n = sizeof(array)/sizeof(array[0]); //Finding size of array
bubbleSort(array, n); //Function calling
cout<<"Sorted array: \n";
for (int i = 0; i < n; i++) //printing the sorted array
cout << array[i] << " ";
cout << endl;
return 0;
}
<u>Output</u>
Sorted array:
2 3 7 8 16 56 67