好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

STL学习笔记排序算法

STL学习笔记排序算法

STL学习笔记--排序算法

排序算法

    C++ STL 的排序算法(Sorting algorithms)是一组将无序序列排列成有序序列的模板函数或与排序相关的模板函数,提供了排序、折半搜索、归并、集合操作、堆操作、最值求解、字典比较和排列组合等功能。
    排序算法一般要求容器提供随机访问迭代器,一般适用于序列容器,如向量容器、队列容器和字符串容器等,但不适用于内部数据结构较为复杂的关联容器,如集合容器、映照容器、哈希集合容器和哈希映照容器等(有些容器是 SGI C++ STL里面的,在编译器自带的STL里面没有,这里不深入讨论,有兴趣的可以自己查相关资料)。

 目录:

元素入堆 push_heap

创建堆 make_heap

元素出堆 pop_heap

堆排序 sort_heap

局部排序 partial_sort

局部排序复制 partial_sort_copy

排序 sort

归并 merge

内部归并 inplace_merge

稳定排序 stable_sort

第n个元素 nth_emement

下确界 lower_bound

上确界 upper_bound

等价区间 equal_range

折半搜索 binary_search

子集合 includes

集合求并 set_union

集合求交 set_intersection

集合求差 set_difference

集合求异 set_sysmmetric_difference

最小元素 min_element

最大元素 max_element

字典比较 lexicographical_compare

下一排列组合 next_permutation

上一排列组合 prev_permutation

应用 push_heap 算法将新元素压入堆

  1   /*      下面示例程序将已构成堆的向量容器 v={38, 29, 32, 17, 26, 15, 11, 9, 10},压入最后一个元素60,使之仍然是堆。打印输出为“60 38 32 17 29 15 11 9 10 26”。
   2   */ 
  3  -----------------------------------------------  应用 push_heap 算法将新元素压入堆
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   void  print( int   x)
  10   {
  11      cout << x <<  "    "  ;
  12   }
  13  
 14   int   main()
  15   {
  16      vector< int >   v;
  17      v.push_back( 38  );
  18      v.push_back( 29  );
  19      v.push_back( 32  );
  20      v.push_back( 17  );
  21      v.push_back( 26  );
  22      v.push_back( 15  );
  23      v.push_back( 11  );
  24      v.push_back( 9  );
  25      v.push_back( 10  );
  26      v.push_back( 60  );
  27  
 28       //   将最后的元素60 入堆 
 29       push_heap(v.begin(), v.end());
  30       for_each(v.begin(), v.end(), print);
  31      cout <<  endl;
  32  
 33       return   0  ;
  34  }

应用 make_heap 算法在向量容器元素中建立堆

 1   /*      下面示例程序,将向量容器 v={5, 6, 4, 8, 2, 3, 7, 1, 9}元素调整为一个堆.  
   2   */ 
  3  -----------------------------------------------  应用 make_heap 算法在向量容器元素中建立堆
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   //   小顶堆 
 10   bool  comp( int  x,  int   y)
  11   {
  12       return  x > y ?  1  :  0  ;
  13   }
  14  
 15   void  print( int   x)
  16   {
  17      cout << x <<  "    "  ;
  18   }
  19  
 20   int   main()
  21   {
  22      vector< int >   v;
  23      v.push_back( 5  );
  24      v.push_back( 6  );
  25      v.push_back( 4  );
  26      v.push_back( 8  );
  27      v.push_back( 2  );
  28      v.push_back( 3  );
  29      v.push_back( 7  );
  30      v.push_back( 1  );
  31      v.push_back( 9  );
  32  
 33       for_each(v.begin(), v.end(), print);
  34      cout <<  endl;
  35  
 36      cout <<  "  创建堆  "  <<  endl;
  37       //   make_heap默认是2个参数,默认是创建大顶堆 
 38       make_heap(v.begin(), v.end(), comp);
  39  
 40       for_each(v.begin(), v.end(), print);
  41      cout <<  endl;
  42  
 43       return   0  ;
  44  }

