Sort by bubble sort

1. Definition

    Bubble sort is a relatively simple sorting method, its complexity is O(n*n), it is a stable sorting method.

    Its idea is this, with n unordered numbers, we compare the first number with the adjacent number if the first one is smaller

   (or large, depending on whether you are in ascending or descending order, we default to descending order) then exchange the two numbers, otherwise do not make any changes

     Change, carry out a total of n-1 times to determine the smallest one at the top. This is the first pass, and the second pass uses the second number and the adjacent number.

     Make a comparison, compare n-2 times to determine that the second smallest is placed in the second place, and then repeat n-1 times to determine the order of all numbers.

2. Use

    Requirements: Input 10 strings to sort them, and output the sorted results.

void Bubble_Sort()
{
	int i = 0,j=0;
	char array[10][10];
	char has[10];
	printf("please input ten strings\n");//Prompt for input strings
	for(i=0;i<10;i++)
	{
		scanf("%s",array[i]);//Enter the input string into the array
	}
	for(i=0;i<9;i++)//Bubble sort algorithm main body
	{
		for(j=0;j<9-i;j++)
		{
			if(strcmp(array[j],array[j+1]))
			{
				strcpy(tem,array[j]);
				strcpy(array[j],array[j+1]);
				strcpy(array[j+1],tem);
			}		
		}	
	}
	for(i=0;i<10;i++)
	{
		printf("%s ",array[i]);	
	}	
}

intmain()
{
	Bubble_Sort();	
}

Welcome to point out the shortcomings

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326588700&siteId=291194637