Horje
Hash Sort in C++ Code Example
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;
}




Cpp

Related
c++ enum Code Example c++ enum Code Example
parking charge system project c++ Code Example parking charge system project c++ Code Example
c++ code Code Example c++ code Code Example
C++ Join thread Code Example C++ Join thread Code Example
get start with c++ Code Example get start with c++ Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
12