求一个数字在数组中排序,顺序搜索

    技术2022-05-20  67

    template <class T>void GetXH(T v[], int n, T order[]){ for(int x=0; x<n; x++) {  order[x] = 0; } for(int x=0; x<n; x++) {  for(int y=x+1; y<n; y++)  {   if(v[x] > v[y])    order[x]++;   else    order[y]++;  } }}

    template <class T>void GetXH2(T v[], int n, T order[]){ for(int x=0; x<n; x++) {  order[x] = 0; } for(int x=0; x<n; x++) {  for(int y=0; y<x; y++)  {   if(v[x] > v[y])    order[x]++;   else    order[y]++;  } }}

    template<class T>int ShunXuSouSuo(T v[], int n, T a){ int x = 0; for(; x<n && v[x] != a; x++); return x==n ? -1 : x;}

    int _tmain(int argc, _TCHAR* argv[]){ int v[] = { 4, 5, 9, 2 , 52, 2, 37, 8, 1}; int order[100]; GetXH2(v, _countof(v), order);

     _tprintf(TEXT("/nvalues;/n")); for(int x=0; x< _countof(v); x++) {  _tprintf(TEXT("d "), v[x]); }

     _tprintf(TEXT("/norders;/n")); for(int x=0; x< _countof(v); x++) {  _tprintf(TEXT("d "), order[x]); } _tprintf(TEXT("/n"));

     int nA = 37; int x = ShunXuSouSuo(v, _countof(v), nA); _tprintf(TEXT("%d'sIndex is %d/n"), nA, x);

     return 0;}

     


    最新回复(0)