Insertion sort

This is a discussion on Insertion sort within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <vector> using namespace std; template <typename T> void insertionSort(vector<T> &v1); int main() { const int SIZE ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Insertion sort

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    template <typename T>
    void insertionSort(vector<T> &v1);
    int main()
    {    
        const int SIZE = 5;
        int myArray[SIZE] = {5,3,2,4,1};
        vector<int> vect(myArray, myArray+5);
        insertionSort(vect);
    
        for(int i=0; i<SIZE; i++)
        {
            cout<< vect[i] << " ";
        }
        cout<<endl;
    
        return 0;
    }
    
    void insertionSort(vector<int> &v1)
    {
        int i;
        int j;
        int temp;
        
        for(i = 1; i < v1.size(); i++)
        {
            j=i;
            while(j>0 && v1[j-1] > v1[j])
            {
                temp = v1[j-1];
                v1[j-1] = v1[j];
                v1[j] = temp;
                j--;
            }
        }
    }
    The code is runs properly, but for some reason it's getting an error that says "insertion sort\test.cpp(29): warning C4018: '<' : signed/unsigned mismatch" What does that mean?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    vector::size does not return an int. You should give your loop counter type std::size_t which would be what the function returns. This will eliminate your warnings.
    The warnings comes from that std::size_t is an unsigned type (cannot contain negative numbers) while "i" is a signed type (can contain negative numbers). When comparing a signed and an unsigned type of the same size, you get this warning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    In the c++ program language, when you compare the sign int with the unsigned int ,the compiler will convert the "sign int " to "unsigned int". so the negative integer will be converted to nonegative, this is very dangeous. the warning is this mean.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion sort
    By Fatima Rizwan in forum C++ Programming
    Replies: 5
    Last Post: 02-26-2010, 01:24 PM
  2. Replies: 1
    Last Post: 01-26-2010, 08:02 AM
  3. Insertion Sort Help
    By Odinwar in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2009, 10:27 AM
  4. Insertion sort... Please help!
    By xMEGANx in forum C++ Programming
    Replies: 11
    Last Post: 11-30-2007, 02:30 AM
  5. Insertion Sort Help
    By vgame64 in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2006, 07:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21