首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

数组索引排序 求优化建议 (欢饮拍砖)!该如何解决

2012-04-18 
数组索引排序求优化建议(欢饮拍砖)!C/C++ code/*求优化建议*//* 哪里不足尽管指出 */#include cstdlib#i

数组索引排序 求优化建议 (欢饮拍砖)!

C/C++ code
/*    求优化建议    *//* 哪里不足尽管指出 */#include <cstdlib>#include <iostream>#include <vector>#include <stdlib.h>#include <time.h>   using namespace std;   vector<unsigned long int>   a(0);                                //Index sortvector<unsigned long int>   b(0);                                //Save Indexunsigned long int StartIndex = 0;                                //The max value's absolute distance in array in accordance with 0unsigned long int N;                                             //The counts of array's elements unsigned long int MaxValueCounts;                                //The counts of filter max value will beshort int Compare(const unsigned long int &middle);void Insert(const unsigned long int &index);unsigned long int ConvertIndex(const long int index);            //Calculate the absolute distance in accordance with StartIndex//fast declare    unsigned long int d;                                         //Save the value of a[i]    unsigned long int CurFilterUpper;                            //The upper of array's filter max value    unsigned long int i;//end fast declareint main(int argc, char *argv[]){    double t1,t2,duration;        bool ifdo = true;    unsigned long int LeftIndex,RightIndex,Middle;        cout << "Please input the total of enter data value: ";    cin >> N;    cout << "Please input the counts of filter max value: " ;    cin >> MaxValueCounts;    a.resize(N + 1);    b.resize(MaxValueCounts + 2);     srand(time(NULL));    for (i = 1; i <= N; i++)    {        a[i] = rand();    }    t1 = clock();        //-------------Sort data value from max to min-------------//    a[0] = 0-1;     b[0] = 0;    b[1] = 1;    for (i = 2; i <= N; ++i)    {        d = a[i];        LeftIndex = 1;        RightIndex = CurFilterUpper = i - 1;                     //Save the upper of array's filter max value        if (CurFilterUpper > MaxValueCounts)        {            RightIndex = CurFilterUpper = MaxValueCounts;        }                ifdo = true;        do         {                Middle = (LeftIndex + RightIndex) / 2;               switch( Compare(a[ b[ConvertIndex(Middle)] ]) )             {                 case 1:                     if (LeftIndex >= RightIndex)                     {                         Insert(Middle + 1);                         ifdo = false;                         break;                     }                     LeftIndex = Middle + 1;                     break;                 case -1:                     if (LeftIndex >= RightIndex)                     {                         Insert(Middle);                         ifdo = false;                         break;                     }                     RightIndex = Middle;                     break;                 case 0:                     Insert(Middle + 1);                     ifdo = false;                     break;             };         } while (ifdo);    }    //------------------------Finished sort------------------------//        t2 = clock();    duration = t2 - t1;    cout << "Use time: " << duration << endl;    system("PAUSE");    for (i = 1; i <= MaxValueCounts; i++)        cout << a[ b[ConvertIndex(i)] ] << endl;       system("PAUSE");    return EXIT_SUCCESS;}short int Compare(const unsigned long int &middle){      if (middle < d) return -1;      if (middle > d) return 1;      return 0;}void Insert(const unsigned long int &index){    long int k;    unsigned long int middle = CurFilterUpper / 2;    if (middle < index)    {        for (k = CurFilterUpper; k >= index; --k)            b[ConvertIndex(k + 1)] = b[ConvertIndex(k)];        b[ConvertIndex(index)] = i;    } else {        b[ConvertIndex(CurFilterUpper + 1)] = b[ConvertIndex(- 1)];                for (k = 0; k <= index - 1; ++k)            b[ConvertIndex(k - 1)] = b[ConvertIndex(k)];        b[ConvertIndex(index - 1)] = i;                StartIndex = ConvertIndex(- 1);    }    return;}unsigned long int ConvertIndex(const long int index){                                                                                  return (StartIndex + index + (MaxValueCounts + 1)) % (MaxValueCounts + 1);      //Absolute distance base on 0     /*  应该就是上面这句,频繁调用忒别慢...  */} 



[解决办法]
侬撒意思?
看代码,结论泥?
[解决办法]
switch 里面居然不配default. 牛人啊。

热点排行