排序

冒泡排序

/*
冒泡 升序 
每次把最大的放到数组末 
*/ 
#include <stdio.h>
void BubbleSort(int *pa , int nNum)
{
	for(int i = nNum - 1; i > 0; i--)
	{
		for(int j = 0; j < i; j++)
		{
			if(pa[j] > pa[j+1])
			{
				int nTmp = pa[j];
				pa[j] = pa[j+1];
				pa[j+1] = nTmp;
			}
		}
	}
}
#define NUM 5
int main()
{
	int an[NUM] = {5,4,8,2,1};
	BubbleSort(an, NUM);
	for(int i=0; i < NUM; i++)
		printf("%d\n", an[i]);
	return 0;
}

STL排序(sort)


#include <algorithm>

sort(begin,end);



函数名功能描述
sort对给定区间所有元素进行排序
stable_sort对给定区间所有元素进行稳定排序
partial_sort对给定区间所有元素部分排序
partial_sort_copy对给定区间复制并排序
nth_element找出给定区间的某个位置对应的元素
is_sorted判断一个区间是否已经排好序
partition使得符合某个条件的元素放在前面
stable_partition相对稳定的使得符合某个条件的元素放在前面


  • 若需对vector, string, deque, 或 array容器进行全排序,你可选择sort或stable_sort;
  • 若只需对vector, string, deque, 或 array容器中取得top n的元素,部分排序partial_sort是首选.
  • 若对于vector, string, deque, 或array容器,你需要找到第n个位置的元素或者你需要得到top n且不关系top n中的内部顺序,nth_element是最理想的;
  • 若你需要从标准序列容器或者array中把满足某个条件或者不满足某个条件的元素分开,你最好使用partition或stable_partition;
  • 若使用的list容器,你可以直接使用partition和stable_partition算法,你可以使用list::sort代替sort和stable_sort排序。若你需要得到partial_sort或nth_element的排序效果,你必须间接使用。正如上面介绍的有几种方式可以选择。

    参考:

    详细解说 STL 排序(Sort)

    C++ 排序函数 sort(),qsort()的用法



    函数名功能描述
    sort对给定区间所有元素进行排序
    stable_sort对给定区间所有元素进行稳定排序
    partial_sort对给定区间所有元素部分排序
    partial_sort_copy对给定区间复制并排序
    nth_element找出给定区间的某个位置对应的元素
    is_sorted判断一个区间是否已经排好序
    partition使得符合某个条件的元素放在前面
    stable_partition相对稳定的使得符合某个条件的元素放在前面
  • 若需对vector, string, deque, 或 array容器进行全排序,你可选择sort或stable_sort;
  • 若只需对vector, string, deque, 或 array容器中取得top n的元素,部分排序partial_sort是首选.
  • 若对于vector, string, deque, 或array容器,你需要找到第n个位置的元素或者你需要得到top n且不关系top n中的内部顺序,nth_element是最理想的;
  • 若你需要从标准序列容器或者array中把满足某个条件或者不满足某个条件的元素分开,你最好使用partition或stable_partition;
  • 若使用的list容器,你可以直接使用partition和stable_partition算法,你可以使用list::sort代替sort和stable_sort排序。若你需要得到partial_sort或nth_element的排序效果,你必须间接使用。正如上面介绍的有几种方式可以选择。

    #include <algorithm>

    sort(begin,end);



    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <vector>
    #include <string>
    using namespace std;
    
    class student{
            public:
            student(const string &a, int b):name(a), score(b){}
            string name;
            int score;
            bool operator < (const student &m)const {
                    return score< m.score;
            }
    };
    
    int main() {
            vector< student> vect;
            student st1("Tom", 74);
            vect.push_back(st1);
            st1.name="Jimy";
            st1.score=56;
            vect.push_back(st1);
            st1.name="Mary";
            st1.score=92;
            vect.push_back(st1);
            st1.name="Jessy";
            st1.score=85;
            vect.push_back(st1);
            st1.name="Jone";
            st1.score=56;
            vect.push_back(st1);
            st1.name="Bush";
            st1.score=52;
            vect.push_back(st1);
            st1.name="Winter";
            st1.score=77;
            vect.push_back(st1);
            st1.name="Andyer";
            st1.score=63;
            vect.push_back(st1);
            st1.name="Lily";
            st1.score=76;
            vect.push_back(st1);
            st1.name="Maryia";
            st1.score=89;
            vect.push_back(st1);
            cout<<"------before sort..."<<endl;
            for(int i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":\t"<<vect[i].score<<endl;
        
        	sort(vect.begin(), vect.end());
    //	stable_sort(vect.begin(), vect.end(),less<student>());
    //	partial_sort(vect.begin(), vect.begin()+5, vect.end(),less<student>());
    //	nth_element(vect.begin(), vect.begin()+3, vect.end(),less<student>());
    //	student exam("pass", 60);
    //	stable_partition(vect.begin(), vect.end(), bind2nd(less<student>(), exam));
    
            cout <<"-----after sort ...."<<endl;
            for(int i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":\t"<<vect[i].score<<endl;
            return 0 ;
    }


    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <vector>
    using namespace std;
    
    
    class myclass {
            public:
            myclass(int a, int b):first(a), second(b){}
            int first;
            int second;
            bool operator < (const myclass &m)const {
                    return first < m.first;
            }
    };
    
    
    bool less_second(const myclass & m1, const myclass & m2) {
            return m1.second < m2.second;
    }
    
    
    int main() {
            
            vector< myclass > vect;
            for(int i = 0 ; i < 10 ; i ++){
                    myclass my(10-i, i*3);
                    vect.push_back(my);
            }
            for(int i = 0 ; i < vect.size(); i ++) 
            cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n";
            sort(vect.begin(), vect.end());
            cout<<"after sorted by first:"<<endl;
            for(int i = 0 ; i < vect.size(); i ++) 
            cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n";
            cout<<"after sorted by second:"<<endl;
            sort(vect.begin(), vect.end(), less_second);
            for(int i = 0 ; i < vect.size(); i ++) 
            cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n";
            
            return 0 ;
    }
    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值