#include <stdlib.h>
#include <iostream>
void swap(int* bigger, int* smaller)
{
*bigger += *smaller;
*smaller = *bigger - *smaller;
*bigger = *bigger - *smaller;
}
int* bubbleSort(int *data, int size)
{
bool swapped = true;
int *sortedArray = (int*)malloc(size*sizeof(int));
int* s = sortedArray;
for (int i = 0; i < size;i++){
*s++ = *data++;
}
while (swapped)
{
swapped = false;
for (int i = 1; i <= size - 1; i++)
{
if (sortedArray[i - 1] > sortedArray[i])
{
swap(&sortedArray[i - 1], &sortedArray[i]);
swapped = true;
}
}
size--;
}
return sortedArray;
}
int main()
{
int data[] = {9,2,8,3,7,4,6,5,1,0};
int* sorted = bubbleSort(data, 10);
for(int i = 0; i < 10; ++i)
{
std::cout << data[i] << " " << sorted[i] << "\n";
}
return 0;
}
I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8aW9zdHJlYW0+Cgp2b2lkIHN3YXAoaW50KiBiaWdnZXIsIGludCogc21hbGxlcikKewogICAgKmJpZ2dlciArPSAqc21hbGxlcjsKICAgICpzbWFsbGVyID0gKmJpZ2dlciAtICpzbWFsbGVyOwogICAgKmJpZ2dlciA9ICpiaWdnZXIgLSAqc21hbGxlcjsKfQoKaW50KiBidWJibGVTb3J0KGludCAqZGF0YSwgaW50IHNpemUpCnsKICAgIGJvb2wgc3dhcHBlZCA9IHRydWU7CiAgICBpbnQgKnNvcnRlZEFycmF5ID0gKGludCopbWFsbG9jKHNpemUqc2l6ZW9mKGludCkpOwogICAgaW50KiBzID0gc29ydGVkQXJyYXk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemU7aSsrKXsKICAgICAgICAqcysrID0gKmRhdGErKzsKICAgIH0KCiAgICB3aGlsZSAoc3dhcHBlZCkKICAgIHsKICAgICAgICBzd2FwcGVkID0gZmFsc2U7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gc2l6ZSAtIDE7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChzb3J0ZWRBcnJheVtpIC0gMV0gPiBzb3J0ZWRBcnJheVtpXSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3dhcCgmc29ydGVkQXJyYXlbaSAtIDFdLCAmc29ydGVkQXJyYXlbaV0pOwogICAgICAgICAgICAgICAgc3dhcHBlZCA9IHRydWU7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgc2l6ZS0tOwogICAgfQogICAgcmV0dXJuIHNvcnRlZEFycmF5Owp9CgppbnQgbWFpbigpCnsKCWludCBkYXRhW10gPSB7OSwyLDgsMyw3LDQsNiw1LDEsMH07CglpbnQqIHNvcnRlZCA9IGJ1YmJsZVNvcnQoZGF0YSwgMTApOwoJCglmb3IoaW50IGkgPSAwOyBpIDwgMTA7ICsraSkKCXsKCQlzdGQ6OmNvdXQgPDwgZGF0YVtpXSA8PCAiICIgPDwgc29ydGVkW2ldIDw8ICJcbiI7Cgl9CglyZXR1cm4gMDsKfQ==