//选择排序
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