输入样例:
5
4 5 3 2 9
6
1 3 5 7 9 2
0
输出样例:
2 3 4 5 9
1 2 3 5 7 9
简单选择排序
#include
#include
typedef struct LNode {
int data;
struct LNode *next;
} LNode, *LinkList;
void list_init(LinkList *L);
void list_insert(LinkList L, int e);
void list_destroy(LinkList *L);
void list_show(LinkList L);
void list_selection_sort(LinkList L);
void listSelectionSort(LinkList L);
int main() {
int m, x;
while (scanf("%d", &m) && m != 0) {
LinkList L = NULL;
list_init(&L);
while (m--) {
scanf("%d", &x);
list_insert(L, x);
}
listSelectionSort(L);
list_show(L);
list_destroy(&L);
}
return 0;
}
void list_init(LinkList *L) {
*L = (LNode *) malloc(sizeof(LNode));
(*L)->data = 0;
(*L)->next = NULL;
}
void list_insert(LinkList L, int e) {
LNode *new_node = (LNode *) malloc(sizeof(LNode));
LNode *tail = L;
while (tail->next) {
tail = tail->next;
}
new_node->data = e;
new_node->next = NULL;
tail->next = new_node;
}
void list_destroy(LinkList *L) {
LNode *head = *L, *tmp;
*L = NULL;
while (head->next) {
tmp = head->next;
head->next = tmp->next;
free(tmp);
}
free(head);
}
void list_show(LinkList L) {
LNode *p = L->next;
while (p) {
printf("%c%d", (p == L->next) ? 10 : 32, p->data);
p = p->next;
}
}
void list_selection_sort(LinkList L) {
LNode *p = L, *q, *tmp;
LNode *min_prior_position;
while (p->next) {
min_prior_position = p;
q = p->next;
while (q->next) {
if (q->next->data < min_prior_position->next->data) {
min_prior_position = q;
}
q = q->next;
}
if (min_prior_position != p) {
if (p->next == min_prior_position) {
p->next = min_prior_position->next;
min_prior_position->next = min_prior_position->next->next;
p->next->next = min_prior_position;
} else {
tmp = p->next->next;
p->next->next = min_prior_position->next->next;
min_prior_position->next->next = tmp;
tmp = p->next;
p->next = min_prior_position->next;
min_prior_position->next = tmp;
}
}
p = p->next;
}
}
void listSelectionSort(LinkList L) {
LNode *fakeHead = (LNode *) malloc(sizeof(LNode)), *tail = L, *minPos, *cursor, *tmp;
fakeHead->next = L->next;
L->next = NULL;
while (fakeHead->next) {
minPos = fakeHead;
for (cursor = fakeHead; cursor->next; cursor = cursor->next) {
if (cursor->next->data < minPos->next->data) {
minPos = cursor;
}
}
tail->next = minPos->next;
minPos->next = minPos->next->next;
tail = tail->next;
tail->next = NULL;
}
free(fakeHead);
}
冒泡排序
#include
#include
typedef struct LNode {
int data;
struct LNode *next;
} LNode, *LinkList;
void list_init(LinkList *L);
void list_insert(LinkList L, int e);
void list_destroy(LinkList *L);
void list_show(LinkList L);
int list_count(LinkList L);
void list_bubble_sort(LinkList L);
int main() {
int m, x;
while (scanf("%d", &m) && m != 0) {
LinkList L = NULL;
list_init(&L);
while (m--) {
scanf("%d", &x);
list_insert(L, x);
}
list_bubble_sort(L);
list_show(L);
list_destroy(&L);
}
return 0;
}
void list_init(LinkList *L) {
*L = (LNode *) malloc(sizeof(LNode));
(*L)->data = 0;
(*L)->next = NULL;
}
void list_insert(LinkList L, int e) {
LNode *new_node = (LNode *) malloc(sizeof(LNode));
LNode *tail = L;
while (tail->next) {
tail = tail->next;
}
new_node->data = e;
new_node->next = NULL;
tail->next = new_node;
}
void list_destroy(LinkList *L) {
LNode *head = *L, *tmp;
*L = NULL;
while (head->next) {
tmp = head->next;
head->next = tmp->next;
free(tmp);
}
free(head);
}
void list_show(LinkList L) {
LNode *p = L->next;
while (p) {
printf("%c%d", (p == L->next) ? 10 : 32, p->data);
p = p->next;
}
}
int list_count(LinkList L) {
LNode *p = L->next;
int len = 0;
while (p) {
p = p->next;
len++;
}
return len;
}
void list_bubble_sort(LinkList L) {
LNode *cursor, *tmp;
int len = list_count(L);
for (int i = 0; i < len; i++) {
cursor = L;
for (int j = 0; j < len - i - 1; j++) {
if (cursor->next->data > cursor->next->next->data) {
tmp = cursor->next;
cursor->next = tmp->next;
tmp->next = tmp->next->next;
cursor->next->next = tmp;
}
cursor = cursor->next;
}
}
}
直接插入排序
#include
#include
typedef struct LNode {
int data;
struct LNode *next;
} LNode, *LinkList;
void list_init(LinkList *L);
void list_insert(LinkList L, int e);
void list_destroy(LinkList *L);
void list_show(LinkList L);
void list_insertion_sort(LinkList L);
int main() {
int m, x;
while (scanf("%d", &m) && m != 0) {
LinkList L = NULL;
list_init(&L);
while (m--) {
scanf("%d", &x);
list_insert(L, x);
}
list_insertion_sort(L);
list_show(L);
list_destroy(&L);
}
return 0;
}
void list_init(LinkList *L) {
*L = (LNode *) malloc(sizeof(LNode));
(*L)->data = 0;
(*L)->next = NULL;
}
void list_insert(LinkList L, int e) {
LNode *new_node = (LNode *) malloc(sizeof(LNode));
LNode *tail = L;
while (tail->next) {
tail = tail->next;
}
new_node->data = e;
new_node->next = NULL;
tail->next = new_node;
}
void list_destroy(LinkList *L) {
LNode *head = *L, *tmp;
*L = NULL;
while (head->next) {
tmp = head->next;
head->next = tmp->next;
free(tmp);
}
free(head);
}
void list_show(LinkList L) {
LNode *p = L->next;
while (p) {
printf("%c%d", (p == L->next) ? 10 : 32, p->data);
p = p->next;
}
}
void list_insertion_sort(LinkList L) {
if (!L->next) return;
LNode *left, *right = L->next->next, *tmp;
L->next->next = NULL;
while (right) {
left = L;
while (left->next && left->next->data < right->data) {
left = left->next;
}
tmp = right->next;
right->next = left->next;
left->next = right;
right = tmp;
}
}