Find the maximum of the three numbers(Take this opportunity to review the sorting algorithm)

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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nine_mink

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值