以前写过时间复杂度O(n^2)的,怎么能更快一些呢?可以换个思路,哪些排序算法时间复杂度快过O(n^2)呢?快排,归并,就是这样了,关键是能立马反应过来。
// sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;
stack<int> revertStack(stack<int> &s) {
stack<int> rs;
while (!s.empty()) {
rs.push(s.top());
s.pop();
}
return rs;
}
stack<int> stackSort(stack<int> &s) {
if (s.size() <= 1) {
return s;
}
stack<int> greater;
stack<int> less;
int cur;
int pivot = s.top();
s.pop();
while (!s.empty()) {
cur = s.top();
s.pop();
if (cur > pivot) {
greater.push(cur);
}
else {
less.push(cur);
}
}
greater = stackSort(greater);
// Need revert the