Sắp xếp chèn
•Sắp xếp chèn
•Cài đặt bằng mảng
•Cài đặt bằng danh sách moc nối
•Phân tích
•Bài tập
void insert(int A[], int & end, int value) {
if (end == ‐1) {
end++;
A[end] = value;
} else {
int pos = 0, i;
while (A[pos] < value) pos++;
for (i = end; i >= pos; i‐‐) A[i + 1] = A[i];
A[pos] = value;
end = end + 1;
}
}
void insertionSort(const int B[], int n, int A[]) {
int end = ‐1;
for (int i = 0; i < n; i++)
insert(A, end, B[i]);
}
typedef struct node {
long masoSV;
char hoten[30];
char diachi[50];
float diemTB;
struct node * pNext;
}NODE;
NODE *list=NULL;
int insert(NODE * & pHead, long msSV, char ht[], char dc[], float diem) {
NODE * ptr = (NODE * ) malloc(sizeof(NODE));
ptr‐ > masoSV = msSV;
strcpy(ptr‐ > hoten, ht);
strcpy(ptr‐ > diachi, dc);
ptr‐ > diemTB = diem;
if (pHead == NULL) {
ptr‐ > pNext = NULL;
pHead = ptr;
} else {
if (pHead‐ > masoSV >= msSV) {
ptr‐ > pNext = pHead;
pHead = ptr;
} else {
NODE * preQ = pHead;
NODE * q = pHead‐ > pNext;
while (q != NULL && q‐ > masoSV < msSV) {
preQ = q;
q = q‐ > pNext;
}
ptr‐ > pNext = q;
preQ‐ > pNext = ptr;
}
}
}