Test interview series --- sort algorithm album --- insert sort directly --- correct incorrect answers, insert directly ---
Interview question 24: code implementation directly insert sort original answer:
# Include <iostream. h> void main (void) {int ARRAY [10] = {0, 6, 3, 2, 7, 5, 4, 9, 1, 8}; int I, j; for (I = 0; I <10; I ++) {cout <ARRAY [I] <";}cout <endl; for (I = 2; I <= 10; I ++) // convert ARRAY [2],…, ARRAY [n] inserts {if (ARRAY [I] <ARRAY [I-1]) sequentially. // if ARRAY [I] is greater than all ordered values, ARRAY [I] will remain unchanged in the original position {ARRAY [0] = ARRAY [I]; // The ARRAY [0] will be treated as a sentry, is the copy of ARRAY [I] j = I-1; do {// from right to left in the ordered area ARRAY [1 .. find the insert position of ARRAY [I] in I-1] ARRAY [j + 1] = ARRAY [j]; // move the value after ARRAY [I] record j --;} while (ARRAY [0] <ARRAY [j]); ARRAY [j + 1] = ARRAY [0]; // insert ARRAY [I] to the correct position} for (I = 0; I <10; I ++) {cout <ARRAY [I] <";}cout <endl ;}
Train of Thought Analysis: there is no error in the entire train of thought. Error Analysis: for (I = 2; I <= 10; I ++) // Error 1: The default I = 0 and I = 1 are sorted; error 2: I = 10 obviously exceeds the size of array allocation and stack overflow error
ARRAY [0] // The number of the first digit in the sequence is always changed and cannot be sorted correctly.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>
Corrected answer:
# Include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int ARRAY [10] = {6, 9, 3, 7, 2, 5, 4, 0, 1, 8}; int I, j, temp; // output the sequence before sorting (I = 0; I <10; I ++) {cout <ARRAY [I] <"" ;}cout <endl; for (I = 1; I <10; I ++) // convert ARRAY [2],..., ARRAY [n] inserts {if (ARRAY [I] <ARRAY [I-1]) sequentially. // if ARRAY [I] is greater than all ordered values, // ARRAY [I] will remain unchanged in the original position {temp = ARRAY [I]; // use temp as the sentry, which is a copy of ARRAY [I] j = I-1; do {// from right to left in the ordered area ARRAY [1 .. in I-1]; find the insert position of ARRAY [I] ARRAY [j + 1] = ARRAY [j]; // move the value greater than ARRAY [I] record after j --;} while (temp <ARRAY [j]); ARRAY [j + 1] = temp; // ARRAY [I] inserted to the correct position} // output the sorted sequence for (I = 0; I <10; I ++) {cout <ARRAY [I] <"" ;}cout <endl; return 0 ;}
Correction analysis: Revision 1: Talk about temp as the sentry; Amendment 2: The for loop ends from 9 Beginning with I = 1. Output after correction:
Which of the following sorting algorithms is unstable? () A: Fast sorting; B: Merge Sorting; C: Bubble Sorting; D: insert sorting directly.
Select.
Reference: baike.baidu.com/view/547325.htm? FromTaglist
Question 9: directly inserting a sorting algorithm is an unstable Sorting Algorithm ()
No.
Ps: In the best case of direct sorting (the columns to be sorted are sorted by key code), only one comparison is required for each sort without moving elements. Therefore, the number of comparisons between n elements is n-1, and the number of moves is 0.
In the worst case (Reverse Order), the I-th element must be compared with the previous one, and the number of moves I + 1, so the total number of comparisons is relatively large, it will not be written.
Summary: it is a stable sorting method, with time complexity O (n ^ 2). There is only one auxiliary space in the sorting process, so space complexity
Please accept it. The task is required. Thank you!
Reference: zhidao.baidu.com/question/17279425.html