Organization and handle integer array sort C language data exhaustive bubble sort algorithm

1. bubble sort
now to give you a thought, give you 10 different quality of plastic pellets, so you turn to separate lighter to heavier, weighing method can not solve, you can think of any way to use it?
Basic Concepts bubble sort (the BubbleSort) is: sequentially comparing adjacent two numbers, the numbers are placed ahead of large numbers on the back. I.e. the first pass: First, a first comparator and a second number, the decimal place before, after the release of large numbers. Then compare the second number and a third number, the decimal place before and after the release of large numbers, so continues until the last two numbers compare, before the decimal place, after the release of large numbers.
2. The bubble sort algorithm design principle of the
bubble sort algorithm operates as follows :( forward) from
(1) comparing adjacent elements. If the first is greater than the second, the two of them exchanged.
(2) for each pair of adjacent elements for the same job from the beginning to the end of a first of the last pair. At this point, it should be the last element is the largest number.
(3) repeating the above steps for all elements, except the last one.
(4) the duration of each repetition of the above steps fewer elements, until there is no need to compare a pair of numbers.
3. Application of the bubble sort
(1) from small to large,
the original to be sorted array |. 6 | 2 |. 4 | 1 |. 5 |. 9 |
(2) analysis using bubbling
① first pass sort (outer loop)
of a pairwise comparison 6> 2 exchange (inner loop)
exchange state before | 6 | 2 | 4 | 1 | 5 | 9 |
after switching state | 2 | 6 | 4 | 1 | 5 | 9 |
second twenty-two comparison 6> 4 exchange
switching state before | 2 | 6 | 4 | 1 | 5 | 9 |
After exchange state | 2 | 4 | 6 | 1 | 5 | 9 |
third pairwise comparisons 6> 1 exchange
before switching state | 2 | 4 | 6 | 1 | 5 | 9 |
after switching state | 2 | 4 | 1 | 6 | 5 | 9 |
fourth pairwise comparisons 6> 5-exchange
switching state before | 2 | 4 | 1 | 6 | 5 | 9 |
after switching state | 2 | 4 | 1 | 5 | 6 | 9 |
fifth pairwise comparison, 6 <9 not exchanged
prior to the exchange state | 2 | 4 | 1 | 5 | 6 | 9 |
after switching state | 2 | 4 | 1 | 5 | 6 | 9 |
② second Run sorting (outer loop)
first pairwise comparison 2 <4 not exchanged
prior to the exchange state | 2 | 4 | 1 | 5 | 6 | 9 |
after switching state | 2 | 4 | 1 | 5 | 6 | 9 |
second Ci pairwise comparison, 4> 1 exchange
before the exchange state | 2 | 4 | 1 | 5 | 6 | 9 |
after switching state | 2 | 1 | 4 | 5 | 6 | 9 |
third pairwise comparison, 4 < 5 does not exchange
switching state before | 2 | 1 | 4 | 5 | 6 | 9 |
after switching state | 2 | 1 | 4 | 5 | 6 | 9 |
fourth pairwise comparison, 5 <6 is not switched
before the switching status | 2 | 1 | 4 | 5 | 6 | 9 |
After exchange state | 2 | 1 | 4 | 5 | 6 | 9 |
③ third sorting times (outer loop)
first pairwise comparison 2> 1 switching
the switching status | 2 | 1 | 4 | 5 | 6 | 9 |
after exchange state | 1 | 2 | 4 | 5 | 6 | 9 |
second pairwise comparison, 2 <4 without switching
the switching state | 1 | 2 | 4 | 5 | 6 | 9 |
after switching state | 1 | 2 | 4 | 5 | 6 | 9 |
third pairwise comparison, 4 <5 without switching
the switching state | 1 | 2 | 4 | 5 | 6 | 9 |
after switching state | 1 | 2 | 4 | 5 |. 6 |. 9 |
④ fourth sorting times (outer loop) without exchanging
⑤ fifth sorting times (outer loop) no exchange
⑥ sorted, outputs a final result 124569

(3) the following reference code:

/* 从低到高排序 */
#include "stdio.h"
void main()
{
    int a[6] = { 6, 2, 4, 1, 5, 9 };
    int temp;
    int i,j;
	for (i = 1; i < 6; i++)
	{
	    for (j = 0; j <6-i ; j++)
	    {
	        if (a[j] > a[j+1])
	        {
	            temp = a[j];
	            a[j] = a[j+1];
	            a[j+1] = temp;
	        }
	    }
	}
	printf("从低到高依次为\n");
	
	for(i=0;i<6;i++)
	printf("%d\t",a[i]);
}

