当前位置:网站首页>TZC 1283: simple sort Bubble Sort

TZC 1283: simple sort Bubble Sort

2022-07-26 05:17:00 Orange teacher

We use TZC 1283 As an example, briefly explain the sorting ( Including ten classic sorting algorithms ) Of python Implementation method and C Implementation method . For bubble sorting principle, see :https://www.runoob.com/w3cnote/bubble-sort.html

In a word, it is : Compare two adjacent numbers , Large number of sinking bottom , Floating decimal .

Original link :1283: Simple order


Python The code is as follows :

#  Bubble sort 
def bubble_sort(lst):
    for a in range(len(lst) - 1):
        for b in range(len(lst)-1-a):
            if lst[b] > lst[b+1]:
                t = lst[b]
                lst[b] = lst[b+1]
                lst[b+1] = t


T = int(input())
for i in range(T):
    s = input().split()
    lt = [int(x) for x in s]
    lt1 = lt[::-1]
    lt1.pop()
    n = len(lt1)

    bubble_sort(lt1)

    for j in range(n):
        if j != n - 1:
            print(lt1[j], end=' ')
        else:
            print(lt1[j])

C The language code is as follows :

#include <stdio.h>
void cmpsort(int x[],int n)  //  Bubble sort , From front to back , Large number of sinking bottom , From the back forward , Floating decimal  
{
	int i,j,temp;
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-1-i;j++)
		{
			if(x[j]>x[j+1])
			{
				temp=x[j];
				x[j]=x[j+1];
				x[j+1]=temp;
			}
		}
	}
}
int main()
{
	int m,n,i,j,a[1000]={0};
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		cmpsort(a,n);
		for(i=0;i<n-1;i++)
		   printf("%d ",a[i]);
		printf("%d\n",a[n-1]);
	}
	return 0;
}

 

原网站

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