快速排序

来源:互联网 发布:sql server 2008 sp4 编辑:程序博客网 时间:2024/06/05 19:11

int Random(int start,int end)  

{  

   int t;  

   srand((unsigned int )time(NULL));   

    while(true)  

    {  

        t=rand()%(end+1);  

       if(t>=start)  

           return t;  

    }  

}

int findPartition(int aLow,int aHight)

{

   char x;

    //srand((unsigned  int )time(0));

    //int randIndex = aLow+rand()%(aHight-aLow);

    

//    int randIndex = Random(aLow,aHight);

//    x = temp[randIndex];

//    temp[randIndex] = temp[aHight];

//    temp[aHight] = x;

   int par = temp[aHight];

   int i = aLow-1;

    

   for (int j = aLow; j < aHight;++j) 

    {

       if (par >= temp[j]) 

        {

            ++i;

            x =temp[i];

           temp[i] = temp[j];

           temp[j] = x;

        }

    }

    

    x =temp[i+1];

   temp[i+1] = temp[aHight];

   temp[aHight] = x;

   return i+1;

}

void quickSort(int aLow,int aLen)

{

   if (aLow < aLen) 

    {

       int privote = findPartition(aLow,aLen); 

       quickSort(aLow,privote-1);

       quickSort(privote+1,aLen);

    }

}

原创粉丝点击