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) 计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中. 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范 ...
随机推荐
- Java 时间的表示
//不建议的方式 out.println("<p>结束Servlet的时间:" + new Date().toLocaleString() + "</p ...
- 各大浏览器内核介绍(Rendering Engine)
在介绍各大浏览器的内核之前,我们先来了解一下什么是浏览器内核. 所谓浏览器内核就是指浏览器最重要或者说核心的部分"Rendering Engine",译为"渲染引擎&qu ...
- Javascript设计模式学习三(策略模式)
定义:定义一系列的算法,把它们一个个封装起来,并且使它们可以互相替换.目的:将算法的使用和算法的实现分离开来.比如: if(input == 'A'){ return 1; } if(input == ...
- C#RSA算法实现+如何将公钥为XML格式转为PEM格式,给object-C使用
.net中,处于安全的考虑,RSACryptoServiceProvider类,解密时只有同时拥有公钥和私钥才可以.原因是公钥是公开的,会被多人持有.这样的数据传输是不安全的.C#RSA私钥加密,公钥 ...
- ES6学习笔记一
块级作用域:在ES5中只有全局作用域与函数作用域,ES6中新增的块级作用域避免变量的覆盖与泄露.考虑到代码运行环境,在块级作用域中应避免声明函数,最好用函数表达式的方式声明. let与const:le ...
- redis的一些操作
public class WnsRedisFactory { private static Cache pool = null; private static JedisConnectionFacto ...
- 在Extjs中对日期的处理,以及在后端数据在SQL语句的判断处理
jsp页面可选择时间: { xtype : 'datefield', id : 'START_CREATION_DATE_', format : 'Y-m-d H:i:s', submitFormat ...
- JavaScript学习笔记(1))——————call,apply方法
学习前端也有一段时间了,但是效果甚微.利用时间不够充分,虽然是利用工作之余来学习.但是这不能成为我的借口. 今天学习了(其实看了很多遍)call apply方法. function abc(a,b){ ...
- Qtp常见问题
(1)Qtp不能识别对象 插件加载错误 IE内未加载BHOManager加载项
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...