Algorithm: Sort Summary: small hum books

ahalei · Updated on 2018-11-28 11:00:43

Sort Summary: small hum books

Speaking before the three commonly used classic sort. There are many sorting algorithm, for example, selection sort, counting sort, radix sort, insertion sort, heap sort and merge sort like. Heap sort is sorted based on binary tree, talk about it later. To share a cool video of sorting algorithms.

Let's look at a specific example of a "small hum books" to look at the difference between three sorting and limitations on the application. Small hum of the school to build a library corner, the teacher sent a small hum to find some students to do research to see what students like to read books. Small Well let each student to write a book ISBN number of their most want to read (you know? Each book has a unique ISBN number, do not believe, then you go to the back of the book turn to look at). Of course, there are some good books there will be many students are like, which would collect a lot of duplicate ISBN number. Small hum which need to remove duplicate ISBN number, that is, each ISBN number but one, is to say only buy the same book (the school is quite stingy). Then put these ISBN numbers from small to large, small hum accordance sorted ISBN number to the bookstore to buy books. Please help small hum Finish "go heavy" and "sort" work.

picturez.1

Row 2 is input, the behavior of a first positive integer representing the students participating in the survey of n (n <= 100). The second row has space-separated positive integer number n for each book ISBN number (assuming the ISBN number between 1 and 1000).

Output is 2 line, the first acts of a positive integer k, expressed the need to buy a lot of books. Second row k space-separated positive integers, from small to large has been scheduled for the book ISBN numbers need to buy a good sequence. For example, enter

102040326740208930040015

Output

8152032406789300400

Finally, the program run time limit: 1 sec.

The solution to this problem are basically two, the first method: First ISBN number of the book to the n weight, then sort and output from small to large. The second method: first, from small to large, when the output to go heavy. Both methods can be.

The first method first look. Through the first study we found little change bucket sort can just go play the effect of weight, so we can use the bucket sort of way to solve this problem.


    #include <stdio.h>
    int main()
    {
       int a[1001],n,i,t;
        for(i=1;i<=1000;i++)
             a[i]=0; //初始化

        scanf("%d",&n); //读入n
        for(i=1;i<=n;i++) //循环读入n个图书的ISBN号
        {
            scanf("%d",&t); //把每一个ISBN号读到变量t中
            a[t]=1; //标记出现过的ISBN号
        }

        for(i=1;i<=1000;i++) //依次判断1~1000这个1000个桶
        {
              if(a[i]==1)//如果这个ISBN号出现过则打印出来
                 printf("%d ",i);
        }

        getchar();getchar();
        return 0;
    }

Time complexity of this method is that time complexity bucket sort is O (N + M).

The second way we need to go re-ordering. We can sort or quick sort used bubble sort.

20 40 32 67 40 20 89 300 400 15

After these 10 small to large number of 1,520,203,240,406,789,300 400

Next, at the time to remove duplicate output. Because we have already sorted, so the same number will be close together. As long as at the time of output current at the pre-determined number with a number in front of a a [i-1] are the same. If the same number has already said this before output over a different output again. Different, it said this is the first time the number required, you need to output this number.


    #include <stdio.h>
    int main()
    {
        int a[101],n,i,j,t;

        scanf("%d",&n);   //读入n
        for(i=1;i<=n;i++) //循环读入n个图书ISBN号
        {
            scanf("%d",&a[i]);
        }

       //开始冒泡排序
        for(i=1;i<=n-1;i++)
        {
             for(j=1;j<=n-i;j++)
            {
                 if(a[j]>a[j+1])
                {  t=a[j]; a[j]=a[j+1]; a[j+1]=t;  }
            }
        }
        printf("%d ",a[1]); //输出第1个数
        for(i=2;i<=n;i++) //从2循环到n
        {
             if( a[i] != a[i-1] ) //如果当前这个数是第一次出现则输出
                 printf("%d ",a[i]);
        }

        getchar();getchar();
        return 0;
    }

Time complexity of this method consists of two parts, a part of the bubble sort is the time complexity of O (N2 of), the other is read and the output is O (N), so the time complexity of the algorithm is O ( 2 N + N2 of). N2 is relative, 2 N negligible (typically we ignore the low-order), the method final time complexity is O (N2).

Next we also need to look at the data range. Each book ISBN number is an integer between 1 to 1000, and the number of students participated in the survey does not exceed 100, that n <= 100. Having said that, when the time complexity of a rough calculation, we usually think about computer running one billion times per second (of course, the actual situation is faster). Thus the above two methods can be calculated in a second solution. If the subject of the book ISBN number is not in the range between 1 and 1000, but between -2147483648 to 2147483647, then the first method is not feasible, because you can not apply for such a large array to mark out each ISBN number appears too. Further if the range of n is not equal to 100 but less than 100,000 or less, then the sorting portion of the second method can not be used bubble sort. Because the requirements of the subject time limit is 1 second, using bubble sort number sort 100,000, the computer needs to run 10 billion times, 10 seconds, need to be replaced quickly sort, quicksort only need 100000 × log2100000≈100000 × 17≈170 million times, which is less than 0.0017 seconds. Is not it amazing, the same problem using different algorithms actually have such a large time gap, this is the charm algorithm!

We look back at the time complexity of this chapter three kinds of sorting algorithms. Bucket sort is the fastest, its time complexity is O (N + M); bubble sort is O (N2); quicksort is O (NlogN).

A Monday algorithm [small] hum books
http://bbs.ahalei.com/thread-4443-1-1.html
(Source: Aha Lei _ programmed start from here)


http://wiki.jikexueyuan.com/project/easy-learn-algorithm/xiaohen-buy-book.html

Guess you like

Origin www.cnblogs.com/bqwzx/p/11029694.html