Problem Description
Input
Output
Example Input
5,7,9
Example Output
max=9
Although this topic is simple, you can directly take a maximum value and then can Accept.However, we can use this title complex learning sort algorithm.
Bubble Sort:
First read the n data with the cycle.
From a [1] to a [n], the two adjacent numbers are compared to two, which is a distinctive feature of bubble sorting.That is: a [1] and a [2] ratio, a [2] and a [3] than a [n-1] and a [n] ratio.
It is only necessary to know the symbol of the preceding element in the two numbers, and it can be compared with the latter number element (adjacent number), which can be written as the general form a [i] and a [i + 1], then the number of times And can be controlled by 1 ~ (n-1) cycles (ie, the number of cycles is related to the element number in front of the two).
In each comparison, if the larger number in the back, put the two pairs, the larger number transferred to the front, or do not need to swap position.
The following list of five numbers to illustrate the two comparison and exchange of specific circumstances:
5 6 4 3 7 5 and 6, the exchange position, in the following order;
6 5 4 3 7 5 and 4 comparison, not exchange;
6 5 4 3 7 4 and 3 are not exchanged;
6 5 4 3 7 3 and 7, the exchange position, in the following order;
6 5 4 7 3 3 was transferred to the end.
After the first round of 1 ~ (n-1) times comparison, you can put the smallest number of 10 number to the end of the position, the second round of comparison 1 ~ (n-2) times the same treatment, Of the "minimum number" transferred to the range of "the last end of the position" ... ..., each after a round of two comparison, the next round of the scope of the comparison to reduce one. The last round is only once compared.In the process of comparison, each time there is a "minimum number" down "out", in this way the order, known as the bubble sort.
Here I am talking about the bubble sort of improvement:
In the bubble sort, you can find that after a number of exchange data may have been ordered, then you can end the sort.But the computer has been ordered when the data is not known to have been ordered, so when a trip comparison, if there is no data exchange, then know that the order has been ordered, you can stop.
We set a variable bo to record whether the exchange, the value of 0 that the exchange was carried out in this trip, the value of 1 is not.
Here are only the code for the unoptimized algorithm:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,n;
int a[1000];
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (j=1;j<=n-1;j++)//Since the following a [i] is to be compared with a [i + 1] so we only loop to n-1.
for (i=1;i<=n-j;i++)//Since the back of n-j are already ordered, it is only looped to n-j.
if (a[i]<a[i+1])
{k=a[i]; a[i]=a[i+1]; a[i+1]=k;}
for (i=1;i<=n;i++) printf("%d ",a[i]);
printf("Hello world!\n");
return 0;
}
It is not enough to sort the bubble.Bubble sorting is the most basic sorting algorithm, and its time complexity reaches O (n ^ 2).For a little bit of data will be TLE, so, then we will speak unstable sorting but quickly sort quickly, and stable sorting but the amount of code is very large heap sort.