Добавил программы по тестированию
This commit is contained in:
65
programs/1_bubble_sort.cpp
Normal file
65
programs/1_bubble_sort.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void swap(int &a, int &b)
|
||||
{
|
||||
int tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
void bubbleSort(int arr[], int n)
|
||||
{
|
||||
int nPairs = n;
|
||||
A:
|
||||
nPairs = nPairs - 1;
|
||||
bool hasSwapped = false;
|
||||
int i = 0;
|
||||
while (i < nPairs)
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
swap(arr[i], arr[i + 1]);
|
||||
hasSwapped = true;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (hasSwapped)
|
||||
{
|
||||
goto A;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
if (n < 1 || n > 10000) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int *arr = new int[n];
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int tmp;
|
||||
cin >> tmp;
|
||||
if (tmp > 10000 || tmp < -10000) return 1;
|
||||
arr[i] = tmp;
|
||||
}
|
||||
|
||||
bubbleSort(arr, n);
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << arr[i];
|
||||
if (i < n - 1)
|
||||
cout << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
delete[] arr;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user