c语言用邻接表实现基数排序

本人小白,大佬勿喷,发表一些日常学习的记录,简单来说基数排序是基于桶排序的多关键字排序。这里的关键字指的是数据每位上数字的大小。下面直接展示代码

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct list {
	int data;
	list* next;
}list;
list* creatlist()//创建单向链表,且头指针不含有数据
{
	list* pl;
	pl = (list*)malloc(sizeof(list));
	if (!pl) exit(1);
	pl->next = NULL;
	return pl;
}
void tailinsert(list* head , int data)//尾插法,其实这里用头插法效率要高一点,但是尾插法好理解
{
	list* ph, * temp;
	temp = head;
	ph = (list*)malloc(sizeof(list));
	if (!ph) exit(1);
	ph->next = NULL;
	ph->data = data;
	while (temp->next != NULL)
	{
		temp = temp->next;
	}
	temp->next = ph;
}
int select_max(int a[])//找到数组中最大的一个数
{
	int max, j;
	max = a[0];
	for (j = 1; j < 10; j++)
	{
		if (a[j] > max)
		{
			max = a[j];
		}
	}
	return max;
}
void clear_list(list *lp)//清空链表上的数据
{
	list* mp, * hp;
	hp = lp->next;
	while (hp != NULL)
	{
		mp = hp->next;
		free(hp);
		hp = mp;
	}
	lp->next = NULL;
}
void radix_sort(int a[])//基数排序函数
{
	int it = 0, temp, kp;
	list* litmp[10] = {};//创建一个结构体指针数组
	for (int k = 0; k < 10; k++)
	{
		litmp[k] = creatlist();
	}//创建邻接表
	kp = select_max(a);
	while ((int)pow(10, it) <= kp)
	{
		list* lp;
		lp = litmp[0];
		int i = 0;
		for (i; i < 10; i++)
		{
			temp = a[i] / (int)pow(10, it) % 10;
			tailinsert(litmp[temp], a[i]);
		}//让数组数据入桶
		for (int m = 0,h = 0; m < 10;)
		{
			if (lp->next != NULL)
			{
				a[h++] = lp->next->data;
				lp =  lp->next;
			}
			else
			{
				m++;
				lp = litmp[m];
			}
		}//让数组数据出桶
		for (int f = 0; f < 10; f++)
		{
			clear_list(litmp[f]);
		}//清桶,为下一次入桶做准备
		it++;
	}
}
int main()
{
	int a[] = { 2,3,42,12,154,123,2,4,2,10 };
	radix_sort(a);
	print(a);
}

基数排序的时间复杂度取决于数组中最大元素的位数,在最大元素比较小的时候,效率高于快速排序。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
邻接表是一种图的存储结构,可以用来表示稀疏图。下面是一个用C语言实现邻接表存储的示例代码: ```c #include <stdio.h> #include <stdlib.h> struct AdjListNode { int dest; struct AdjListNode* next; }; struct AdjList { struct AdjListNode* head; }; struct Graph { int V; struct AdjList* array; }; struct AdjListNode* newAdjListNode(int dest) { struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } struct Graph* createGraph(int V) { struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); graph->V = V; graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList)); int i; for (i = 0; i < V; ++i) { graph->array[i].head = NULL; } return graph; } void addEdge(struct Graph* graph, int src, int dest) { struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } void printGraph(struct Graph* graph) { int v; for (v = 0; v < graph->V; ++v) { struct AdjListNode* pCrawl = graph->array[v].head; printf("\n Adjacency list of vertex %d\n head ", v); while (pCrawl) { printf("-> %d", pCrawl->dest); pCrawl = pCrawl->next; } printf("\n"); } } int main() { int V = 5; struct Graph* graph = createGraph(V); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); printGraph(graph); return 0; } ``` 这个示例代码创建了一个包含5个顶点的无向图,然后使用邻接表存储这个图,并最终输出它的邻接表。在这个示例代码中,我们首先定义了三种结构体:`AdjListNode`、`AdjList`和`Graph`。其中,`AdjListNode`表示邻接表中的一个节点,包括目标节点的编号和指向下一个节点的指针;`AdjList`表示一个节点的邻接表,包括指向第一个节点的指针;`Graph`表示整个图,包括顶点数和一个指向邻接表数组的指针。 接下来,我们定义了一些函数来操作这些结构体,包括`newAdjListNode`用于创建一个新的邻接表节点,`createGraph`用于创建一个新的图,`addEdge`用于向图中添加一条边,`printGraph`用于输出图的邻接表。 在`main`函数中,我们首先创建了一个包含5个顶点的图,然后添加了7条边,最后输出了这个图的邻接表。 以上就是一个简单的使用C语言实现邻接表存储的示例代码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值