c++快速排序算法代码分享

  介绍

本篇内容介绍了“C + +快速排序算法代码分享”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

 # include  & lt; iostream>
  # include  & lt; algorithm>
  # include  & lt; string>
  # include  & lt; cctype>
  
  {struct 例子
  ,,,int  id;
  ,,,std:: string 名称;
  };
  
  template  & lt; class  T>
  
  template  & lt; class  T>
  
  bool  intCheck (int ,, int  b) {
  ,,,return  (& lt; a  b);
  }
  
  bool  stringCheck (std:: string ,, std:: string  b) {
  ,,,(int 小姐:=,0;,小姐:& lt;, a.length();,我+ +)
  ,,,,,,,一个[我],=,低([我]);
  ,,,(int 小姐:=,0;,小姐:& lt;, b.length();,我+ +)
  ,,,,,,,(我),b=,低(b[我]);
  ,,,如果(比较字符串(a.c_str (),, b.c_str()),在=,0)
  ,,,,,,,return 假;
  ,,,else  return 真实;
  }
  
  bool  exampleCheck (example ,, example  b) {
  ,,,如果(比较字符串(a.name.c_str (),, b.name.c_str()),在=,0),return 假;
  ,,,else  return 真实;
  }
  
  ,,,int  iArray[],=,{1, 2,,, 56岁,213年,,2,,32岁,32216年,14,};
  ,,,std:: string  sArray [],=, {,“Hello",,“请,断开连接;你?“,,“elephant",,“啊!”,,“zzzzz",,“queen",};
  ,,,example  structArray [],=, {, {1,“Joe"},
  ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,{4,“Billy"},
  ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,{2,“Zander"},
  ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,{3,“Tom"},};
  
  ,,,快速排序(iArray,, 0,, 7日,intCheck);
  ,,,快速排序(sArray,, 0,, 5日,stringCheck);
  ,,,快速排序(structArray,, 0,, 3,, exampleCheck);
  
  ,,,std:: cout  & lt; & lt;,“整数:“,& lt; & lt;, std:: endl;
  ,,,(int 小姐:=,0;,小姐:& lt;, 8;我+ +)
  ,,,,,,,std:: cout  & lt; & lt;, & # 39; t # 39;, & lt; & lt;, iArray[我],& lt; & lt;, std:: endl;
  
  ,,,std:: cout  & lt; & lt;, std:: endl  & lt; & lt;,“弦:“,& lt; & lt;, std:: endl;
  ,,,(int 小姐:=,0;,小姐:& lt;, 6,我+ +)
  ,,,,,,,std:: cout  & lt; & lt;, & # 39; t # 39;, & lt; & lt;, sArray[我],& lt; & lt;, std:: endl;
  
  ,,,std:: cout  & lt; & lt;, std:: endl  & lt; & lt;,“Structure  (By 名字):“,& lt; & lt;, std:: endl;
  ,,,(int 小姐:=,0;,小姐:& lt;, 4,我+ +)
  ,,,,,,,std:: cout  & lt; & lt;,“t {,“& lt; & lt;, structArray[我].id  & lt; & lt;,,,,,, & lt; & lt;, structArray[我].name  & lt; & lt;,,,},, & lt; & lt;, std:: endl;
  
  ,,,std:: cin.get ();
  ,,,return  0;
  }
  
  template  & lt; class  T>
  ,,,,int  pos =, l;
  ,,,,std:: swap (uA [r], uA (pos));
  ,,,,if  (l  & lt;, r) {
  ,,,,,,,std:: swap (uA [r], uA (pos));
  ,,,,,,,}
  ,,,,return  pos;
  }
  
  template  & lt; class  T>
  ,,,,如果(r 祝辞,l) {
  ,,,,,,,int  pos =,分区(uA,大敌;l, r, less );
  ,,,,,,,快速排序(uA,大敌;l, pos-1,,少);
  ,,,,,,,快速排序(uA,大敌;pos + 1,,,,更少);
  ,,,,}
  }

c++快速排序算法代码分享