Hash function for sorting 100 or more integar numbers [closed]

A HashTable is absolutely useless in this scenario.
A hash specifically removes ordering information, making it impossible to sort!

Assume that A < B.
When you have hash(A) and hash(B), you cannot make ANY statement about the relationship.

Any of the following are possibly true:

  • hash(A) < hash(B)
  • hash(A) == hash(B)
  • hash(A) > hash(B)

So you cannot use a hash function to sort.

HashTables are not for sorting!

Use Function QSort.

int LessThan(const void* pA, const void* pB) { return *(int*)pA - *(int*)pB; }

int main()
{
    int num[100];
    // Fill in the array

    qsort(num, 100, sizeof(int), LessThan);

    // Your array is now sorted.
    return 0;
}

Leave a Comment