find out the neighbouring max D_value by counting sort in stack
#include <stdio.h>
#include <malloc.h>
#define MAX_STACK 10 int COUNT = ;
// define the node of stack
typedef struct node {
int data;
node *next;
}*Snode; // define the stack
typedef struct Stack{
Snode top;
Snode bottom;
}*NewStack; void creatStack(NewStack s) {
// allocate stack
// allocate the elements of stack s->top = (Snode) malloc(sizeof(node));
s->bottom = s->top;
s->top->next = NULL;
} bool push(NewStack s,int val) {
//Snode top = s->top;
// Snode bottom = s->bottom;
if(COUNT == MAX_STACK) {
printf("StackOverFlowError\n");
return false;
}
Snode newNode = (Snode) malloc(sizeof(node));
newNode->data = val; // push the new node into the stack
newNode->next = s->top;
s->top = newNode;
COUNT ++;
//printf("%d \n",s->top->data);
return true;
} int pop(NewStack s) {
if(s->top == s->bottom) {
printf("this is bottom of stack!\n");
return NULL;
}
int val = s->top->data;
Snode tempNode = s->top;
s->top = s->top->next;
free(tempNode);
return val;
} void printStack(NewStack s){
Snode priNode = (Snode) malloc (sizeof(node));
priNode = s->top;
printf("now it is the show time of ur stack:\n");
while(priNode != s->bottom) {
printf("%d \t",priNode->data);
priNode = priNode->next;
}
} void scanfStack(NewStack s) {
printf("now u can write down the elements u cant push:\n");
int i = ;
int val;
for(i = ; i<MAX_STACK; ++i) {
scanf("%d",&val);
push(s,val);
}
} bool deleteStack(NewStack s) {
Snode clear = (Snode) malloc (sizeof(node));
while(s->top != s->bottom) {
clear = s->top;
s->top = s->top->next;
free(clear);
} return true;
} int getMax(NewStack s) {
Snode top = s->top;
Snode bottom = s->bottom;
int max = top->data;
while(top != bottom) {
if(top->data > max) {
max = top->data;
}
top = top->next;
} return max;
} int getMin(NewStack s) {
Snode top = s->top;
Snode bottom = s->bottom;
int Min = top->data;
while(top != bottom) {
if(top->data < Min) {
Min = top->data;
}
top = top->next;
} return Min;
} // using the counting sort -- is not appropriate for the neibouring numbers where there are big difference.
int getMaxNeighD_value(NewStack s){
Snode top = s->top;
Snode bottom = s->bottom;
int max = getMax(s);
int min = getMin(s);
int num = max-min+; // get the length of the new counting sort array
int arr[num];
int i = ;
int j = ; // to put the elements into the new counting sort array
for(; j<num; ++j) {
arr[j] = -;
}
while(top != bottom) {
arr[top->data - min] = top->data;
top = top->next;
} // to find out the max zone where there are max number of -1
int Max_count = ;
int count = ;
for(;i < num; ++i) {
//printf("%d:%d\n",i,arr[i]);
if(arr[i] == -) {
count ++;
}else {
if(count > Max_count) {
Max_count = count;
}
count = ;
}
//printf("%d\n",Max_count+1);
} return Max_count+;
} int main() {
NewStack s = (NewStack) malloc(sizeof(Stack));
creatStack(s);
// push(s,5);
// push(s,6);
// push(s,4);
// push(s,7);
// push(s,10);
// push(s,9);
// push(s,3);
// push(s,56);
// push(s,88);
// push(s,44);
// push(s,66);
scanfStack(s);
printStack(s);
//printf("%d \t",pop(s));
int max = getMax(s);
int min = getMin(s);
printf("%d %d\n",max,min);
int c = getMaxNeighD_value(s);
printf("the max neighbouring D_value is : %d \n",c);
//deleteStack(s);
//pop(s);
}
illustration : counting sort is not appropriate for the array where there are too big difference such as {1, 2, 100000} , then i will report the
new method of sort called bucket sort to solve the problem.
find out the neighbouring max D_value by counting sort in stack的更多相关文章
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- 《算法导论》——计数排序Counting Sort
今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...
- HDU 1718 Rank counting sort解法
本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的.奇怪的推断系统. 由于分数值的范围是0到100,很小,而student 号码又很大, ...
- 41. First Missing Positive(困难, 用到 counting sort 方法)
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- [Algorithms] Counting Sort
Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...
- 【HackerRank】 The Full Counting Sort
In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...
- 排序算法六:计数排序(Counting sort)
前面介绍的几种排序算法,都是基于不同位置的元素比较,算法平均时间复杂度理论最好值是θ(nlgn). 今天介绍一种新的排序算法,计数排序(Counting sort),计数排序是一个非基于比较的线性时间 ...
- [MIT6.006] 7. Counting Sort, Radix Sort, Lower Bounds for Sorting 基数排序,基数排序,排序下界
在前6节课讲的排序方法(冒泡排序,归并排序,选择排序,插入排序,快速排序,堆排序,二分搜索树排序和AVL排序)都是属于对比模型(Comparison Model).对比模型的特点如下: 所有输入ite ...
- 【算法】计数排序(Counting Sort)(八)
计数排序(Counting Sort) 计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中. 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范 ...
随机推荐
- 我的C语言进化史
Hello, world! 这三年就看看我的C语言还有JAVAscript进化史吧.更厉害的sunmarvell,我等你
- JAVA 各种数值类型最大值和最小值 Int, short, char, long, float,&nbs
转载地址:http://blog.sina.com.cn/s/blog_5eab3d430101fdv6.html 代码片段: fmax = Float.MAX_VALUE; fmin = Float ...
- Android Studio 导出jar包
不像在Eclipse,可以直接导出jar包.AndroidStudio只可以生成aar包. 在网上看到许多朋友问怎么可以像Eclipse一样导出jar包,其实我们只要知道它的原理就可以了. 用jar命 ...
- composer 使用笔记
使用composer 更新项目比如: composer create-project topthink/think wwwroot dev-master --prefer-dist提示openssl异 ...
- 域名管理系统DNS
域名系统DNS,将域名转化为ip地址.域名到ip地址解析过程是以这种方式进行的,当某一程序需要把主机名解析为IP地址时,该应用进程就调用解析程序(本地程序),这时候该进程就变成了DNS的一个客户,将待 ...
- Object类.
equals方法. 比较的是内存地址.比较的是是否指向同一对象. toString:将对象转换成字符串. System.out.println()等价于 System.out.println(obj ...
- 【项目】百度搜索广告CTR预估
-------倒叙查看本文. 6,用auc对测试的结果进行评估: auc代码如下: #!/usr/bin/env python import sys def auc(labels,predicted_ ...
- oracle‘s package,function,proceture编译时无响应(解决)
在对Procedure.Function或Package进行Debug时,如果长时间没有操作,公司的防火墙会杀掉会话连接.这个时候数据库不会主动的释放会话的资源,如果再次对Procedure.Func ...
- iOS版本更新在APP中直接访问AppStore
1.导入框架 #import <StoreKit/StoreKit.h> 2.添加代理 <SKStoreProductViewControllerDelegate> 3.设置跳 ...
- cocoapods真机调试出现问题解决
swift中使用cocoapods时,Podfile中必须写上 use_frameworks! 使用cocoapods导入框架在真机调试出现问题的解决方案: 1.build phases 2.+ ne ...