Code and Implementation of Bubble Sort
// Program for implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;
// Function to implement bubble sort
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])
swap(arr[j], arr[j + 1]);
}
// Function to print Array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Main Code
int main()
{
int arr[] = { -3, 15, 0, 8, -7};
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
OUTPUT:
-7 -3 0 8 15
8
