10.31 Array learning algorithm - bubble, selection, dichotomy

Often ⻅ sorting algorithm efficiency than ⽐

¥ ae¿eaea a¾caee¿ °

Various sorting the best number of comparisons worst

https://blog.csdn.net/u013176681/article/details/41786263

Sorting algorithm - stability, the number of comparisons, the number of exchanges

https://blog.csdn.net/derhasebruder/article/details/78388637

Bubble Sort:

 

Bubble Sort

https://blog.csdn.net/kelinfeng16/article/details/84034386

Original Address: https: //github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md

Bubble Sort (Bubble Sort) it is also a simple and intuitive sorting algorithm. It repeatedly visited the number of columns to be sorted, a comparison of two elements, if they put them in the wrong order switching over. The number of visits to the column work is repeated until there is no longer need to swap, that is to say the number of columns already sorted completed. The origin of the name of the algorithm is because the smaller elements will slowly through the exchange of "float" to the top of the columns.

As one of the simplest sorting algorithms, feeling bubble sort I feel like Abandon word appears in the book, like, every time on the first page first, so the most familiar. There is also a bubble sort algorithm optimization is to set up a flag, when the element does not occur in the exchange trip traversal sequence, then prove that the sequence has been ordered. But for this improvement to enhance performance

He says and does not have much effect.

1. algorithm steps

Compare adjacent elements. If the first is greater than the second, the two of them exchanged.

Do the same work for each pair of adjacent elements, from the beginning to the end of the first to the last pair. Once this is done, the last element is the biggest number.

Repeat these steps for all elements, except the last one.

Continued to repeat the above steps for each time fewer and fewer elements, a pair of numbers until there is no need to compare.

https://blog.csdn.net/lu_1079776757/article/details/80459370


 

 

Explanation why were Ni-1 times  

冒泡排序就是把最大(最小)的放在前(后)面,那么后面的数据肯定已经排好了,不用再比较了,循环一次,排好一个数,那么再次循环的时候就把需要比较的长度-1,也就是-i。
如:
原数987654321
第一次876543219 比较次数9-1-0 两两比较,全部比
第二次765432189 这时候比较的次数是9-1-1,最后一个不用比
三,654321789 次数9-1-2 89不用比
...
...

 

#include<stdio.h>
#define N 8
int main()
{
	int i,j,k,t;
	int a[N]={99,1,3,6,22,11,56,31};
	for(i=1;i<N;i++)// 控制循环次数,执行七次循环; 
	{
		for(j=0;j<N-1;j++) //1.i=0;i<N-1; 2.i=1;i<N;因为每一次是用相邻两个元素比较,所以就是n-1次。
		{
			if(a[j]>a[j+1])//比较前后两数大小,替换 
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
	for(j=0;j<N;j++)//输出 a[0]-a[7] 
	printf("%d ",a[j]);
	printf("\n");
}




#include<stdio.h>
#define N 8
int main()
{
	int i,j,k,t;
	int a[N]={99,1,3,6,22,11,56,31};
	for(i=0;i<N-1;i++)// i=1;i<N;i++ 遍历次数,N-1次 
	{
		for(j=0;j<N-i-1;j++) //元素下标 a[0]-a[7] 
		{
			if(a[j]>a[j+1])//比较前后两数大小,替换 
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
	for(j=0;j<N;j++)//输出 a[0]-a[7] 
	printf("%d ",a[j]);
	printf("\n");
}


 

 选择排序

https://blog.csdn.net/kelinfeng16/article/details/84073496 

选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。

https://www.cnblogs.com/antusheng/p/10313347.html

1. 算法步骤

首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。

再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。

重复第二步,直到所有元素均排序完毕。

 

 

 

 

#include<stdio.h>
#define N 8
int main()
{
	int i,j,imax,t;
	int a[N]={36,1,3,6,89,101,56,31};
	for(i=0;i<N-1;i++)// i=1;i<N;i++ 遍历次数,N-1次 
	{
		imax=i;//令i为最大值 
		for(j=i+1;j<N;j++)
		{
			if(a[j]>a[imax])
			imax=j;	
		}
		t=a[i];
		a[i]=a[imax];
		a[imax]=t;
	}
	for(j=0;j<N;j++)//输出 a[0]-a[7] 
	printf("%d ",a[j]);
	printf("\n");
}

二分查找法

仅当列表有序的时候,二分查找才管用!!

https://blog.csdn.net/qq_42739440/article/details/96860023

【算法图解】 之 [二分查找法] 详解

https://blog.csdn.net/lan410812571/article/details/88120166

算法探索实录 - 1、二分查找算法

  • 二分查找算法优点:

  1. 查找次数较少
  2. 查找速度快
  3. 平均性能好

 

  • 二分查找算法缺点:

  1. 要求列表为有序列表
  2. 插入删除困难
  3. 不适用于经常变动的数据列表

 

 

#include<stdio.h>
#define N 8
int main()
{
	int i,j,index,t,x,low,high,mid,n=0;
	int a[N]={36,1,3,6,89,101,56,31};
	for(i=0;i<N-1;i++)// i=1;i<N;i++ 遍历次数,N-1次 
	{
		index=i;//令i为最大值 
		for(j=i+1;j<N;j++)
		{
			if(a[j]>a[index])
			index=j;	
		}
		t=a[i];
		a[i]=a[index];
		a[index]=t;
	}
	for(j=0;j<N;j++)//输出 a[0]-a[7] 
	printf("%d ",a[j]);
	printf("\n");
	printf("请输入你要查找的数:");
	scanf("%d",&x);
	low=0;
	high=N-1;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(x==a[mid])
		break;
		else if (x>a[mid])
		high=mid-1;   
		else
		low=mid+1;  
	}
	if(low<=high)
	{
	 printf("%d was found!\n",x);	
	}
	else
	printf("%d was not found!\n",x);
}

 

Published 57 original articles · won praise 27 · views 10000 +

Guess you like

Origin blog.csdn.net/ao_mike/article/details/102837833