1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//Minh hoa InsertSort chen truc tiep //Minh hoa thuat: InsertSort (Sap xep bang chen truc tiep) int Mang[20]={10,9,8,7,6,5,4,3,2,1}; void InsertSort(int mang[],int n) { int pos,i,x,vt; for(i=1;i<n;i++) { //gan gia tri phan tu thu i se duoc chen vao vi tri pos x=mang[i]; pos=i; //tim vi tri chen x while(pos>0&&x<mang[pos-1]) pos=pos-1; for(vt=i;vt>=pos;vt--) mang[vt]=mang[vt-1]; // chen x vao vi tri pos mang[pos]=x; } } void inday(int mang[],int n) { int j; for(j=0;j<n;j++) printf("%d ",mang[j]); } void main() { clrscr(); printf("Day truoc khi sap xep\n"); inday(Mang,10); printf("\nDay sau khi sap xep\n"); InsertSort(Mang,10); inday(Mang,10); getch(); } |
nosomovo
Xem thêm Code C cài đặt thuật toán sắp xếp bằng cách chèn trực tiếp – Insert sort