选择排序(C语言实现)

选择排序

void select_sort(int *a,int length)
{
	int i, j, temp, min, pos;
	for(i=0; i<=length-1; i++)
	{
		min = a[i]; 
		for(j=i; j<=length-1; j++)
			if(a[j]<min) {
				min = a[j];
				pos = j;//记录最小元素的位置 
				//交换最小元素和第一个元素 
				temp = a[i]; 
				a[i] = a[pos]; 
				a[pos] = temp;
			}			
	}					
} 
int main()
{
    int array[] = {6, 4, 10, -7, 23, 4, 8, 13,77};
    int len = (int)sizeof(array) / sizeof(int); 
    select_sort(array,len);
    int i;
    for(i=0; i<=len-1; i++) printf("%d ",array[i]); 
    return 0;
}

输出结果如下:

-7 4 4 6 8 10 13 23 77
--------------------------------
Process exited after 0.1093 seconds with return value 0
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/qq_41580631/article/details/88528239