189 8069 5689

单链表的折半查找,冒泡排序,选择排序

//选择排序

伊川网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

void SelectSort(SeqList* pSeqList)

{

                 int i = 0, j = 0;

                 int iMaxPos = 0;

                 int iMinPos = 0;


                DataType temp;

for (; i < pSeqList->Size/2; ++i)

   {

          iMaxPos = i;

          iMinPos = i;

      for(j = 1; j < pSeqList->Size - i; j++)

           {

            if (pSeqList->arry[iMaxPos] < pSeqList->arry[j])

                     {

                                 iMaxPos = j;

                     }


            if (pSeqList->arry[iMinPos] > pSeqList->arry[j])

                     {

                              iMinPos = j;

                       }

            }


        if (iMaxPos != i)

          {

                   temp = pSeqList->arry[iMaxPos];

               pSeqList->arry[iMaxPos] = pSeqList->arry[pSeqList->Size - i - 1];

                pSeqList->arry[pSeqList->Size - i - 1] = temp;

            }


         if (iMinPos != i)

         {

                 temp = pSeqList->arry[iMinPos];

                   pSeqList->arry[iMinPos] = pSeqList->arry[i];

                   pSeqList->arry[i] = temp;

           }

    }

}

//冒泡排序

void BorbbleSort(SeqList* pSeqList)

{

                 int iCount = 0;

                 int iBegin = 0;

                 int iFlag = 0;

                 if (NULL == pSeqList)

                {

                                 return;

                }


        for (; iCount Size - 1; iCount++)

        {

               iFlag = 1;

               for (iBegin = 0; iBegin < pSeqList->Size - iCount - 1; ++iBegin)

                {

                    if (pSeqList->arry[iBegin] > pSeqList->arry[iBegin+1])

                      {

                                DataType temp = pSeqList->arry[iBegin];

                                pSeqList->arry[iBegin] = pSeqList->arry[iBegin+1];

                                pSeqList->arry[iBegin+1] = temp;

                                 iFlag = 0;

                        }

                   }

                   if (iFlag)

                   {

                          return;

                    }

          }

}


// 折半查找

int BinarySearch(SeqList* pSeqList, DataType data)

{

                 int iLeft = 0;

                 int iRight = pSeqList->Size;// [)

                 //int iMid = (iLeft + iRight)/2;

                 int iMid = 0;

                DataType temp;

                 if (NULL == pSeqList)

                {

                                 return;

                }


                 // [0, 4]

                 while(iLeft < iRight)

                {

                                iMid = iLeft + (iRight - iLeft)/2;

                                temp = pSeqList->arry[iMid];

                                 if (temp < data)

                                {

                                                iLeft = iMid;

                                }

                                 else if (temp > data)

                                {

                                                iRight = iMid - 1;

                                }

                                 else

                                {

                                                 return iMid;

                                }

                }


                 return -1;

}


当前文章:单链表的折半查找,冒泡排序,选择排序
标题URL:http://cdxtjz.com/article/jshehe.html

其他资讯