#include <stdio.h>
#include <stdlib.h>
void cocktail_sort(int *data, int n)
{
int i, tmp, k, left, right = n-1, shift = -1;
while((left=shift) < right)
{
for(i = left+1; i < right; i++)
{
if (data[i] > data[i+1])
{
tmp = data[(shift=i)];
data[i] = data[i+1];
data[i+1] = tmp;
for(k = 0; k < n; k++)
if (k == i || k == i+1) printf("[%2d] ", data[k]); else printf(" %2d ", data[k]);
printf("\n");
}
else { for(k = 0; k < n; k++) printf(" %2d ", data[k]); printf("\n"); }
}
printf("\n");
for(i = (right=shift)-1; i > left; i--)
{
if (data[i] > data[i+1])
{
tmp = data[(shift=i)];
data[i] = data[i+1];
data[i+1] = tmp;
for(k = 0; k < n; k++)
if (k == i || k == i+1) printf("[%2d] ", data[k]); else printf(" %2d ", data[k]);
printf("\n");
}
else { for(k = 0; k < n; k++) printf(" %2d ", data[k]); printf("\n"); }
}
printf("\n");
}
}
int main()
{
int i, n = 6;
int data[6] = { 40, 25, 13, 17, 75, 7 };
printf("========== 정렬 전 ==========\n");
for(i = 0; i < n; i++) printf(" %2d ", data[i]); printf("\n");
printf("\n========== 정렬 ==========\n");
cocktail_sort(data, n);
printf("========== 정렬 후 ==========\n");
for(i = 0; i < n; i++) printf(" %2d ", data[i]); printf("\n");
system("pause");
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCgoKdm9pZCBjb2NrdGFpbF9zb3J0KGludCAqZGF0YSwgaW50IG4pCnsKICBpbnQgaSwgdG1wLCBrLCBsZWZ0LCByaWdodCA9IG4tMSwgc2hpZnQgPSAtMTsKCgoKICB3aGlsZSgobGVmdD1zaGlmdCkgPCByaWdodCkKICB7CiAgICBmb3IoaSA9IGxlZnQrMTsgaSA8IHJpZ2h0OyBpKyspCiAgICB7CiAgICAgIGlmIChkYXRhW2ldID4gZGF0YVtpKzFdKQogICAgICB7CiAgICAgICAgdG1wID0gZGF0YVsoc2hpZnQ9aSldOwogICAgICAgIGRhdGFbaV0gPSBkYXRhW2krMV07CiAgICAgICAgZGF0YVtpKzFdID0gdG1wOwogICAgICAgCiAgICAgICAgZm9yKGsgPSAwOyBrIDwgbjsgaysrKQogICAgICAgICAgaWYgKGsgPT0gaSB8fCBrID09IGkrMSkgcHJpbnRmKCJbJTJkXSAiLCBkYXRhW2tdKTsgZWxzZSBwcmludGYoIiAlMmQgICIsIGRhdGFba10pOwogICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgfQogICAgICBlbHNlIHsgZm9yKGsgPSAwOyBrIDwgbjsgaysrKSBwcmludGYoIiAlMmQgICIsIGRhdGFba10pOyBwcmludGYoIlxuIik7IH0KICAgIH0KICAgIHByaW50ZigiXG4iKTsKICAgICAgCiAgICBmb3IoaSA9IChyaWdodD1zaGlmdCktMTsgaSA+IGxlZnQ7IGktLSkKICAgIHsKICAgICAgaWYgKGRhdGFbaV0gPiBkYXRhW2krMV0pCiAgICAgIHsKICAgICAgICB0bXAgPSBkYXRhWyhzaGlmdD1pKV07CiAgICAgICAgZGF0YVtpXSA9IGRhdGFbaSsxXTsKICAgICAgICBkYXRhW2krMV0gPSB0bXA7CiAgICAgICAKICAgICAgICBmb3IoayA9IDA7IGsgPCBuOyBrKyspCiAgICAgICAgICBpZiAoayA9PSBpIHx8IGsgPT0gaSsxKSBwcmludGYoIlslMmRdICIsIGRhdGFba10pOyBlbHNlIHByaW50ZigiICUyZCAgIiwgZGF0YVtrXSk7CiAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgICB9CiAgICAgIGVsc2UgeyBmb3IoayA9IDA7IGsgPCBuOyBrKyspIHByaW50ZigiICUyZCAgIiwgZGF0YVtrXSk7IHByaW50ZigiXG4iKTsgfQogICAgfQogICAgcHJpbnRmKCJcbiIpOwogIH0KfQoKCgppbnQgbWFpbigpCnsKICBpbnQgaSwgbiA9IDY7CiAgaW50IGRhdGFbNl0gPSB7IDQwLCAyNSwgMTMsIDE3LCA3NSwgNyB9OwogCiAgcHJpbnRmKCI9PT09PT09PT09IOygleugrCDsoIQgPT09PT09PT09PVxuIik7CiAgZm9yKGkgPSAwOyBpIDwgbjsgaSsrKSBwcmludGYoIiAlMmQgICIsIGRhdGFbaV0pOyBwcmludGYoIlxuIik7CgoKCiAgcHJpbnRmKCJcbj09PT09PT09PT0g7KCV66CsID09PT09PT09PT1cbiIpOwogIGNvY2t0YWlsX3NvcnQoZGF0YSwgbik7CgoKCiAgcHJpbnRmKCI9PT09PT09PT09IOygleugrCDtm4QgPT09PT09PT09PVxuIik7CiAgZm9yKGkgPSAwOyBpIDwgbjsgaSsrKSBwcmludGYoIiAlMmQgICIsIGRhdGFbaV0pOyBwcmludGYoIlxuIik7CgogIHN5c3RlbSgicGF1c2UiKTsKICByZXR1cm4gMDsKfQ==