Write a C++ program that reads 10 integer numbers and stores them in an array Numbers, sorts the numbers using the bubble sort algorithm, and displays the array elements whenever there is an element swap. This C++ program must have three functions: 1. reading the array's ten elements, 2. displaying the array's elements, 3. The array elements are being sorted.
#include <iostream>
using namespace std;
int* ReadArray()
{
int* arr=new int[10];
cout << "Please, enter 10 elements of array: ";
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
return arr;
}
void SwapInt(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void Display(int* arr)
{
for (int k = 0; k < 10; k++)
{
cout << arr[k] << " ";
}
cout << endl;
}
void BubbleSort(int* arr)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10 - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
SwapInt(&arr[j], &arr[j + 1]);
}
}
Display(arr);
}
}
int main()
{
int* arr = ReadArray();
BubbleSort(arr);
}
Comments
Leave a comment