应用 pop_heap 算法进行堆元素的出堆操作

  1   /*      下面示例程序先将数组 iArray构建成一个堆,然后进行一次元素出堆操作。
   2   从程序中可以看到,元素出堆,并没有将元素删除,只是将其移动位置,便于进行堆排序
   3   */ 
  4  -----------------------------------------------  应用 pop_heap 算法进行堆元素的出堆操作
   5  #include <algorithm>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   void  print( int   x)
  10   {
  11      cout << x <<  "    "  ;
  12   }
  13  
 14   int   main()
  15   {
  16       int  iArray[] = { 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9  };
  17       const   int  len =  sizeof (iArray) / sizeof ( int  );
  18  
 19      cout <<  "  创建堆  "  <<  endl;
  20      make_heap(iArray, iArray+ len);
  21      for_each(iArray, iArray+ len, print);
  22      cout <<  endl;
  23  
 24      cout <<  "  执行一次元素出堆  "  <<  endl;
  25      pop_heap(iArray, iArray+ len);
  26      for_each(iArray, iArray+ len, print);
  27      cout <<  endl;
  28  
 29       return   0  ;
  30  }

应用 sort_heap 算法对向量容器元素进行堆排序

 1   /*      下面示例程序先对向量容器v={3, 9, 6, 3, 12, 17, 20} 构建堆,然后利用 sort_heap 算法进行排序。(默认是小顶堆排序方式)
   2   */ 
  3  -----------------------------------------------  应用 sort_heap 算法对向量容器元素进行堆排序
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   void  print( int   x)
  10   {
  11      cout << x <<  "    "  ;
  12   }
  13  
 14   int   main()
  15   {
  16      vector< int >   v;
  17      v.push_back( 3  );
  18      v.push_back( 9  );
  19      v.push_back( 6  );
  20      v.push_back( 3  );
  21      v.push_back( 12  );
  22      v.push_back( 17  );
  23      v.push_back( 20  );
  24       for_each(v.begin(), v.end(), print);
  25      cout <<  endl;
  26  
 27       //   创建堆 
 28       make_heap(v.begin(), v.end());
  29  
 30       //   堆排序 
 31      cout <<  "  进行堆排序  "  <<  endl;
  32       sort_heap(v.begin(), v.end());
  33       for_each(v.begin(), v.end(), print);
  34      cout <<  endl;
  35  
 36       return   0  ;
  37  }

 应用 partial_sort 算法对数组元素进行局部排序

 1   /*      下面示例程序将数组iArray进行局部排序,middle=5时,给出前5个元素的排序;middle=8时,给出前8个元素的排序。
   2   */ 
  3  -----------------------------------------------  应用 partial_sort 算法对数组元素进行局部排序
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   int   main()
  14   {
  15       int  iArray[] = { 3 ,  9 ,  6 ,  8 , - 10 ,  7 , - 11 ,  19 ,  30 ,  12 ,  23  };
  16       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  17      for_each(iArray, iArray+ len, print);
  18      cout <<  endl;
  19  
 20       //   前5个元素的局部排序 
 21       int  middle =  5  ;
  22      partial_sort(iArray, iArray+middle, iArray+ len);
  23      cout <<  "  middle=  "  << middle <<  endl;
  24      for_each(iArray, iArray+ len, print);
  25      cout << endl <<  endl;
  26  
 27       //   前8个元素的局部排序 
 28      middle =  8  ;
  29      partial_sort(iArray, iArray+middle, iArray+ len);
  30      cout <<  "  middle=  "  << middle <<  endl;
  31      for_each(iArray, iArray+ len, print);
  32      cout << endl <<  endl;
  33  
 34       return   0  ;
  35  }

应用 partial_sort_copy 算法将数组拷贝到向量容器

 1   /*      下面示例程序分别将数组iArray局部排序复制6个元素到向量容器v1。将整个数组局部排序复制到向量容器v2。
   2   */ 
  3  -----------------------------------------------  应用 partial_sort_copy 算法将数组拷贝到向量容器
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   void  print( int   x)
  10   {
  11      cout << x <<  "    "  ;
  12   }
  13  
 14   int   main()
  15   {
  16       int  iArray[] = { 3 ,  9 ,  6 ,  2 ,  11 ,  23 ,  80 ,  27 ,  1 ,  62 ,  55  };
  17       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  18  
 19       //   仅拷贝6个元素到v1 
 20      vector< int >  v1( 6  );
  21      partial_sort_copy(iArray, iArray+ len, v1.begin(), v1.end());
  22       for_each(v1.begin(), v1.end(), print);
  23      cout << endl <<  endl;
  24  
 25       //   全部拷贝到 v2 
 26      vector< int >   v2(len);
  27      partial_sort_copy(iArray, iArray+ len, v2.begin(), v2.end());
  28       for_each(v2.begin(), v2.end(), print);
  29      cout <<  endl;
  30  
 31       return   0  ;
  32  }

