#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct HashNode
{
int data;
HashNode *next;
} *HashList;//散列链表的每个结点的数据结构
inline void swap(int &i,int &j)
{
int temp=i;
i=j;
j=temp;
}
template <int n,int k>
void HashSort(int (&A)[n],HashList (&Bin)[k], int m)
{
int i, j;
HashNode *p, *q;
for (j = 0; j < k; ++j)
{
Bin[j]=NULL;
}
for (i = 0; i < n; ++i)
{
p = new HashNode;
p->data = A[i];
p->next=NULL;
j = A[i] / m;
if( Bin[j]== NULL) Bin[j] = p;
else
{
q = Bin[j];
while (q->next != NULL)
q = q->next;
q->next = p;
}
}
for (int j = 0; j < k; j++)
{
for (p = Bin[j]; p!=NULL; p = p->next)
for (q = p->next; q != NULL; q = q->next)
{
if (p->data > q->data)
swap(p->data, q->data);
}
}
for (int j=0;j<k;j++)
{
for (p = Bin[j]; p!=NULL; p = p->next)
{
cout<<p->data<<" ";
}
cout<<endl;
}
}
int main ()
{
int a[]={274,384,123,77,201,15,375,145,101,64,341,235,38,178,137,264,350,212,184,26};
HashList Bin[4];
HashSort(a,Bin,100);
return 0;
}