Compare two Version numbers

A version number is a string which is used to identify unique states of a software product. A version number looks like a.b.c.d where a, b etc are number, so version number is a string in which numbers are separated by dots. These number generally represent hierarchy from major to minor (a is major and d is minor).
In this problem we are given two version numbers, we need to compare them and conclude which one is lates version number (that is which version number is smaller).

For example if
V1 = “1.0.31”
V2 = “1.0.27”
Then V2 version is latest (or smaller) 
because V2 < V1

It is not possible to compare them directly because of dot but we can compare them numeric part wise and then we can check which version is latest. In below code such a method is implemented which traverse through strings, separates numeric part and compare them, if equal go for next numeric part and so on until they differ otherwise flag them as equal.
In below code, a method is implemented to compare two versions, If we have more than two version then below versionCompare method can be used as cmp method of sort method, which will sort all versions according to specified comparison.

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

//  C/C++ program to compare two version number
#include <bits/stdc++.h>
using namespace std;
  
//  Method to compare two versions. Returns 1 if v2 is
// smaller, -1 if v1 is smaller, 0 if equal
int versionCompare(string v1, string v2)
{
    //  vnum stores each numeric part of version
    int vnum1 = 0, vnum2 = 0;
  
    //  loop untill both string are processed
    for (int i=0,j=0; (i<v1.length() || j<v2.length()); )
    {
        //  storing numeric part of version 1 in vnum1
        while (i < v1.length() && v1[i] != '.')
        {
            vnum1 = vnum1 * 10 + (v1[i] - '0');
            i++;
        }
  
        //  storing numeric part of version 2 in vnum2
        while (j < v2.length() && v2[j] != '.')
        {
            vnum2 = vnum2 * 10 + (v2[j] - '0');
            j++;
        }
  
        if (vnum1 > vnum2)
            return 1;
        if (vnum2 > vnum1)
            return -1;
  
        //  if equal, reset variables and go for next numeric
        // part
        vnum1 = vnum2 = 0;
        i++;
        j++;
    }
    return 0;
}
  
//  Driver method to check above comparison function
int main()
{
    string version1 = "1.0.3";
    string version2 = "1.0.7";
  
    if (versionCompare(version1, version2) < 0)
        cout << version1 << " is smaller\n";
    else if (versionCompare(version1, version2) > 0)
        cout << version2 << " is smaller\n";
    else
        cout << "Both version are equal\n";
    return 0;
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to compare two version number
  
# Method to compare two versions.
# Return 1 if v2 is smaller,
# -1 if v1 is smaller,,
# 0 if equal
def versionCompare(v1, v2):
      
    # This will split both the versions by '.'
    arr1 = v1.split(".")
    arr2 = v2.split(".")
  
    # Initializer for the version arrays
    i = 0 
      
    # We have taken into consideration that both the
    # versions will contains equal number of delimiters
    while(i < len(arr1)):
          
        # Version 2 is greater than version 1
        if int(arr2[i]) > int(arr1[i]):
            return -1
          
        # Version 1 is greater than version 2
        if int(arr1[i]) > int(arr2[i]):
            return 1
  
        # We can't conclude till now
        i += 1
          
    # Both the versions are equal
    return 0
  
# Driver program to check above comparison function
version1 = "1.0.3"
version2 = "1.0.7"
  
ans =  versionCompare(version1, version2)
if ans < 0:
    print version1 + " is smaller"
elif ans > 0:
    print version2 + " is smaller"
else:
    print "Both versions are equal"
  
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

chevron_right



Output:

1.0.3 is smaller

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.