应用 sort 算法将数组排序

 1   /*      下面示例程序将数组iArray元素进行 sort 排序,元素默认由小到大排列过来,然后用 greater 函数对象(也可以自己重新定义)作为 sort 的谓词判断,将元素有大到小排列出来。
   2   */ 
  3  -----------------------------------------------  应用 sort 算法将数组排序
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   bool  greater( int  x,  int   y)
   9   {
  10       return  x >  y;
  11   }
  12  
 13   void  print( int   x)
  14   {
  15      cout << x <<  "    "  ;
  16   }
  17  
 18   int   main()
  19   {
  20       int  iArray[] = { 2 ,  8 , - 15 ,  90 ,  26 ,  7 ,  23 ,  30 , - 27 ,  39 ,  55  };
  21       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  22  
 23      cout <<  "  由小到大排序  "  <<  endl;
  24      sort(iArray, iArray+ len);
  25      for_each(iArray, iArray+ len, print);
  26      cout <<  endl;
  27  
 28      cout <<  "  由大到小排序  "  <<  endl;
  29      sort(iArray, iArray+ len, greater);
  30      for_each(iArray, iArray+ len, print);
  31      cout <<  endl;
  32  
 33       return   0  ;
  34  }

 应用 merge 算法二路归并两个有序数组

  1   /*      下面示例程序分别对升序和降序的两个数组元素进行归并
   2   */ 
  3  -----------------------------------------------  应用 merge 算法二路归并两个有序数组
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   bool  greater( int  x,  int   y)
   9   {
  10       return  x >  y;
  11   }
  12  
 13   void  print( int   x)
  14   {
  15      cout << x <<  "    "  ;
  16   }
  17  
 18   int   main()
  19   {
  20       int  iArray1[ 3 ] = { 20 ,  23 ,  38  };
  21       int  iArray2[ 6 ] = { 2 ,  9 ,  13 ,  18 ,  26 ,  30  };
  22  
 23       //   升序归并 
 24       int  result[ 9  ];
  25      merge(iArray1, iArray1+ 3 , iArray2, iArray2+ 6  , result);
  26      for_each(result, result+ 9  , print);
  27      cout << endl <<  endl;
  28  
 29       //   降序归并 
 30       int  iArray3[ 5 ] = { 30 ,  20 ,  17 ,  8 ,  6  };
  31       int  iArray4[ 4 ] = { 10 ,  5 ,  2 ,  0  };
  32      merge(iArray3, iArray3+ 5 , iArray4, iArray4+ 4  , result, greater);
  33      for_each(result, result+ 9  , print);
  34      cout <<  endl;
  35  
 36       return   0  ;
  37  }

应用 inplace_merge 算法内部归并数组元素

  1   /*      下面示例程序对升序的数组iArray1 和 降序的数组 iArray2 分别进行内部归并。
   2   */ 
  3  -----------------------------------------------  应用 inplace_merge 算法内部归并数组元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   bool  greater( int  x,  int   y)
   9   {
  10       return  x >  y;
  11   }
  12  
 13   void  print( int   x)
  14   {
  15      cout << x <<  "    "  ;
  16   }
  17  
 18   int   main()
  19   {
  20       int  iArray1[] = { 2 ,  4 ,  6 ,  8 ,  10 ,  1 ,  3 ,  5 ,  7 ,  9 ,  11 ,  13  };
  21       const   int  len1 =  sizeof (iArray1) /  sizeof ( int  );
  22  
 23       //   从第 5 个元素开始,可以将iArray看作是 2 个单独的有序序列。
  24       //   升序内部归并 
 25      inplace_merge(iArray1, iArray1+ 5 , iArray1+ len1);
  26      for_each(iArray1, iArray1+ len1, print);
  27      cout << endl <<  endl;
  28  
 29       //   降序内部归并 
 30       int  iArray2[] = { 100 ,  80 ,  60 ,  40 ,  20 ,  10 ,  90 ,  70 ,  50 ,  30  };
  31       const   int  len2 =  sizeof (iArray2) /  sizeof ( int  );
  32      inplace_merge(iArray2, iArray2+ 6 , iArray2+ len2, greater);
  33      for_each(iArray2, iArray2+ len2, print);
  34      cout <<  endl;
  35  
 36       return   0  ;
  37  }

