Software Technology Experimental Course - Four Sorting

Software Technology Experiment Class Code

 IV. Sort chapter

#include <stdio.h>
#include <time.h>
#define size 11
typedef char datatype;
typedef struct
{
	int key;
	datatype others;
} rectype;
/************插入排序********/
//26 5 37 1 61 11 59 15 48 19
void INSERTSORT(int a[])
{
	int i, j, k = 0;
	for(i = 2; i < size; i++)
	{
		a[0] = a[i];
		j = i - 1;
		while( a[0] < a[j])
		{
			a[j + 1] = a[j];
			j--;
		}
		a[j+1] = a[0];
	}
}
void SHELLSORT(rectype R[], int n)
{
	int i, j, h;
	rectype temp;
	h = n/2;
	while(h > 0)
	{
		for(j = h; j <= n - 1; j++)
		{
			temp = R[j];
			i = j - h;
			while((i >= 0) && temp.key < R[i].key)
			{
				R[i + h] = R[i];
				i = i - h;
			}
			R[i + h] = temp;
		}
		h = h/2;
	}
}
/************快速排序算法**************/
int PARTITION(rectype R[], int l, int h)
{
	int i, j;
	rectype temp;
	i = l, j = h, temp = R[i];
	do
	{
		while( ( R[j].key >= temp.key) && ( i < j))
			j--;
		if(i < j) R[i++]= R[j];
		while( (R[i].key <= temp.key) && (i < j))
			i++;
		if(i < j)
			R[j--] = R[i];
	}
	while(i != j);
	R[i] = temp;
	return i;
}
void QUICKSORT(rectype R[], int s1, int t1)
{
	int i;
	if(s1 < t1)
	{
		i = PARTITION(R,s1,t1);
		QUICKSORT(R,s1,i - 1);
		QUICKSORT(R,i+1,t1);
	}
}//快排
void main()
{
	int a[11]={0};
	rectype R[size];
	int i;
	printf("请输入使用插入排序法排序的10个数据:\n");
	for(i = 1; i < size; i++)
		scanf("%d",&a[i]);
	printf("排序之前\n");
	
	for(i = 1; i < size; i++)
		printf("%d\t",a[i]);
	printf("\n");
	
	INSERTSORT(a);
	printf("插入排序过后\n");
	for(i = 1; i < size; i++)
		printf("%d\t",a[i]);
	printf("\n");
	
	
	
	//希尔测试
	printf("\n请输入用于希尔排序测试的10个数据\n");
	for(i  = 0; i < size - 1; i++)
		scanf("%d",&R[i].key);
	printf("排序之前:\n");
	for(i = 0; i < size - 1; i++)
		printf("%d\t",R[i].key);
	SHELLSORT(R,10);
	printf("\n排序后:\n");
	for(i = 0; i < size - 1; i++)
		printf("%d\t",R[i].key);
		
		
		
	//快速排序
	printf("\n请输入用于快速排序算法的10个数据:\n");
	for(i = 1; i < size; i++)
		scanf("%d",&R[i].key);
	printf("排序前:\n");
	for(i = 1; i < size; i++)
		printf("%d\t",R[i].key);
	printf("\n排序后:\n");
	QUICKSORT(R,1,10);
	for(i = 1; i < size; i++)
		printf("%d\t",R[i].key);
}

Insertion sort: the R [0] is set to monitor the post, and then starting from the second digital input data, so that R [0] = the number,
and compares the sequentially forward if this number [0] is greater than R, then , this number will be a moving backwards, if this number [0] is small, and its rear a ratio R than R [0] is large, inserted between these two numbers R [0].
Hill sorting: Sort the first part of the data is good, then slowly expand the number of sorting data, the experiment data has the number 10, will then be n = 10, first h = n / 2, and preferably greater than the first row h is equal to the data subject under the element, then let h each half, until h = 0, to complete the entire order.
Quick Sort: is a first intermediate value set, then this will be less than the number of data movement to the left of it, will be greater than the intermediate data of the mobile element to its right and then to the intermediate value after the border, and then its data on the left and right repeat this until all the data sorting is completed.
Efficiency Comparison: Bubble <insert <Hill <fast

 

Guess you like

Origin blog.csdn.net/CSDNsabo/article/details/91863428