Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Hash Sort in C++

#include<iostream>
#include<unordered_map>
#include<algorithm>
using namespace std;
void sortUsingHash(int arr[], int n)
{
    int max = *std::max_element(arr, arr + n);
    int min = abs(*std::min_element(arr, arr + n));
    int positiveNum[max + 1] = { 0 };
    int negativeNum[min + 1] = { 0 };
    for (int i = 0; i < n; i++)
    {
        if (arr[i] >= 0)
            positiveNum[arr[i]] += 1;
        else
            negativeNum[abs(arr[i])] += 1;
    }
    for (int i = min; i > 0; i--)
    {
        if (negativeNum[i])
        {
            for (int j = 0; j < negativeNum[i]; j++)
            {
                cout << (-1) * i << " ";
            }
        }
    }
    for (int i = 0; i <= max; i++)
    {
        if (positiveNum[i])
        {
            for (int j = 0; j < positiveNum[i]; j++)
            {
                cout << i << " ";
            }
        }
    }
}
int main()
{
    int a[] = {7, 5, -4, -3, 2, 4, 1, -2, -1, 0, 6, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    sortUsingHash(a, n);
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: number of characters in string 
Cpp :: how to run the code 
Cpp :: what is blob in computer vision 
Cpp :: cin une énumération 
Cpp :: delete[] cpp 
Cpp :: C++ Converting Celsius to Kelvin 
Cpp :: transform c++ 
Cpp :: rgb(100,100,100,0.5) validation c++ 
Cpp :: how to compile with libstdc++ fedora 
Cpp :: how to calculate 2^7 in cpp code 
Cpp :: ubuntu dotnet create blazorserver linux 
Cpp :: how to find the sum of elements in a stack in cpp 
Cpp :: cannot access base class members 
Cpp :: c++ comments 
Cpp :: c++ qt qtreewidget lock first column 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: char to binary 
Cpp :: erase in c++ 
Cpp :: Marin and Photoshoot codeforces solution in c++ 
Cpp :: ex: java script 
Cpp :: std::random_device 
Cpp :: how to make a running text in c++ 
Cpp :: left recursion program in c++ 
Cpp :: this is my p phone number in punjabi 
Cpp :: calculate number of edges of graph in data structure c++ 
Cpp :: is there interfaces in c++ like 
Cpp :: Marin and Anti-coprime Permutation codeforces solution in c++ 
Cpp :: beecrowd problem 1001 solution 
Cpp :: c++ cout int 
Cpp :: Basic stack implementation in c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =