#include<iostream>
#include<algorithm>
class LinkedList {
public:
struct LinkedNode {
int val;
LinkedNode* prev;
LinkedNode* next;
LinkedNode(int x) :val(x), prev(NULL), next(NULL) {};
};
LinkedList() {
dummyHead = new LinkedNode(0);
len = 0;
}
void initList(int n) {
int x;
len = n;
LinkedNode* cur = dummyHead;
while (n--) {
std::cin >> x;
LinkedNode* newNode = new LinkedNode(x);
cur->next = newNode;
newNode->prev = cur;
cur = cur->next;
}
}
void sortList() {
for (int i = 0; i < len - 1; i++) {
LinkedNode* cur = dummyHead->next->next;
LinkedNode* last = dummyHead->next;
for (int j = 0; j < len - 1 - i; j++) {
if (cur->val < last->val) {
std::swap(cur->val, last->val);
}
cur = cur->next;
last = last->next;
}
}
}
void printList() {
LinkedNode* cur = dummyHead->next;
while (cur != NULL) {
std::cout << cur->val << " ";
cur = cur->next;
}
}
private:
LinkedNode* dummyHead;
int len;
};
int main() {
LinkedList list;
int n;
std::cin >> n;
list.initList(n);
list.sortList();
list.printList();
return 0;
}
【STL】
#include<iostream>
#include<vector>
#include<algorithm>
int main() {
int n;
std::cin >> n;
std::vector<int> vec(n);
for (int i = 0; i < n; i++) std::cin >> vec[i];
std::sort(vec.begin(), vec.end(), [](int a, int b) {
return a < b;
});
for (int a : vec) std::cout << a << " ";
return 0;
}