ai đó xem giúp mình code về giải thuật với sau nó chỉ ra đúng selection sort còn Insertion Sort thì ra sai dù mình chạy thử 2 file riêng thì ra đúng nhưng rap lại thì chỉ nhận 1 giải thuật thôi
Bn nào coi và giúp mình sửa lại (nếu bít) dùm mình ạ.
Xin cảm ơn rất nhiều ạ!!!
Code:
#include<stdio.h>
int main(){
int i, j, count, temp, number[50], min;
int dem1,temp1 ,k,h;
int dem;
printf(" Vui long nhap so phan tu: ");
scanf("%d",&count);
printf("Nhap %d phan tu: ", count);
// vong lap de lay dc so phan tu trong mang
for(i=0;i<count;i++)
scanf("%d",&number[i]);
//printf("Mang ban dau:%d\n",number[i]);
// selection sort
printf("\nSelection Sort:");
{ for(i=0;i<count;i++){
{
min=i;
temp=number[i];
j= i;
for(j=i+1;j<count;j++){
if(number[j]<number[min]){
min=j;
}
}
if(min != number[i]){
printf("\n ==>Trao doi phan tu: [%d,%d ]", number[i],number[min]);
temp=number[min];
number[min]=number[i];
number[i]=temp;
dem++;
}
printf("\n Vong lap thu %d" ,(i+1));
printf("\n [");
// Duyet qua tat ca phan tu
for (j = 0; j < count; j++) {
printf(" %d",number[j]);
}
printf("]\n");
}
}
printf("\nMang sau khi sap xep:");
for(j=0;j<count;j++){
printf(" %d",number[j]);
}
printf("\nSo lan hoan doi vi tri:%d",dem);
}
printf("\n Insertion Sort:");
for(i=1;i<count;i++){
// chon mot gia tri de chen
temp=number[i];
j= i;
// kiem tra xem so lien truoc co lon hon gia tri duoc chen khong
while(j > 0 && number[j-1] > temp){
number[j]=number[j-1];
j--;
printf("\n Di chuyen phan tu:%d",number[j]);
}
if(number[j] !=number[i]){
printf("\nChen phan tu:%d", temp );
number[j]=temp;
dem1++;
}
printf("\n Vong lap thu %d" ,(i));
printf("\n [");
// Duyet qua tat ca phan tu
for (j = 0; j < count; j++) {
printf(" %d",number[j]);
}
printf("]\n");
}
//Displaying the sorted array
printf("\n mang sau khi sap xep: ");
for(i=0;i<count;i++){
printf(" %d",number[i]);
}
printf("\nSo lan hoan doi vi tri:%d",dem1);
return 0;
}Giaithuat.c