C language -- bubble selection sort (pointer use)

Bubble Sort

time complexity

If the initial state of the file is in positive order, the sorting can be completed in one scan. Required number of keyword comparisons
and record the number of moves
both reach the minimum value:
Therefore, the best time is
.
  If the initial file is in reverse order, you need to do
Sort by. Sorting is done each time
The comparison of the second keyword (1≤i≤n-1), and each comparison must move the record three times to achieve the exchange record position. In this case, both comparisons and moves are at their maximum:
The worst time complexity of bubble sort is
To sum up, so the total average time complexity of bubble sort is


Algorithm Stability

Bubble sort is to move small elements forward or large elements backward. A comparison is a comparison of two adjacent elements, and a swap also occurs between these two elements. So, if two elements are equal, I think you will not be boring to swap them; if two equal elements are not adjacent, then even if the two are adjacent by the previous pairwise swap, this The time will not be exchanged, so the order of the same elements has not changed, so the bubble sort is a stable sorting algorithm.
C language algorithm
void maopao(int a[],int n)
{
	int i,j,t;
	for(i=0;i<n-1;i++)
		for(j=0;j<n-1-i;j++)
			if(a[j]>a[j+1])
			{
				t=a[j];a[j]=a[j+1];a[j+1]=t;
			}
    for(i=0;i<n;i++)
		printf(" %d ",a[i]);
	printf("\n");
}
pointer version
void maopao1(int *p,int n)
{
	int i,j,k,*p1;
	p1=p;
	for(i=0;i<n-1;i++)
		for(j=0;j<n-1-i;j++)
			if(*(p+j)>*(p+j+1))
			{
				k=*(p+j);*(p+j)=*(p+j+1)=k;
			}
    for(i=0;i<n;i++)
		printf(" %d ",*(p1+i));
	printf("\n");
}
选择排序

基本选择排序


排序算法即解决以下问题的算法:

输入

n个数的序列<a1,a2,a3,...,an>。

输出

原序列的一个重排<a1*,a2*,a3*,...,an*>;,使得a1*<=a2*<=a3*<=...<=an*
排序算法有很多,包括 插入排序冒泡排序堆排序归并排序,选择排序, 计数排序基数排序桶排序快速排序等。插入排序,堆排序,选择排序,归并排序和快速排序,冒泡排序都是比较排序,它们通过对 数组中的元素进行比较来实现排序,其他排序算法则是利用非比较的其他方法来获得有关输入数组的排序信息。

思想

n个记录的文件的 直接选择排序可经过n-1趟直接选择排序得到有序结果:
①初始状态:无序区为R[1..n],有序区为空。
②第1趟排序
在无序区R[1..n]中选出 关键字最小的记录R[k],将它与无序区的第1个记录R[1]交换,使R[1..1]和R[2..n]分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。
……
③第i趟排序
第i趟排序开始时,当前有序区和无序区分别为R[1..i-1]和R(i..n)。该趟排序从当前无序区中选出关键字最小的记录 R[k],将它与无序区的第1个记录R交换,使R[1..i]和R分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。

     C语言实现
 
 
void xuanze(int a[],int n)
{
 int i,j,k,t;
 for(i=0;i<n-1;i++)
 {
  k=i;
  for(j=i+1;j<n;j++)
   if(a[j]<a[k])
   {
    t=a[j];a[j]=a[k];a[k]=t;
   }
 }
 for(i=0;i<n;i++)
  printf(" %d ",a[i]);
 printf("\n");
}

指针版

void xuanze1(int *p,int n)
{
	int i,j,k,t,*p1;
	p1=p;
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
			if(*(p+j)<*(p+k))
			{
				t=*(p+j);*(p+j)=*(p+k);*(p+k)=t;
			}
	}
    for(i=0;i<n;i++)
		printf(" %d ",*(p1+i));
	printf("\n");
}

完整

#include<stdio.h>
void maopao(int a[],int n);
void xuanze(int a[],int n);
void maopao1(int *p,int n);
void xuanze1(int *p,int n);
void main()
{
	int i;
	int a[10];;
	printf("输入每个数:");
	for(i=0;i<10;i++)
		scanf("%d",&a[i]);
	printf("\n冒泡排序");
	maopao(a,10);
	printf("\n选择排序");
	xuanze(a,10);
	printf("\n选择排序");
	maopao1(a,10);
	printf("\n选择排序");
	xuanze1(a,10);
}

void maopao(int a[],int n)
{
	int i,j,t;
	for(i=0;i<n-1;i++)
		for(j=0;j<n-1-i;j++)
			if(a[j]>a[j+1])
			{
				t=a[j];a[j]=a[j+1];a[j+1]=t;
			}
    for(i=0;i<n;i++)
		printf(" %d ",a[i]);
	printf("\n");
}

void xuanze(int a[],int n)
{
	int i,j,k,t;
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
			if(a[j]<a[k])
			{
				t=a[j];a[j]=a[k];a[k]=t;
			}
	}
	for(i=0;i<n;i++)
		printf(" %d ",a[i]);
	printf("\n");
}

void maopao1(int *p,int n)
{
	int i,j,k,*p1;
	p1=p;
	for(i=0;i<n-1;i++)
		for(j=0;j<n-1-i;j++)
			if(*(p+j)>*(p+j+1))
			{
				k=*(p+j);*(p+j)=*(p+j+1)=k;
			}
    for(i=0;i<n;i++)
		printf(" %d ",*(p1+i));
	printf("\n");
}

void xuanze1(int *p,int n)
{
	int i,j,k,t,*p1;
	p1=p;
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i+1;j<n;j++)
			if(*(p+j)<*(p+k))
			{
				t=*(p+j);*(p+j)=*(p+k);*(p+k)=t;
			}
	}
    for(i=0;i<n;i++)
		printf(" %d ",*(p1+i));
	printf("\n");
}

结果

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727707&siteId=291194637