Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 11, 2018 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/ca7b8f5ffa02c7539f15576d3944b803 to your computer and use it in GitHub Desktop.
Save completejavascript/ca7b8f5ffa02c7539f15576d3944b803 to your computer and use it in GitHub Desktop.
void ShakerSort(int *a, int N)
{
// Thuật toán cải tiến thuật toán sắp xếp nổi bọt
// Bình thường khi ta sắp xếp nổi bọt, giả sử tăng dần
// Thì phần tử nhỏ nhất sẽ được dồn về trái, dần dần cho đến hết.
// Còn với thuật toán này ta sẽ dồn phần tử nhỏ nhất về trái, phần tử lớn lớn sang phải đồng thời
int left, right, k;
left = 0;
right = N-1;
k = N-1;
while(left < right)
{
for(int j = right; j > left; j--)
{
if(a[j] < a[j-1])
{
swap(a[j], a[j-1]);
k = j;
}
}
left = k;
for (int j = left; j < right; j++)
{
if (a[j] > a[j+1])
{
swap(a[j], a[j+1]);
k = j;
}
}
right = k;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment