简体   繁体   中英

Insertion Sort sorts array halfway

I am reading a book of DS and Algortihms. I saw an algorithm named insertion sort and then tried to apply that in c++. When I sort the array, the first element of the array remains at the place it was previously while the other elements go to their appropriate place! I can't understand what's happeing. My code :

#include<iostream>
#include<vector>

using namespace std;

void vec_in(vector<unsigned>& vec, unsigned size);
void vec_out(vector<unsigned> vec);

int main(){
    vector<unsigned> vec;
    unsigned size;
    cout<<"SIZE : ";
    cin>>size;
    cout<<"ARRAY : ";
    vec_in(vec,size);
    cout<<endl;
    for(unsigned j=1; j<size; j++){
        unsigned key = vec[j];
        unsigned i=j-1;
        cout<<key<<endl;
        while(i>0&&vec[i]>key){
            vec[i+1]=vec[i];
            i--;
        }
        vec[i+1]=key;
    }
    cout<<"SORTED ARRAY : ";
    vec_out(vec);

} 

void vec_in(vector<unsigned>& vec, unsigned size){
    for(unsigned i=0; i<size; i++){
        unsigned in;
        cin>>in;
        vec.push_back(in);
    }
    cout<<endl;
}
void vec_out(vector<unsigned> vec){
    for(unsigned i=0; i<vec.size(); i++){ 
        cout<<vec[i]<<" ";
    } 
    cout<<endl;
} 

I tried this code with many samples the result remains same : the first element is never sorted.

You should replace while(i>0&&vec[i]>key){ by while(i>=0&&vec[i]>key){ . In this case i can get negative. Thus, it needs to be singed, ie, this code works.

If you want to stay with your types, you can replace all occurences of i with i-1 and obtain

#include<iostream>
#include<vector>

using namespace std;

void vec_in(vector<unsigned>& vec, unsigned size);
void vec_out(vector<unsigned> vec);

int main(){
    vector<unsigned> vec;
    unsigned size;
    cout<<"SIZE : ";
    cin>>size;
    cout<<"ARRAY : ";
    vec_in(vec,size);
    cout<<endl;
    for(unsigned j=1; j<size; j++){
        unsigned key = vec[j];
        unsigned i=j;
        cout<<key<<endl;
        while(i>0&&vec[i-1]>key){
            vec[i]=vec[i-1];
            i--;
        }
        vec[i]=key;
    }
    cout<<"SORTED ARRAY : ";
    vec_out(vec);

} 

void vec_in(vector<unsigned>& vec, unsigned size){
    for(unsigned i=0; i<size; i++){
        unsigned in;
        cin>>in;
        vec.push_back(in);
    }
    cout<<endl;
}
void vec_out(vector<unsigned> vec){
    for(unsigned i=0; i<vec.size(); i++){ 
        cout<<vec[i]<<" ";
    } 
    cout<<endl;
} 

which works as well .

Note that your approach need N^2 steps (where N is the size of the vector). There are much better approaches, which do it in only log(N) N steps, like quick sort. The standard library implements such an algorithm. Thus, you should always use std::sort for sorting.

Let's assume we have only two elements: 4 and 3 .

Now, what happens when for loop is entered.

    for(unsigned j=1; j<size; j++){
        unsigned key = vec[j];         /* key := 3      */
        unsigned i=j;                  /* i := 1        */
        cout<<key<<endl;               /* print(3)      */
        while(i>0&&vec[i-1]>key){      /* i > 0 (false) */
            vec[i]=vec[i-1];
            i--;
        }
        vec[i]=key;
    }

The while loop that does the sorting is not entered. This will always happen when trying to compare vec[0] to vec[1] .

As a rule of thumb, please do the following tests first time:

1) empty vector
2) vector with one element
3) vector with two elements a) sorted acending b) sorted descending
...

They will allow you to quickly find minor bugs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM