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