本文主要是介绍算法导论CLRS 7 思考题 Stooge排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*7思考题 Stooge排序*STOOGE-SORT*/
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<vector>using namespace std;
typedef vector<int>::iterator ivecIte;size_t chkivIte(ivecIte iteB, ivecIte iteE)
{if(iteB > iteE) {cout<<"wrong with in iterator range!"<<endl;exit(0);}return iteE-iteB;
}
void stoogeSort(vector<int> ivec, ivecIte iteB, ivecIte iteE)
{chkivIte(iteB, iteE);if(1<iteE-iteB && *(iteE-1)<*iteB) {*(iteE-1) = *(iteE-1) ^ *iteB;*iteB = *(iteE-1) ^ *iteB;*(iteE-1) = *(iteE-1) ^ *iteB;}if(2<iteE-iteB) {size_t k = (iteE-iteB)/3;stoogeSort(ivec, iteB, iteE-k);stoogeSort(ivec, iteB+k, iteE);stoogeSort(ivec, iteB, iteE-k);}
}int main()
{vector<int> ivec;int inData;cout<<"input some integers with end-of-file!"<<endl;while(cin>>inData)ivec.push_back(inData);stoogeSort(ivec, ivec.begin(), ivec.end());for(ivecIte ite = ivec.begin(); ivec.end() != ite; ++ite) cout<<setw(5)<<*ite;cout<<endl;system("PAUSE");return EXIT_SUCCESS;
}
这篇关于算法导论CLRS 7 思考题 Stooge排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!