Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- void swap(int &a, int &b) {
- int k = a;
- a = b;
- b = k;
- }
- // СОРТИРОВКА МЕТОДОМ ПУЗЫРЬКА
- void bubble(int *& a) {
- bool f = true;
- for (int i = N; i > 0 && f; i--) {
- f = false;
- for (int j = 0; j < i - 1; j++)
- if (a[j] > a[j + 1]) {
- swap(a[j], a[j+1]);
- f = true;
- }
- }
- }
- int main() {
- setlocale(LC_ALL, "rus");
- int N;
- cout << "N = ";
- cin >> N;
- int * a = new int[N];
- srand(time(0));
- cout << "Изначальный массив ";
- for (int i = 0; i < N; i++) {
- a[i] = rand() % 21 - 10;
- cout << a[i] << " ";
- }
- bubble(a);
- cout << endl;
- cout << "Отсортированный массив ";
- for (int i = 0; i < N; i++)
- cout << a[i] << " ";
- delete[] a;
- return 0;
- }
Add Comment
Please, Sign In to add comment