应用 stable_sort 算法对向量容器元素进行稳定排序

  1   /*      下面示例程序将学生记录放入向量容器,按学号进行 sort 算法排序,同分数的“李强”排在“丁宏”前面。然后,按分数进行 stable_sort 算法排序,“李强”依然在“丁宏”的前面,相对顺序维持不变。
   2   */ 
  3  -----------------------------------------------  应用 stable_sort 算法对向量容器元素进行稳定排序
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   //   学生结构体 
 10   struct   Student
  11   {
  12       int   id;
  13       char *  name;
  14       int   score;
  15      Student( int  id_,  char * name_,  int   score_)
  16       {
  17          id =  id_;
  18          name =  name_;
  19          score =  score_;
  20       }
  21   };
  22  
 23   //   按学号比较 
 24   bool    compByid(Student s1, Student s2)
  25   {
  26       return  s1.id < s2.id ?  1  :  0   ;
  27   }
  28  
 29   //   按分数比较 
 30   bool   compByscore(Student s1, Student s2)
  31   {
  32       return  s1.score < s2.score ?  1  :  0  ;
  33   }
  34  
 35   void   print(Student s)
  36   {
  37      cout << s.id <<  "    "  << s.name <<  "    "  << s.score <<  endl;
  38   }
  39  
 40   int   main()
  41   {
  42      vector<Student>   v;
  43      v.push_back(Student( 5 ,  "  李强  " ,  90  ));
  44      v.push_back(Student( 9 ,  "  王文  " ,  80  ));
  45      v.push_back(Student( 8 ,  "  张天  " ,  87  ));
  46      v.push_back(Student( 6 ,  "  丁宏  " ,  90  ));
  47      v.push_back(Student( 7 ,  "  赵庆  " ,  99  ));
  48  
 49      cout <<  "  按学号执行 sort 算法排序:  "  <<  endl;
  50       sort(v.begin(), v.end(), compByid);
  51       for_each(v.begin(), v.end(), print);
  52      cout <<  endl;
  53  
 54      cout <<  "  按分数执行 stable_sort 算法排序:  "  <<  endl;
  55       stable_sort(v.begin(), v.end(), compByscore);
  56       for_each(v.begin(), v.end(), print);
  57      cout <<  endl;
  58  
 59       return   0  ;
  60  }

应用 nth_element 算法排列数组元素

 1   /*      下面示例程序将数组iArray,进行重新排序,使第9个元素(从0开始计起)满足 nth_element算法要求。重排后的打印输出为“7, 8, 6, 2, 9, 5, 1, 3, 0, 10, 11, 13, 12”,可见重新排列后的第9个元素的值为10,它大于前面的元素,而又不大于后面的元素。 
   2   */ 
  3  -----------------------------------------------  应用 nth_element 算法排列数组元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   int   main()
  14   {
  15       int  iArray[] = { 7 ,  8 ,  6 ,  2 ,  9 ,  5 ,  10 ,  3 ,  0 ,  1 ,  13 ,  11 ,  12  };
  16       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  17      cout <<  "  打印 iArray 数组元素  "  <<  endl;
  18      for_each(iArray, iArray+ len, print);
  19      cout <<  endl;
  20  
 21       //   执行 nth_element 算法 
 22      cout <<  "  *nth设为第9个元素,打印iArray数组元素  "  <<  endl;
  23      nth_element(iArray, iArray+ 9 , iArray+ len);
  24      for_each(iArray, iArray+ len, print);
  25      cout <<  endl;
  26  
 27       return   0  ;
  28  }

用 lower_bound 算法找出不小于某值的有序数组下确界元素

 1   /*      下面示例程序找出数组iArray中首个不小于16的元素
   2   */ 
  3  -----------------------------------------------  应用 lower_bound 算法找出不小于某值的有序数组下确界元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7   int   main()
   8   {
   9       int  iArray[] = { 3 ,  6 ,  9 ,  13 ,  18 ,  20 ,  27  };
  10       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  11       int  *result = lower_bound(iArray, iArray + len,  16  );
  12      cout <<  "  数组iArray中不小于16的下确界元素为:  "  << *result <<  endl;
  13  
 14       return   0  ;
  15  }

