selection sort (C語言實現)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define   ulong  unsigned long


void swap2(int* a ,int* b)
{
	int m;
	m=*a;
	*a=*b;
	*b=m;
}



void selection_sort(int* f,ulong n)
{
	ulong i;
	for(i=0;i<n;i++)
	{
		int v=f[i];
		ulong m=i;
		ulong j=n;

		while(--j > i)
		{
			if(f[j] < v)
			{
				m =j;
				v=f[m];
			}
		}
		swap2(&f[i],&f[m]);
	}
}



int main()
{
	int a[]={7,4,3,0,5,3,2,5,8,4,3,2,5,4,3,5,4,5,3,2};
	ulong i;
	selection_sort(a,20);

	for(i=0;i<20;i++)
	{
		printf("%d\r\n",a[i]);
	}
	
	return 0;
}
相關文章
相關標籤/搜索