温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-07 15:51:25 来源:网络 阅读:966 作者:浅唱逝流年 栏目:编程语言

//选择排序

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 <pSeqList->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;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI