Direct Insert Sort comparison, number of moves and number of assignments min/MAX/avg

Source: Internet
Author: User

Direct Insert Sort comparison, number of moves and number of assignments min /MAX /avg

Based on the program:

Number of comparisons: refers to the number of times a while loop moves

Number of moves: refers to the number of times the data is moved in a vector (one-dimensional array ), including R[0]<-r[i],r[j+1]<-r[0], and r[j+1]<-r[j in the while Loop ]

Number of assignments: include all assignment statements

About the relationship between the number of comparisons and the number of assignments:

For a while loop, the program compares T-times to exit the loop, moves t-1 times, assigns a value 2t-2 times, because the front t-1 a while loop executes, and the first t while Loop does not execute.

For A For loop, the program moves 2 extra times each time, assigning 3 times.

So each for loop compares t times, moves t+1[(t-1) +2] times, assigns a value 2t+1 times [(2t-2) +3].

I. Average number of comparisons and number of assignments

For placing the number of I (i=2,3,..., n) in the corresponding position,

The program exits the while loop at the J position ,A[i] into the a[j+1] place,

the probability of J equals i-1 ~ 0 is the same (the probability of all probabilities is equal for the position of a number in a sequence )

So

Ii. minimum number of comparisons and number of assignments

The minimum value is the First Direct exit of the while loop, with a comparison of 1 occurrences.

Iii. maximum number of comparisons and assignments

The maximum value is the case when j=0 exits while loop, and the number of comparisons is i times.

Summarize:

Number of comparisons:

Number of moves:

Number of assignments:

Then I do not know my understanding of the concept and calculation of understanding is right ...

Anyway, I think some of the ppt is not right, I can be rude to say, this is a little pit dad ah ...

Direct Insert Sort comparison, number of moves and number of assignments min/MAX/avg

Related Article

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.