码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-04-06 11:38:25      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:单链表   折半查找   选择排序 冒泡排序

//选择排序

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;

}


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

标签:单链表   折半查找   选择排序 冒泡排序

原文地址:http://10921020.blog.51cto.com/10911020/1760725

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!