#include <iostream> #include <vector> using namespace std; struct Node { int val; Node* next = nullptr; Node(int val) : val(val) {} }; void SelectSort(Node* head) { for (Node *inode = head; inode->next; inode = inode->next) { Node *p_pre_min_node = inode, *jnode = inode; while (jnode->next) { if (jnode->next->val < p_pre_min_node->next->val) p_pre_min_node = jnode; jnode = jnode->next; } jnode = p_pre_min_node->next; p_pre_min_node->next = p_pre_min_node->next->next; jnode->next = inode->next; inode->next = jnode; } } void PrintLL(Node *head) { for (Node *node = head->next; node; node = node->next) { cout << node->val << ' '; } cout << endl; } int main() { int n; while (cin >> n) { Node head(0); Node* pnode = &head; int val; for (int i = 0; i < n; ++i) { cin >> val; pnode->next = new Node(val); pnode = pnode->next; } SelectSort(&head); PrintLL(&head); } } // 64 位输出请用 printf("%lld")
基于单链表的选择排序