STL's Stable_sort and sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.