当前位置:网站首页>C language: bubble sort

C language: bubble sort

2022-06-25 08:45:00 zbossz

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 10
void Bubble_Sort(int* a,int i,int j,int t)
{
    
	for (i = 0;i < N - 1;i++)
	{
    
		for (j = 0;j < N - i - 1;j++)
		{
    
			if (a[j] > a[j + 1])
			{
    
				t = a[j];
				a[j] = a[j + 1];
				a[j + 1] = t;
			}
		}
	}
}
int main()
{
    
	int i=0, j=0, t=0, a[N];
	printf(" Please enter %d It's an integer :",N);
	for (i = 0;i < N;i++)
		scanf("%d", &a[i]);
	Bubble_Sort(a,i,j,t);
	for (i = 0;i < N;i++)
		printf("%d ", a[i]);
	printf("\n");
	return 0;
}

flag In order to reduce the number of cycles of the constant sequence , To maximize efficiency .
such as 1111111111 This sequence , Just go in 10 Secondary cycle .
If not flag, I'll go in 100 Secondary cycle . The efficiency is greatly reduced .
 Insert picture description here

原网站

版权声明
本文为[zbossz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200556148089.html