Introduction to Algorithms 2: several exercises 2016.1.2

Source: Internet
Author: User

First, in the merge sort the decimal group to use the insertion sort (put in the previous article);

Second, bubble sort

Bubble sort efficiency is almost the lowest in all sorts, but it is popular because it is also the lowest complexity. Most of the time, the efficiency is less than the insertion sort, actually bubble sort, insert sort, select sort basically effect is similar (this effect is not function). Functionally speaking certainly almost ah are sort), but the process is slightly different. Now that you have written it here, let me summarize the three of them.

1. Insert sort--the process of touching poker

Assuming that the previous one is ordered, the second is inserted into the position where it should be, then the first two are ordered, and the third one is inserted into the position where it should be, then the first three are ordered ... Until the first n is ordered.

Time is mainly spent on inserting and moving. Complexity of n^2.

2. Bubble sort--tall and self-queued

First to the last person said: "If you are ahead of the people taller than you, you will change with him." "(now the second-to-last person is the shortest of the last two people.)

Then say to the second-to-last person: "If you are taller than you in front of you, you will change with him." (now the third person is the shortest of the last three people.)

Then say the same thing to the third person ... A round down, the first person is the shortest one (in fact, and choose a sort of a bit like, but the choice of the way is not the same, and the process of the platoon has a tendency to order the queue)

Perform the same process for post n-1 individuals ... Until the same process is performed on the last 2 people.

Time is mainly spent on exchange. Complexity of n^2.

3. Select Sort--the teacher gives a queue

First of all, the teacher choose one of the shortest, with the first person to exchange positions. Then choose the shortest one from the n-1 and swap the position with the second person ... Until you choose the shortest one from the latter, put it at the end.

Time is mostly spent on choosing the most value. Complexity of n^2. (There will be a heap ordering, which optimizes the selection process, thus achieving the complexity of the NLGN)

As you can see, these three types are the correct sorting algorithms. The following is the bubble sort code:

#include <stdio.h>voidBubblesort (int*a,intLintR) {    inti,j;  for(i=l+1; i<=r;i++) {         for(j=r;j>=i;j--) {            if(a[j]<a[j-1]) {                inttemp=A[j]; A[J]=a[j-1]; A[j-1]=temp; }        }    }}intMain () {intN; scanf ("%d",&N); inta[ One]={}; inti;  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); } bubblesort (A,1, N);  for(i=1; i<=n;i++) {printf ("%d |", A[i]); }    return 0;}

Iii. correctness of the rules of Horner
This is a method to find the value of the polynomial function, reading the code is simply kneeling, unexpectedly so concise to complete the function!

The Horner rule is to use the least multiplication strategy, to find the polynomial a (x) = Anxn+ an-1xn-1+...+ a1x + a0 at the value of x0, the rule is a (x0) = (...) ((Anx0+ an-1) x0+...+ A1) x0+ a0)

Here's the code for the implementation:

#include <stdio.h>DoubleHornerDouble*a,intNDoublex) {    Doubley=0; inti;  for(i=n;i>=0; i--) {y=a[i]+x*y; }    returny;}intMain () {intN; inti; Doublex; scanf ("%d",&N); Doublea[ One]={};  for(i=0; i<=n;i++) {scanf ("%LF",&A[i]); } printf ("A (x) ="); printf ("%.2LF", a[0]);  for(i=1; i<=n;i++) {printf ("+%.2lfx^%d", A[i],i); } printf ("\ n");  while(1) {scanf ("%LF",&x); printf ("A (%.2LF) =%.2lf\n", X,horner (a,n,x)); }    return 0;}


Four, reverse order to the problem

First, the definition of an inverse pair : an array of n mutually dissimilar elements A, the number of a[i]>a[j when satisfying the i<j] condition.

Input:n (number of elements), a array output: number of reverse pairs

It is easy to think of the n^2 algorithm that is compared on a one-by-one basis, but the NLGN algorithm is guided by the introduction of algorithms. (Modify merge Sort)

First of all, if an array has already known, the first half of the number of reversed pairs of K1, the second half of the number of reverse pairs of K2, and both parts have been from small to large order. Then in the process of the merge can by the way between the two parts of the reverse order of the number K (because the first half of the larger than the second part of the number must be a reverse pair). Then the total number of reverse pairs is k1+k2+k.

So, how to find the first half of the number of reverse order? In the same way, the first half, the second part, and the two parts are found. Return and. So is a recursive problem, only need to modify the merge sort.

Here's the code:

#include <stdio.h>intMergenixu (int*a,int*b,intLintMidintR) {    intk=0; intI=l,j=mid+1; intcou=l;  while(I<=mid && j<=r) {if(a[i]<=A[j]) {B[cou]=A[i]; I++; Cou++; }        Else{B[cou]=A[j]; K+=mid-i+1; J++; Cou++; }    }     while(i<=mid) {B[cou]=A[i]; I++; Cou++; }     while(j<=R) {B[cou]=A[j]; J++; Cou++; }     for(i=l;i<=r;i++) {A[i]=B[i]; }    returnK;}intMergesortnixu (int*a,int*b,intLintR) {    if(r-l<1)return 0; intMid= (L+R)/2; intk1=Mergesortnixu (A,b,l,mid); intK2=mergesortnixu (a,b,mid+1, R); intk=Mergenixu (A,B,L,MID,R); return(k1+k2+k);}intMain () {intN; inta[ One]={},b[ One]={}; scanf ("%d",&N); inti;  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); }    intK=mergesortnixu (A, B,1, N);  for(i=1; i<=n;i++) {printf ("%d |", A[i]); } printf ("\n%d", K); return 0;}

Introduction to Algorithms 2: several exercises 2016.1.2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.