Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 11, 2018 19:15
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/95d916784fb804bf5af3e21a827f2f3b to your computer and use it in GitHub Desktop.
Save completejavascript/95d916784fb804bf5af3e21a827f2f3b to your computer and use it in GitHub Desktop.
void InsertionSort(int *a, int N)
{
int pos, x;
for(int i = 1; i < N; i++)
{
pos = i - 1;
x = a[i];
// giả sử dãy a[0], a[1], ... , a[i] đã sắp xếp
// bắt đầu từ a[i], duyệt về đầu mảng và tìm vị trí thích hợp cho a[i]
while(pos >= 0 && a[pos] > x)
{
a[pos + 1] = a[pos];
pos--;
}
a[pos+1] = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment