#include <stdio.h>
#include <stdlib.h>
int* bubbleSort(const int* arr, int size) {
int* sortedArr
= (int*)malloc(size
* sizeof(int)); if (sortedArr == NULL) {
// Ошибка при выделении памяти
return NULL;
}
for (int i = 0; i < size; i++) {
sortedArr[i] = arr[i];
}
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (sortedArr[j] > sortedArr[j + 1]) {
int temp = sortedArr[j];
sortedArr[j] = sortedArr[j + 1];
sortedArr[j + 1] = temp;
}
}
}
return sortedArr;
}
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
int* sortedArr = bubbleSort(arr, size);
if (sortedArr == NULL) {
printf("Ошибка выделения памяти.\n"); return 1;
}
printf("Отсортированный массив: "); for (int i = 0; i < size; i++) {
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCogYnViYmxlU29ydChjb25zdCBpbnQqIGFyciwgaW50IHNpemUpIHsKICAgIGludCogc29ydGVkQXJyID0gKGludCopbWFsbG9jKHNpemUgKiBzaXplb2YoaW50KSk7CiAgICBpZiAoc29ydGVkQXJyID09IE5VTEwpIHsKICAgICAgICAvLyDQntGI0LjQsdC60LAg0L/RgNC4INCy0YvQtNC10LvQtdC90LjQuCDQv9Cw0LzRj9GC0LgKICAgICAgICByZXR1cm4gTlVMTDsKICAgIH0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgc29ydGVkQXJyW2ldID0gYXJyW2ldOwogICAgfQogICAgCiAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemUgLSAxOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHNpemUgLSBpIC0gMTsgaisrKSB7CiAgICAgICAgICAgIGlmIChzb3J0ZWRBcnJbal0gPiBzb3J0ZWRBcnJbaiArIDFdKSB7CiAgICAgICAgICAgICAgICBpbnQgdGVtcCA9IHNvcnRlZEFycltqXTsKICAgICAgICAgICAgICAgIHNvcnRlZEFycltqXSA9IHNvcnRlZEFycltqICsgMV07CiAgICAgICAgICAgICAgICBzb3J0ZWRBcnJbaiArIDFdID0gdGVtcDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gc29ydGVkQXJyOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBhcnJbXSA9IHs1LCAyLCA5LCAxLCA1LCA2fTsKICAgIGludCBzaXplID0gc2l6ZW9mKGFycikgLyBzaXplb2YoYXJyWzBdKTsKCiAgICBpbnQqIHNvcnRlZEFyciA9IGJ1YmJsZVNvcnQoYXJyLCBzaXplKTsKICAgIGlmIChzb3J0ZWRBcnIgPT0gTlVMTCkgewogICAgICAgIHByaW50Zigi0J7RiNC40LHQutCwINCy0YvQtNC10LvQtdC90LjRjyDQv9Cw0LzRj9GC0LguXG4iKTsKICAgICAgICByZXR1cm4gMTsKICAgIH0KCiAgICBwcmludGYoItCe0YLRgdC+0YDRgtC40YDQvtCy0LDQvdC90YvQuSDQvNCw0YHRgdC40LI6ICIpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBwcmludGYoIiVkICIsIHNvcnRlZEFycltpXSk7CiAgICB9CiAgICBwcmludGYoIlxuIik7CgogICAgZnJlZShzb3J0ZWRBcnIpOwogICAgcmV0dXJuIDA7Cn0=