Written interview series---sorting algorithm album---direct insertion sort---incorrect answer correct

Source: Internet
Author: User

Interview 24: Encode direct Insert sort the 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 <=; i++)        //array[2],..., Array[n] sequentially insert {if (Array[i] < array[i-1])    //assuming AR Ray[i] greater than all ordered values, Array[i] will remain in place ({array[0] = Array[i];    Array[0] As a sentinel, a copy of array[i] j = i-1;do{            //right-to-left in the ordered area array[1. I-1] [Find array[i] in the insertion position array[j+1] = array[j];    Move the value greater than array[i] record back j--;} while (Array[0] < array[j]); ARRAY[J+1]=ARRAY[0];    
Thought analysis: The whole idea no matter what mistake.

Error analysis such as the following: for (i = 2; I <=; i++)  //error One: the equivalent of the default i=0 and I=1 is already sequenced; Error two: i=10 significantly exceeds the size of the array allocation. Stack Overflow error
array[0]//selected as Sentry causes the first number in the sequence to always change and cannot be sorted correctly
>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>> Fix the 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 sequence before sorting for (i = 0; i <; i++) {cout<<array[i]<< "";} Cout<<endl;for (i = 1; i <; i++)        //array[2],..., Array[n] sequentially insert {if (Array[i] < array[i-1])    //assuming ARR Ay[i] is greater than all ordered values,//array[i] will remain in place {temp = Array[i];    Make Temp Sentinel, is array[i] Copy j = i-1;do{                     //right-to-left in ordered area array[1. I-1] [find array[i] insertion position array[j+1] = array[j];    Move the value greater than array[i] record back j--;} while (temp < array[j]); Array[j+1]=temp;    Array[i] Insert to the correct position}}//output sorted sequence for (i = 0; i <; i++) {cout<<array[i]<< "";} Cout<<endl;return 0;}
Correction Analysis: Fix one: Speak temp as Sentinel. Fix two: The For loop ends at I=1 start 9.

Post-correction output:

Written interview series---sorting algorithm album---direct insertion sort---incorrect answer correct

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.