c排序算法:选择、冒泡、插入、快速、归并、堆排序

1.选择排序法

#include
int main()
{
	int a[5]={2,5,3,1,4};
	int n=5,i,k,b,x;
	for(b=0;b<=4;b++)
	{
		printf("%d",a[b]);
	}
	printf("\n");
	for (i = 0; i< n-1; i++ ) 
	{
		for(k = i+1; k < n; k++ )
		{
			if (a[k] < a[i] ) //选择排序法,每次循环找一个最小的值
			{
				x = a[i]; 
				a[i] = a[k]; 
				a[k] = x;
			}
			for(b=0;b<=4;b++)
			{
				printf("%d",a[b]);
			}
			printf("\n");
		}
	
	}
	return 0;
}
2.冒泡排序法

#include
int main()
{
	int a[5]={2,5,3,1,4};
	int n=5,i,k,b,x;
	for(b=0;b<=4;b++)
	{
		printf("%d",a[b]);
	}
	printf("\n");
	for (i =n-1;i>=0; i-- ) 
	{
		for(k = 0; k < i; k++ )
		{
			if (a[k] > a[k+1] ) //冒泡排序法,两两比较交换位置
			{
				x = a[k+1]; 
				a[k+1] = a[k]; 
				a[k] = x;
			}
			for(b=0;b<=4;b++)
			{
				printf("%d",a[b]);
			}
			printf("\n");
		}
	
	}
	return 0;
}
3.插入排序法

#include
int main()
{
	int a[5]={2,5,3,1,4};
	int i,j,k,n=5,key;
	for(k=0;k<5;k++)
	{
		printf("%d",a[k]);
	}
	printf("\n");
	for(i=1;i=0))
		{
			a[j+1]=a[j];//插入排序法,每个数,比较该数左边的数直到找到合适位置
			a[j]=key;
			j--;
			for(k=0;k<5;k++)
			{
				printf("%d",a[k]);
			}
			printf("\n");
		}
		
	}
	return 0;
}
4.快速排序法

#include
void Q_SORT(int a[],int num);
int main()
{
	int k=0;
	int a[9]={3,6,5,7,9,1,8,2,4};
	Q_SORT(a,9);
	for(k=0;k<9;k++)
	{
		printf("%d",a[k]);
	}
	printf("\n");
	return 0;
}
void Q_SORT(int a[],int num)//把第一个数组元素在第一次循环后放在合适的位置,之后对其前后的数组元素进行快排,递归调用该函数
{
	int i=0,j=num-1;
	int key=a[0];
	if(num>1)
	{
		while(i!=j)
		{
			for(;ikey)
				{
					a[j]=a[i];
					break;
				}
			}
			a[i]=key;
		}
		Q_SORT(a,i);//快速排序法的递归调用
		Q_SORT(a+i+1,num-i-1);//快速排序法的递归调用
	}
}
5.归并排序法

#include
#include
void merge(int a[],int p,int q,int r);
void merge_sort(int a[],int p,int r);
int main()
{
	int a[8]={1,2,4,5,3,9,7,8};
	int i;
	merge_sort(a,0,7);
	for(i=0;i<8;i++)
	{
		printf("%d",a[i]);
	}
	printf("\n");
	return 0;
}
void merge(int a[],int p,int q,int r)//合并排序函数
{
	int i,j,k,n1,n2;
	int *front;
	int *back;
	n1=q-p+1;
	n2=r-q;
	front = (int *)malloc(n1*sizeof(int));
	back = (int *)malloc(n2*sizeof(int));
	for(i=0;i

6.堆排序

#include
typedef struct heap_t
{
	int *arr;	       
	int heapMaxIndex;	//heapMaxIndex是数组最大的序号
	int arrLength;	
}Heap;

void maxHeapify(Heap *hp, unsigned int nodei)
{
	unsigned int l = (nodei+1) << 1 - 1;	//left child = 2i-1
	unsigned int r = (nodei+1) << 1 ;	// right child = 2i
	unsigned int largest = 0;
	int heapMaxI = hp->heapMaxIndex;

	if(l <= heapMaxI && hp->arr[l] > hp->arr[nodei])
		largest = l ;
	else
		largest = nodei ;
	
	if(r <= heapMaxI && hp->arr[r] > hp->arr[largest])
		largest = r ;

	if(largest!=nodei)
	{	
		//exchange 
		int tmp ;
		tmp = hp->arr[largest];
		hp->arr[largest] = hp->arr[nodei];
		hp->arr[nodei] = tmp;
		
		maxHeapify(hp,largest);//下标为largest的节点在交换后作为父节点,可能又违反堆性质,因此递归调用该函数。
	}else{
		return ;
	}
}
Heap *createHeap(int *arrp, int arrLength,Heap *heap)
{
 int i;
 heap->arr = arrp;
 heap->heapMaxIndex = arrLength-1;
 heap->arrLength = arrLength;

 for(i = arrLength>>1-1; i >=0; i--) //对于1个个数为n的堆,n/2-1之前的都是父节点,之后的都是叶子节点,我们只需要对父节点进行maxHeapify就可以了。
  maxHeapify(heap,i);
 return heap;
}

void heapSort(Heap *hp)
{
	int tmp;
	int last;
	while(hp->heapMaxIndex>0)
	{
		last = hp->heapMaxIndex ;
		//exchange
		tmp = hp->arr[last];
		hp->arr[last] = hp->arr[0];
		hp->arr[0] = tmp;
		hp->heapMaxIndex -= 1;
		maxHeapify(hp,0);	//make heap from root node 0 to heapMaxIndex 
	}
	
}

void printArr(int *p, int size)
{
	int i;
	for(i=0; i


你可能感兴趣的:(c排序算法:选择、冒泡、插入、快速、归并、堆排序)