Fourth, live case presentation and analysis of
schools to be singing competition, through the layers of screening, and ultimately elected 10 students finals, now asked to do a program, 10 people entered the competition score, according to the level score, the first discharge name to tenth.
What is the author described as follows:
array storage judges (1) defines a scoring element 10.
(2) because the students to use 10 scores, so use an array
(3) must be entered prior to 10 people to sort of score
(4) Bubble Sort by score
(5) ordering complete output.
2. Case renderings as follows:
! [Inserted here described image]

Here Insert Picture Description

/* 利用冒泡排序解决实际问题 */
#include "stdio.h"
void main()
{
    int a[10];       //存放10位同学成绩的数组
    int temp;        //用于两数比较临时存放数据
    int i,j;         //循环变量
    //利用循环输入10位同学得分
    for(i=0;i<10;i++)
    {
    	printf("请输入第%d位同学得分:",i+1);
    	scanf("%d",&a[i]);    	
    }
    //冒泡排序
    for(i=1;i<10;i++)
    {
    	for(j=0;j<10-i;j++)
    	{
    		if(a[j]<a[j+1])
    		{
    			temp=a[j];
    			a[j]=a[j+1];
    			a[j+1]=temp;    			
    		}    		
    	}    	
    }  
    //输出结果
    printf("成绩从高到低依次是:\n");
    for(i=0;i<10;i++)
    {
    	printf("%d\t",a[i]);
    }       
}

Exhaustive achieve
exhaustive method (Exhaustion), also known as enumeration method (Enumeration): enumerate all possible, one by one temptation.
Two basic elements to solve the problem of the exhaustion method
1. Determine the object exhaustive and exhaustive range
 affect the time complexity of the algorithm, loop structure to achieve
2. Determine the determination condition
 meet the conditions that would be the answer to the problem, the branch structure to achieve
words Not much to say directly on the question!
Two, ACM exhaustive examples 27-- same cage chickens and rabbits (see below)
Here Insert Picture Description

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int i,j;
    for(i=1;i<98;i++)
    {
    	if(2*i+(98-i)*4==386)
    	{
    		printf("鸡%d头\n兔%d只\n",i,98-i);
    	}  	
    	
    }
 }

Fourth, the difference between the above-described analysis of the code following the code

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int i,j;
    for(i=1;i<98;i++)
    {
    	if(2*i+(98-i)*4==386)
    	{
    		printf("鸡%d头\n兔%d只\n",i,98-i);
           break;
    	}  	
    	
    }
    
}

Five, ACM 28 the number of cases of daffodils
in which the number of bits define each cube and daffodils equal to three-digit itself.
The above is known by a three-digit number daffodils, i.e. between 100 to 999; the number per cubic requirement is equal to itself.
Analysis: Suppose To determine the number is num is that the number, ten, one hundred numbers should be:
bit: G = num% 10;
S = num / 10% 10;
B = num / 100;
required to meet the conditions: G G G S + S S + B B B == NUM;
using a method like the following code is exhaustive;

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int num;//定义数变量
    int g,s,b;//定义个、十、百位变量名
    printf("水仙共数有:");
    for(num=100;num<=999;num++)//穷举测试
    {
    	g=num%10;      //个位数
    	s=num/10%10;   //十位数
    	b=num/100;     //百位数
    	if(g*g*g+s*s*s+b*b*b==num)   //判断条件
    	{
    		printf("%d\t",num);
    	}    	
    }    
}

Sixth, the difference between the analysis sample code above code

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int num;//定义数变量
    int g,s,b;//定义个、十、百位变量名
    printf("水仙共数有:");
    for(num=100;num<=999;num++)//穷举测试
    {
    	g=num%10;      //个位数
    	s=num/10%10;   //十位数
    	b=num/100;     //百位数
    	if(g*g*g+s*s*s+b*b*b==num)   //判断条件
    	{
    		printf("%d\t",num);
    		break;
    	}    
    	
    }    
}

Guess you like

Origin blog.csdn.net/SqrsCbrOnly1/article/details/91376035