selection sort

1. Definition

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

    The idea is: Assuming there are n unordered numbers, set i = 0; j = i+1; compare the i-th number and the j-th number if the i-th number

    If the number is larger, the two numbers are exchanged, otherwise unchanged. Then i and j+1 are compared until j=n, and then i is saved

    is the smallest number, and the first trip is completed. After repeating n-1 times, all numbers are sorted.

2. Use

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

void Selection_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++)//The main body of the selection sort algorithm
	{
		for(j=i+1;j<10;j++)
		{
			if(strcmp(array[i],array[j]))
			{
				strcpy(tem,array[j]);
				strcpy(array[j],array[i]);
				strcpy(array[i],tem);
			}		
		}	
	}
	for(i=0;i<10;i++)
	{
		printf("%s ",array[i]);	
	}	
}
intmain()
{
	Selection_Sort();	
}

Welcome to point out the deficiencies

    

Guess you like

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