用 upper_bound 算法,找出大于某值的有序数组上确界元素

 1   /*      下面示例程序找出数组iArray中首个大于13的元素
   2   */ 
  3  -----------------------------------------------  应用 upper_bound 算法,找出大于某值的有序数组上确界元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7   int   main()
   8   {
   9       int  iArray[] = { 3 ,  6 ,  9 ,  13 ,  18 ,  20 ,  27  };
  10       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  11       int  *result = upper_bound(iArray, iArray+len,  13  );
  12      cout <<  "  数组iArray 中大于13的上确界元素为:  "  << *result <<  endl;
  13  
 14       return   0  ;
  15  }

应用 equal_range 算法找出可插入某值的区间元素

 1   /*      下面示例程序查找数组iArray中,可以在前面插入10的区间元素。
   2   */ 
  3  -----------------------------------------------  应用 equal_range 算法找出可插入某值的区间元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   int   main()
  14   {
  15       int  iArray[] = { 3 ,  6 ,  9 ,  10 ,  10 ,  10 ,  13 ,  16 ,  17 ,  20  };
  16       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  17      pair< int *,  int *>  range = equal_range(iArray, iArray+len,  10  );
  18      cout <<  "  第一个可以插入10的元素为:  "  << *range.first <<  endl;
  19      cout <<  "  最后一个可以插入10的元素为:  "  << *range.second <<  endl;
  20      cout <<  "  所有可以在前面插入10的元素为:  "  ;
  21      for_each(range.first, range.second+ 1  , print);
  22      cout <<  endl;
  23  
 24       return   0  ;
  25  }

应用 binary_search 算法在有序区间中搜索元素

  1   /*      下面示例程序在已排序的数组 iArray 中,搜索出整数13
   2   */ 
  3  -----------------------------------------------  应用 binary_search 算法在有序区间中搜索元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   int   main()
   9   {
  10       int  iArray[] = { 2 ,  3 ,  9 ,  12 ,  13 ,  20 ,  23 ,  26  };
  11       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  12       if  (binary_search(iArray, iArray+len,  13  ))
  13          cout <<  "  数组iArray包含元素13   "  <<  endl;
  14       else 
 15          cout <<  "  数组iArray不包含元素13   "  <<  endl;
  16  
 17       return   0  ;
  18  }

应用 includes 算法判断集合包含关系

 1   /*      下面示例程序检查出数组 A={1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 构成的集合,包含数组 B={2, 4, 6, 8, 10} 的集合。
   2   */ 
  3  -----------------------------------------------  应用 includes 算法判断集合包含关系
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7   int   main()
   8   {
   9       int  A[] = { 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,  10  };
  10       const   int  lenA =  sizeof (A) /  sizeof ( int  );
  11       int  B[] = { 2 ,  4 ,  6 ,  8 ,  10  };
  12       const   int  lenB =  sizeof (B) /  sizeof ( int  );
  13  
 14       if  (includes(A, A+lenA, B, B+ lenB))
  15          cout <<  "  B 是A 的子集合  "  <<  endl;
  16       else 
 17          cout <<  "  B 不是A 的子集合  "  <<  endl;
  18  
 19       return   0  ;
  20  }

应用 set_union 算法进行集合求并

  1   /*      下面示例程序将集合A 和集合 B 进行求并操作,并集为 C. 可见C 的元素是稳定排序的,而且重复的元素3只出现两次。
   2   */ 
  3  -----------------------------------------------  应用 set_union 算法进行集合求并
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   int   main()
  14   {
  15       int  A[ 6 ] = { 3 ,  3 ,  6 ,  9 ,  10 ,  12  };
  16       int  B[ 3 ] = { 3 ,  8 ,  11  };
  17       int  C[ 8 ] = { 0  };
  18  
 19      set_union(A, A+ 6 , B, B+ 3  , C);
  20      for_each(C, C+ 8  , print);
  21      cout <<  endl;
  22  
 23       return   0  ;
  24  }

