# Write C++ program to sort an array in ascending order

```
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array in ascending order: \n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
```

Explanation:
- The program starts by including the necessary header file `iostream`

which allows input/output operations.
- The `using namespace std;`

statement is used to avoid having to write `std::`

before standard library functions.
- The function `bubbleSort`

is defined to sort the array in ascending order. It takes two parameters: the array `arr`

and the size of the array `n`

.
- The outer loop `for (int i = 0; i < n - 1; i++)`

iterates `n-1`

times, where `n`

is the size of the array. It controls the number of passes needed to sort the array.
- The inner loop `for (int j = 0; j < n - i - 1; j++)`

iterates from the first element to the last unsorted element in the array. It compares adjacent elements and swaps them if they are in the wrong order.
- The condition `if (arr[j] > arr[j + 1])`

checks if the current element is greater than the next element. If true, it swaps the elements using a temporary variable `temp`

.
- The `main`

function is where the program execution starts. It initializes an array `arr`

with some values and calculates the size of the array `n`

using the `sizeof`

operator.
- The `bubbleSort`

function is called with the array `arr`

and its size `n`

as arguments to sort the array.
- Finally, the sorted array is printed using a loop `for (int i = 0; i < n; i++)`

and the `cout`

statement.

This program uses the bubble sort algorithm to sort an array in ascending order. The bubble sort algorithm works by repeatedly swapping adjacent elements if they are in the wrong order until the array is fully sorted. The outer loop controls the number of passes needed to sort the array, and the inner loop compares adjacent elements and swaps them if necessary. The process is repeated until the array is sorted.