0001 #include <cstdio> 0002 #include <cstring> 0003 #include <cstdlib> 0004 #include <ctime> 0005 #include "_share/util.h" 0006 #include "UniPrint/print.h" 0007 #include "vector/vector.h" 0008 0009 int main( int argc, char* argv[] ) { //基于快速划分的k选取算法测试 0010 if ( 2 > argc ) { printf ( "Usage: %s <n>\a\a\n", argv[0] ); return 1; } 0011 Rank n = atoi( argv[1] ); 0012 if ( n < 1 ) { printf ( "Invalid arguments\a\a\n" ); return 1; } 0013 srand((unsigned int)time(NULL)); //随机种子 0014 //srand( 31415926 ); //固定种子(假种子,调试用) 0015 0016 Vector<int> A; 0017 for ( Rank k = 0; k < n; k++ ) //随机创建一个向量 0018 A.insert( rand() % (4*n + 1) ); 0019 printf( "A random vector of size %d created:\n", n ); PRINT( A ); 0020 0021 Vector<int> S; 0022 for ( Rank k = 0; k < n; k++ ) 0023 S.insert( A[ A.select(k) ] ); //通过依次select,得到对应的排序向量(原向量保持不变) 0024 printf( "A sorted copy of the vector has been created by %d selection(s):\n", n ); PRINT( S ); 0025 printf( "The vector has been sorted:\n" ); A.sort(); PRINT( A ); 0026 0027 Rank diff = n; 0028 for ( Rank k = 0; k < n; k++ ) 0029 diff -= ( A[k] == S[k] ); 0030 printf( "%d diffenece(s) found\n", diff ); 0031 return 0; 0032 }