在看第五章的时候,看到无序后,
就想把一个有序数组变成无序数组,
自己想的题目,
一个斐波那契数列(94个),变成随机排序。
有序变无序是我喜欢的熵增原理,本无公平与否,本就无序,就如三体的另一个结局,哈哈~
解题思路;
1初始化数组
2创建随机数组,内容值是上个数组每个元素的优先级
3对随机数组排序。
4拿出排序后的随机数组的下标,
5打印即可
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <time.h>
#define N 94//超过94就会溢出
struct h_r { int priority; int lower_the_bid; };
void init(int* p, int n);
void happen_rand(struct h_r p[], int n);
int quick_sort(struct h_r p[], int left, int right);
int main()
{
unsigned long long array[N];//斐波那契数列
struct h_r array_rand[N];//随机序列(记录优先级和下标)
unsigned long long array_chaotic[N];//混乱的斐波那契数列
init(array,N);//斐波那契初始化
happen_rand(array_rand, N);//生成无序数组
quick_sort(array_rand,0, N-1);//对无序数组进行快速排序,大的在右边,小的在左边,递归下去,最后有序。左边建立基数,哨兵就从右边开始;
for (int i = 0; i < N; i++) {
array_chaotic[i] = array[array_rand[i].lower_the_bid];//拿出结构体排序后的下标
}
for (int i = 0; i < N; i++) {
printf("%lld\n", array_chaotic[i]);
}
return 0;
}
void init(long long *p, int n)
{
*p = 0, * (p + 1) = 1;
for (int i = 2; i <N; i++) {
*(p + i) = *(p + i - 1) + *(p + i - 2);
}
}
void happen_rand(struct h_r p[], int n)
{
srand();
for (int i = 0; i < N; i++) {
p[i].priority = rand()%N;//存放优先级
p[i].lower_the_bid = i;//存放下标
}
}
int quick_sort(struct h_r p[], int left,int right)
{
if (left >= right) return 0 ;
int benchmark = p[left].priority;//
int l_t_b = p[left].lower_the_bid;
int i = left, j = right;//建立哨兵
while (i < j) {
while ((i < j) && (p[j].priority >= benchmark)) { j--; }
p[i] = p[j];
while ((i < j) && (p[i].priority <= benchmark)) { i++; }
p[j] = p[i];
}
p[i].priority = benchmark;
p[i].lower_the_bid = l_t_b;
quick_sort(p,left,i-1 );
quick_sort(p, i+1, right);
}
#include<stdio.h>
#include <time.h>
#define N 94//超过94就会溢出
struct h_r { int priority; int lower_the_bid; };
void init(int* p, int n);
void happen_rand(struct h_r p[], int n);
int quick_sort(struct h_r p[], int left, int right);
int main()
{
unsigned long long array[N];//斐波那契数列
struct h_r array_rand[N];//随机序列(记录优先级和下标)
unsigned long long array_chaotic[N];//混乱的斐波那契数列
init(array,N);//斐波那契初始化
happen_rand(array_rand, N);//生成无序数组
quick_sort(array_rand,0, N-1);//对无序数组进行快速排序,大的在右边,小的在左边,递归下去,最后有序。左边建立基数,哨兵就从右边开始;
for (int i = 0; i < N; i++) {
array_chaotic[i] = array[array_rand[i].lower_the_bid];//拿出结构体排序后的下标
}
for (int i = 0; i < N; i++) {
printf("%lld\n", array_chaotic[i]);
}
return 0;
}
void init(long long *p, int n)
{
*p = 0, * (p + 1) = 1;
for (int i = 2; i <N; i++) {
*(p + i) = *(p + i - 1) + *(p + i - 2);
}
}
void happen_rand(struct h_r p[], int n)
{
srand();
for (int i = 0; i < N; i++) {
p[i].priority = rand()%N;//存放优先级
p[i].lower_the_bid = i;//存放下标
}
}
int quick_sort(struct h_r p[], int left,int right)
{
if (left >= right) return 0 ;
int benchmark = p[left].priority;//
int l_t_b = p[left].lower_the_bid;
int i = left, j = right;//建立哨兵
while (i < j) {
while ((i < j) && (p[j].priority >= benchmark)) { j--; }
p[i] = p[j];
while ((i < j) && (p[i].priority <= benchmark)) { i++; }
p[j] = p[i];
}
p[i].priority = benchmark;
p[i].lower_the_bid = l_t_b;
quick_sort(p,left,i-1 );
quick_sort(p, i+1, right);
}