//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