111
This commit is contained in:
@@ -1,20 +1,89 @@
|
||||
// CPP5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
|
||||
//
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<time.h>
|
||||
|
||||
#include <iostream>
|
||||
typedef struct Node
|
||||
{
|
||||
int data;
|
||||
Node* next;
|
||||
} Node;
|
||||
|
||||
// 链式基数排序
|
||||
Node* radixSort(Node* head)
|
||||
{
|
||||
if (head == NULL) return NULL;
|
||||
int max = head->data;
|
||||
Node* current = head;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current->data > max) max = current->data;
|
||||
current = current->next;
|
||||
}
|
||||
// 基数排序
|
||||
for (int exp = 1; max / exp > 0; exp *= 10)
|
||||
{
|
||||
Node* buckets[10] = { NULL };
|
||||
Node* tails[10] = { NULL };
|
||||
current = head;
|
||||
while (current != NULL)
|
||||
{
|
||||
Node* next = current->next; // 保存下一个节点
|
||||
current->next = NULL; // 断开,防止形成环或错连
|
||||
|
||||
int digit = (current->data / exp) % 10;
|
||||
if (buckets[digit] == NULL)
|
||||
{
|
||||
buckets[digit] = current;
|
||||
tails[digit] = current;
|
||||
}
|
||||
else
|
||||
{
|
||||
tails[digit]->next = current;
|
||||
tails[digit] = current;
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
Node* newHead = NULL;
|
||||
Node* newTail = NULL;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if (buckets[i] != NULL)
|
||||
{
|
||||
if (newHead == NULL)
|
||||
{
|
||||
newHead = buckets[i];
|
||||
newTail = tails[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
newTail->next = buckets[i];
|
||||
newTail = tails[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newTail != NULL)
|
||||
newTail->next = NULL;
|
||||
head = newHead;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Hello World!\n";
|
||||
}
|
||||
|
||||
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
|
||||
// 调试程序: F5 或调试 >“开始调试”菜单
|
||||
|
||||
// 入门使用技巧:
|
||||
// 1. 使用解决方案资源管理器窗口添加/管理文件
|
||||
// 2. 使用团队资源管理器窗口连接到源代码管理
|
||||
// 3. 使用输出窗口查看生成输出和其他消息
|
||||
// 4. 使用错误列表窗口查看错误
|
||||
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
|
||||
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
|
||||
srand((unsigned int)time(NULL));
|
||||
Node* head = NULL;
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||
newNode->data = rand() % 100;
|
||||
newNode->next = head;
|
||||
head = newNode;
|
||||
}
|
||||
printf("链表已创建!\n");
|
||||
double clock1 = clock();
|
||||
head = radixSort(head);
|
||||
double clock2 = clock();
|
||||
printf("排序完成:\n");
|
||||
printf("排序时间: %f 秒\n", (double)(clock2 - clock1) / CLOCKS_PER_SEC);
|
||||
return 0;
|
||||
}
|
||||
89
cs4/CPP5/作业5.cpp
Normal file
89
cs4/CPP5/作业5.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<time.h>
|
||||
|
||||
typedef struct Node
|
||||
{
|
||||
int data;
|
||||
Node* next;
|
||||
} Node;
|
||||
|
||||
// 链式基数排序
|
||||
Node* radixSort(Node* head)
|
||||
{
|
||||
if (head == NULL) return NULL;
|
||||
int max = head->data;
|
||||
Node* current = head;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current->data > max) max = current->data;
|
||||
current = current->next;
|
||||
}
|
||||
// 基数排序
|
||||
for (int exp = 1; max / exp > 0; exp *= 10)
|
||||
{
|
||||
Node* buckets[10] = { NULL };
|
||||
Node* tails[10] = { NULL };
|
||||
current = head;
|
||||
while (current != NULL)
|
||||
{
|
||||
Node* next = current->next; // 保存下一个节点
|
||||
current->next = NULL; // 断开,防止形成环或错连
|
||||
|
||||
int digit = (current->data / exp) % 10;
|
||||
if (buckets[digit] == NULL)
|
||||
{
|
||||
buckets[digit] = current;
|
||||
tails[digit] = current;
|
||||
}
|
||||
else
|
||||
{
|
||||
tails[digit]->next = current;
|
||||
tails[digit] = current;
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
Node* newHead = NULL;
|
||||
Node* newTail = NULL;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if (buckets[i] != NULL)
|
||||
{
|
||||
if (newHead == NULL)
|
||||
{
|
||||
newHead = buckets[i];
|
||||
newTail = tails[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
newTail->next = buckets[i];
|
||||
newTail = tails[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newTail != NULL)
|
||||
newTail->next = NULL;
|
||||
head = newHead;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
srand((unsigned int)time(NULL));
|
||||
Node* head = NULL;
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||
newNode->data = rand() % 100;
|
||||
newNode->next = head;
|
||||
head = newNode;
|
||||
}
|
||||
printf("链表已创建!\n");
|
||||
double clock1 = clock();
|
||||
head = radixSort(head);
|
||||
double clock2 = clock();
|
||||
printf("排序完成:\n");
|
||||
printf("排序时间: %f 秒\n", (double)(clock2 - clock1) / CLOCKS_PER_SEC);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user