Code:
void ShellSort(int a[],int n, int h[], int k) { int step,i,j, x,len; for (step = 0 ; step <k; step++) { len = h[step]; for (i = len; i<n; i++) { x = a[i]; j = i-len; // a[j] đứng kề trước a[i] trong cùng dãy con while ((x<a[ j])&&(j>=0)// sắp xếp dãy con chứa x { // bằng phương pháp chèn trực tiếp a[ j+len] = a[ j]; j = j - len; } a[j+len] = x; } } }
em cải tiến 1 chút , mục đích là để cho nó chạy thôi, thuật toán thì hiểu rầu
![Big Grin](https://forum.uit.edu.vn/core/images/smilies/biggrin.png)
em bỏ cái < int h[], int k > thay vào đó là 1 vòng for(len=7;0<len;len-2)
nhưng khi chạy ct thì nó ko thực hiện hàm ShellSort lun
![Big Grin](https://forum.uit.edu.vn/core/images/smilies/biggrin.png)
các huynh coi dum e vs
![Big Grin](https://forum.uit.edu.vn/core/images/smilies/biggrin.png)
Code:
#include "stdafx.h" #include "conio.h" #include "stdio.h" void nhap(int a[],int n); void xuat(int a[],int n); void ShellSort(int a[],int n); void main() { int a[10]; int n; printf("Nhap n:"); scanf("%d",&n); nhap(a,n); xuat(a,n); printf("\nMang dc sap xep:\n"); ShellSort(a,n); // ko chạy dc =.=' xuat(a,n); getch(); } void nhap(int a[],int n) { for(int i=0;i<n;i++) { printf("Nhap a[%d]: ",i); scanf("%d",&a[i]); } } void xuat(int a[],int n) { for(int i=0;i<n;i++) printf(" %d",a[i]); } void ShellSort(int a[],int n) { int i,j,len,x; for(len=7;0<len;len-2) { for(i=len;i<n;i++) { x=a[i]; j=i-len; while((x<a[j])&&(j>=0)) { a[j+len]=a[j]; j=j-len; } a[j+len]=x; } } }
Comment