Map sort

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <vector>
#include <map>
#include <climits>
using namespace std;

vector<int> map_sort(vector<int> &nums)
{
    vector<int> temp;
    std::map<int,bool> mp;
    int maxElement = INT_MIN;
    int minElement = INT_MAX;
    for(int i=0; i<nums.size(); i++)
    {
        if(maxElement < nums[i])
        {
            maxElement = nums[i];
        }
        if(minElement > nums[i])
        {
            minElement = nums[i];
        }
        mp[nums[i]] = true;
        
    }
    
    for(int i=minElement; i<= maxElement; i++ )
    {
        if(mp[i])
        {
            temp.push_back(i);
            cout << i <<" ";
        }
    }
    return temp;
    
}

int main()
{
    vector<int> nums{77,55,66,44,11,89 };
    vector<int> temp =  map_sort(nums);
    return 0;

Comments

Popular posts from this blog

VECPP.c

Comparision with Quick Sort

Interview questions