sorting in C++ Vs sorting in JAVA || Speed Analysis
Can anyone please explain the time difference in Sort function in C++ and Java in this problem?\n\nI have implemented the same concept in Java and C++.\nIn Java
Can anyone please explain the time difference in Sort function in C++ and Java in this problem?\n\nI have implemented the same concept in Java and C++.\nIn Java, the soln is Accepted.\nBut in C++, it is showing TLE.\n\n\n**JAVA: Accepted**\n```\n\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        int diff = 0, min = Integer.MAX_VALUE, min_id = 0;\n        Arrays.sort(rooms, (a,b) -> Integer.compare(a[1],b[1]));\n        int[] res = new int[queries.length];\n        for(int i=0;i<queries.length;i++){\n            for(int j=rooms.length-1;j>=0;j--){\n                if(rooms[j][1]>=queries[i][1]){\n                    diff = Math.abs(rooms[j][0]-queries[i][0]);\n                    if(diff<min){\n                        min = diff;\n                        min_id = rooms[j][0];\n                    }\n                    else if(diff==min){\n                        min_id = (min_id < rooms[j][0]) ? min_id : rooms[j][0];\n                    }\n                }\n                else break;\n            }\n            if(min == Integer.MAX_VALUE) min_id = -1;\n            res[i] = min_id;\n            min_id = 0;\n            min = Integer.MAX_VALUE;\n        }\n        return res;\n    }\n}\n```\n**C++: Time Limit Exceeded**\n```\nclass Solution {\npublic:\n    static bool cmp(const vector<int>&a,const vector<int>&b){\n        return a[1]>b[1]; \n    }\n    vector<int> closestRoom(vector<vector<int>>& arr, vector<vector<int>>& q) {\n    int diff = 0, min = INT_MAX, min_id = 0;\n    sort(arr.begin(),arr.end(),cmp);\n    int n=arr.size(),m=q.size();\n    \n    vector<int>ans(m);\n        \n    for(int j=0;j<m;j++){\n        for(int i=0;i<n;i++){\n            if(q[j][1]<=arr[i][1]){\n                diff = abs(arr[i][0]-q[j][0]);\n                    if(diff<min){\n                        min = diff;\n                        min_id = arr[i][0];\n                    }\n                    else if(diff==min){\n                        min_id = (min_id < arr[i][0]) ? min_id : arr[i][0];\n                    }\n            }\n            else\n                break;\n            \n        }\n        if(min == INT_MAX) min_id = -1;\n            ans[j] = min_id;\n            min_id = 0;\n            min =INT_MAX;\n    }\n    return ans;\n    \n    }\n\n    \n};\n```
['Sorting', 'C++']