Posted on 2008-08-27 15:45
dengbo 阅读(751)
评论(0) 编辑 收藏 引用 所属分类:
Data Struct
void SelectSort(int num[], int count)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int tmp,minIndex;
for(int i=0; i<count; i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
minIndex = i;
for(int j=i+1; j<count; j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num[minIndex]>num[j])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
minIndex=j;
}
}
if(minIndex != i)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = num[i];
num[i] = num[minIndex];
num[minIndex] = i;
}
}
}
void quicksort (int a[], int lo, int hi)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
// lo is the lower index, hi is the upper index
// of the region of array a that is to be sorted
int i=lo, j=hi, h;
int x=a[(lo+hi)/2];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// partition
do
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (a[i]<x) i++;
while (a[j]>x) j--;
if (i<=j)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h=a[i]; a[i]=a[j]; a[j]=h;
i++; j--;
}
} while (i<=j);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// recursion
if (lo<j) quicksort(a, lo, j);
if (i<hi) quicksort(a, i, hi);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)