应用 set_intersection 算法进行集合求交

  1   /*      下面示例程序对集合 A 和 B 进行求交。
   2   */ 
  3  -----------------------------------------------  应用 set_intersection 算法进行集合求交
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   int   main()
  14   {
  15       int  A[ 10 ] = { 3 ,  6 ,  6 ,  9 ,  13 ,  17 ,  18 ,  20 ,  23 ,  25  };
  16       int  B[ 7 ] = {- 2 , - 1 ,  6 ,  9 ,  18 ,  30 ,  32  };
  17       int  C[ 3 ] = { 0  };
  18       //   求交集 
 19      set_intersection(A, A+ 10 , B, B+ 7  , C);
  20      for_each(C, C+ 3  , print);
  21      cout <<  endl;
  22  
 23       return   0  ; 

应用 set_difference 算法进行集合求差

  1   /*      下面示例程序对集合 A 、B 进行集合求差。
   2   */ 
  3  -----------------------------------------------  应用 set_difference 算法进行集合求差
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8  
  9   void  print( int   x)
  10   {
  11      cout << x <<  "    "  ;
  12   }
  13  
 14   int   main()
  15   {
  16       int  A[ 12 ] = { 1 ,  2 ,  3 ,  3 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,  10  };
  17       int  B[ 6 ] = { 2 ,  3 ,  4 ,  6 ,  8 ,  10  };
  18       int  C[ 6 ] = { 0  };
  19  
 20       //   求差集 
 21      set_difference(A, A+ 12 , B, B+ 6  , C);
  22      for_each(C, C+ 6  , print);
  23      cout <<  endl;
  24  
 25       return   0  ;
  26  }

应用 set_symmetric_difference 算法进行集合求异

  1   /*      下面示例程序对集合A、B进行求异。
   2   */ 
  3  -----------------------------------------------  应用 set_symmetric_difference 算法进行集合求异
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   int   main()
  14   {
  15       int  A[ 9 ] = { 3 ,  6 ,  6 ,  9 ,  12 ,  13 ,  15 ,  18 ,  20  };
  16       int  B[ 5 ] = { 2 ,  6 ,  15 ,  20 ,  30  };
  17       int  C[ 8 ] = { 0  };
  18  
 19       //   集合求异 
 20      set_symmetric_difference(A, A+ 9 , B, B+ 5  , C);
  21      for_each(C, C+ 8  , print);
  22      cout <<  endl;
  23  
 24       return   0  ;
  25  }

 应用 min_element 算法求双向链表的最小元素

  1   /*      下面示例程序求出双向链表{13, 6, 9, 3, 20}的最小元素3
   2   */ 
  3  -----------------------------------------------  应用 min_element 算法求双向链表的最小元素
   4  #include <algorithm>
  5  #include <list>
  6  #include <iostream>
  7   using   namespace   std;
   8   int   main()
   9   {
  10      list< int >   l;
  11      l.push_back( 13  );
  12      l.push_back( 6  );
  13      l.push_back( 9  );
  14      l.push_back( 3  );
  15      l.push_back( 20  );
  16  
 17      cout <<  "  链表l的最小元素为:  "  << *min_element(l.begin(), l.end()) <<  endl;
  18  
 19       return   0  ;
  20  }

应用 max_element 算法求向量容器的最大元素

 1   /*      下面示例程序求出向量容器 v={30, 25, 32, 23, 38, 21} 的最大元素为38
   2   */ 
  3  -----------------------------------------------  应用 max_element 算法求向量容器的最大元素
   4  #include <algorithm>
  5  #include <vector>
  6  #include <iostream>
  7   using   namespace   std;
   8   int   main()
   9   {
  10      vector< int >   v;
  11      v.push_back( 30  );
  12      v.push_back( 25  );
  13      v.push_back( 32  );
  14      v.push_back( 23  );
  15      v.push_back( 38  );
  16      v.push_back( 21  );
  17  
 18      cout <<  "  向量容器 V 的最大元素为:  "  << *max_element(v.begin(), v.end()) <<  endl;
  19  
 20       return   0  ;
  21  }

应用 lexicographical_compare 算法比较字符串字典大小

  1   /*      下面示例程序比较单词“book” 和 "house" 的字典顺序大小。
   2   */ 
  3  -----------------------------------------------  应用 lexicographical_compare 算法比较字符串字典大小
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   int   main()
   9   {
  10       char   *s1 =  "  book  "  ;
  11       const   int  len1 =  sizeof ( "  book  " ) /  sizeof ( char  );
  12  
 13       char *  s2 =  "  house  "  ;
  14       const   int  len2 =  sizeof ( "  house  " ) /  sizeof ( char  );
  15  
 16       bool  result = lexicographical_compare(s1, s1+len1, s2, s2+ len2);
  17       if   (result)
  18          cout <<  "  单词\"book\"在\"house\"前面  "  <<  endl;
  19       else 
 20          cout <<  "  单词\"book\"在\"house\"后面  "  <<  endl;
  21  
 22       return   0  ;
  23  }

next_permutation 算法重新组合排序数组元素

  1   /*      下面示例程序利用 next_permutation 算法,找出数组iArray 的下一较大组合排列,并进行由小到大的排序。 
   2   */ 
  3  -----------------------------------------------  next_permutation 算法重新组合排序数组元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13   //   排序函数 
 14  template< class  BidirectionalIter>
 15   void   nextPermu_sort(BidirectionalIter first, BidirectionalIter last)
  16   {
  17       while (next_permutation(first, last))  {}    //   利用较大的组合返回 true 
 18   }
  19  
 20   int   main()
  21   {
  22       int  iArray[] = { 3 ,  6 ,  2 ,  9 ,  8  };
  23       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  24      cout <<  "  原组合  "  <<  endl;
  25      for_each(iArray, iArray+ len, print);
  26      cout << endl <<  endl;
  27  
 28       //   新组合 
 29      cout <<  "  新组合  "  <<  endl;
  30      next_permutation(iArray, iArray+ len);
  31      for_each(iArray, iArray+ len, print);
  32      cout << endl <<  endl;
  33  
 34       //  
 35      cout <<  "  利用较慢的 next_permutation算法排序  "  <<  endl;
  36      nextPermu_sort(iArray, iArray+ len);
  37      for_each(iArray, iArray+ len, print);
  38      cout <<  endl;
  39  
 40       return   0  ;
  41  }

应用 prev_permutation 算法重新组合排序数组元素

  1   /*      下面示例程序利用 prev_permutation 算法,找出数组 iArray={1, 3, 9, 6, 7}的上一较小组合排列{1, 3, 7, 9, 6},并进行由小到大的排序。
   2   */ 
  3  -----------------------------------------------  应用 prev_permutation 算法重新组合排序数组元素
   4  #include <algorithm>
  5  #include <iostream>
  6   using   namespace   std;
   7  
  8   void  print( int   x)
   9   {
  10      cout << x <<  "    "  ;
  11   }
  12  
 13  template < class  BidirectionalIter>
 14   void   prevPermu_sort(BidirectionalIter first, BidirectionalIter last)
  15   {
  16       while  (prev_permutation(first, last)) {};  //   利用较小的组合返回  true 
 17   }
  18  
 19   int   main()
  20   {
  21       int  iArray[] = { 1 ,  3 ,  9 ,  6 ,  7  };
  22       const   int  len =  sizeof (iArray) /  sizeof ( int  );
  23  
 24      cout <<  "  原组合  "  <<  endl;
  25      for_each(iArray, iArray+ len, print);
  26      cout << endl <<  endl;
  27  
 28      cout <<  "  新组合  "  <<  endl;
  29      prev_permutation(iArray, iArray+ len);
  30      for_each(iArray, iArray+ len, print);
  31      cout << endl <<  endl;
  32  
 33      cout <<  "  利用较慢的 prev_permutation算法排序  "  <<  endl;
  34      prevPermu_sort(iArray, iArray+ len);
  35      for_each(iArray, iArray+ len, print);
  36      cout <<  endl;
  37  
 38  
 39       return   0  ;
  40  }

 
-------------------------- 排序算法小结
    C++ STL 提供了不少和排序有关的算法。直接可用于排序的算法都是一些内排序(相对于外排序),即排序过程均在内存进行,不需要进行内存、外存的数据交换,包括堆排序heap_sort算法、排序 sort 算法、稳定排序 stable_sort算法、局部排序算法 partial_sort 算法和partial_sort_copy 算法,他们均为【n倍 log以2为底 n的对数】的时间复杂度,一般要求提供随机访问迭代器。当元素个数n相当大时,具有理想的排序效率。
    与排序相关的算法包括,对有序区间进行排序的归并merge算法和内部归并inplace_merge算法、有序区间的折半搜索(下确界lower_bound算法/上确界upper_bound算法/等价区间equal_range算法/折半搜索binary_search算法)、集合操作(子集判断includes算法/求并set_union算法/求交set_intersection/求差set_difference/求异set_symmetric_difference)、最值(最小值min算法/最大值max算法/最小元素mim_element算法/最大元素算法max_element算法)、字典方式比较lexicographical_compare算法和排列组合next_permutation算法/prev_permutation算法。

作者: Music__Liang

出处: http://www.cnblogs.com/music-liang/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.--------------------------【保护知识产权,转载请注明出处】

 

分类:  STL学习笔记

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于STL学习笔记排序算法的详细内容...

  阅读:36次