Both functions can be sorted, the time complexity in all sorts is also the lowest, the only difference is that Stable_sort is a stable sort, in the same time two elements do not exchange position, and sort is not, this difference in a single ordinary array sorting does not affect, However, if the structure is sorted, there will be a noticeable difference. Refer to POJ 1007 Click to open the topic link this problem is to use the normal sort will change the relative position of the structure, put on
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int len;struct Node{char s[55];int score;} P[110];bool CMP (const node A, const node B) {return A.score < B.score;} int sum (char *p) {int len, sum;sum = 0;len = strlen (P), for (int i = 0; i < len; ++i) {for (int j = i + 1; j < Len; ++j) { if (P[i] > P[j]) sum++;}} return sum;} int main () {int Num;char s[110];while (scanf ("%d%d", &len, &num)! = EOF) {for (int i = 0; i < num; ++i) {scanf ("%s", P[I].S);p [i].score = SUM (P[I].S);} Stable_sort (p, p + num, CMP); Stable sort, equivalent does not exchange for (int i = 0; i < num; ++i) printf ("%s\n", P[I].S);} return 0;}
STL's Stable_sort and sort