Create an array of size 10 fill the array from the user. Then ask the user for the order of sorting ascending or descending. And sort the array using bubble sorting algorithm.
#include <iostream>
using namespace std;
void SwapInt(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void AscSorting(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]);
}
}
}
}
void DescSorting(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]);
}
}
}
}
int main()
{
int arr[10];
cout << "Please, enter an elements of array: " << endl;
for (int i = 0; i < 10; i++)
{
cout << "Enter a value of array[" << i << "]: ";
cin >> arr[i];
}
char c;
cout << "Please, enter the order of sorting:\n"
<< "A - ascending\n"
<< "D - Descending\n"
<< "Your select: ";
cin >> c;
if (c == 'A')
{
AscSorting(arr);
}
else if(c == 'D')
{
DescSorting(arr);
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
}
Comments
Leave a comment