-
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?
-
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.
-
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.