#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct ListNode { ElemType val; struct ListNode* next; } ListNode, *PListNode, *List; // function declaration void printList(List L); // 简单选择排序算法。复杂的选择排序又称堆排序!!! List simple_selection_sort_algorithm(List L, const int size); int main(const int argc, const char* argv[]) { int n, val; fscanf(stdin, "%d", &n); PListNode head = NULL, tail = NULL, p; while (fscanf(stdin, "%d", &val) != EOF) { p = (PListNode) malloc(sizeof(ListNode)); p->val = val; p->next = NULL; if (!tail) head = tail = p; else tail = tail->next = p; } head = simple_selection_sort_algorithm(head, n); return printList(head), 0; } void printList(List L) { PListNode p = L; while (p) { fprintf(stdout, "%d", p->val); if (p->next) fputc(32, stdout); p = p->next; } } List simple_selection_sort_algorithm(List L, const int size) { // corner case if (size < 2) return L; PListNode nodes[size], p = L; int i, t; for (i = 0; i < size; ++i, p = p->next) *(nodes + i) = p; int min; PListNode tmp; for (t = 0; t < size - 1; ++t) { min = t; for (i = t + 1; i < size; ++i) if ((*(*(nodes + i))).val < (*(*(nodes + min))).val) min = i; tmp = *(nodes + t); *(nodes + t) = *(nodes + min); *(nodes + min) = tmp; } for (i = 0; i < size - 1; ++i) (*(nodes + i))->next = *(nodes + i + 1); (*(nodes + size - 1))->next = NULL; return *nodes; }