find out the neighbouring max D_value by counting sort in stack

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #define MAX_STACK 10
  4
  5 int COUNT = 0;
  6 // define the node of stack
  7 typedef struct node {
  8     int data;
  9     node *next;
 10 }*Snode;
 11
 12 // define the stack
 13 typedef struct Stack{
 14     Snode top;
 15     Snode bottom;
 16 }*NewStack;
 17
 18 void creatStack(NewStack s) {
 19     // allocate stack
 20     // allocate the elements of stack
 21
 22     s->top = (Snode) malloc(sizeof(node));
 23     s->bottom = s->top;
 24     s->top->next = NULL;
 25 }
 26
 27 bool push(NewStack s,int val) {
 28     //Snode top = s->top;
 29 //    Snode bottom = s->bottom;
 30     if(COUNT == MAX_STACK) {
 31         printf("StackOverFlowError\n");
 32         return false;
 33     }
 34     Snode newNode = (Snode) malloc(sizeof(node));
 35     newNode->data = val;
 36
 37     // push the new node into the stack
 38     newNode->next = s->top;
 39     s->top = newNode;
 40     COUNT ++;
 41     //printf("%d \n",s->top->data);
 42     return true;
 43 }
 44
 45 int pop(NewStack s) {
 46     if(s->top == s->bottom) {
 47         printf("this is bottom of stack!\n");
 48         return NULL;
 49     }
 50     int val = s->top->data;
 51     Snode tempNode = s->top;
 52     s->top = s->top->next;
 53     free(tempNode);
 54     return val;
 55 }
 56
 57 void printStack(NewStack s){
 58     Snode priNode = (Snode) malloc (sizeof(node));
 59     priNode = s->top;
 60     printf("now it is the show time of ur stack:\n");
 61     while(priNode != s->bottom) {
 62         printf("%d \t",priNode->data);
 63         priNode = priNode->next;
 64     }
 65 }
 66
 67 void scanfStack(NewStack s) {
 68     printf("now u can write down the elements u cant push:\n");
 69     int i = 0;
 70     int val;
 71     for(i = 0; i<MAX_STACK; ++i) {
 72         scanf("%d",&val);
 73         push(s,val);
 74     }
 75 }
 76
 77 bool deleteStack(NewStack s) {
 78     Snode clear = (Snode) malloc (sizeof(node));
 79     while(s->top != s->bottom) {
 80         clear = s->top;
 81         s->top = s->top->next;
 82         free(clear);
 83     }
 84
 85     return true;
 86 }
 87
 88 int getMax(NewStack s) {
 89     Snode top = s->top;
 90     Snode bottom = s->bottom;
 91     int max = top->data;
 92     while(top != bottom) {
 93         if(top->data > max) {
 94             max = top->data;
 95         }
 96         top = top->next;
 97     }
 98
 99     return max;
100 }
101
102 int getMin(NewStack s) {
103     Snode top = s->top;
104     Snode bottom = s->bottom;
105     int Min = top->data;
106     while(top != bottom) {
107         if(top->data < Min) {
108             Min = top->data;
109         }
110         top = top->next;
111     }
112
113     return Min;
114 }
115
116 // using the counting sort -- is not appropriate for the neibouring numbers where there are big difference.
117 int getMaxNeighD_value(NewStack s){
118     Snode top = s->top;
119     Snode bottom = s->bottom;
120     int max = getMax(s);
121     int min = getMin(s);
122     int num = max-min+1; // get the length of the new counting sort array
123     int arr[num];
124     int i = 0;
125     int j = 0;
126
127     // to put the elements into the new counting sort array
128     for(; j<num; ++j) {
129         arr[j] = -1;
130     }
131     while(top != bottom) {
132         arr[top->data - min] = top->data;
133         top = top->next;
134     }
135
136     // to find out the max zone where there are max number of -1
137     int Max_count = 0;
138     int count = 0;
139     for(;i < num; ++i) {
140         //printf("%d:%d\n",i,arr[i]);
141         if(arr[i] == -1) {
142             count ++;
143         }else {
144             if(count > Max_count) {
145                 Max_count = count;
146             }
147             count = 0;
148         }
149         //printf("%d\n",Max_count+1);
150     }
151
152     return Max_count+1;
153 }
154
155 int main() {
156     NewStack s = (NewStack) malloc(sizeof(Stack));
157     creatStack(s);
158 //    push(s,5);
159 //    push(s,6);
160 //    push(s,4);
161 //    push(s,7);
162 //    push(s,10);
163 //    push(s,9);
164 //    push(s,3);
165 //    push(s,56);
166 //    push(s,88);
167 //    push(s,44);
168 //    push(s,66);
169     scanfStack(s);
170     printStack(s);
171     //printf("%d \t",pop(s));
172     int max = getMax(s);
173     int min = getMin(s);
174     printf("%d %d\n",max,min);
175     int c = getMaxNeighD_value(s);
176     printf("the max neighbouring D_value is : %d \n",c);
177     //deleteStack(s);
178     //pop(s);
179 }

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.

时间: 2024-12-21 00:18:00

find out the neighbouring max D_value by counting sort in stack的相关文章

counting sort 计数排序

//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorithm> #include<cassert> using namespace std; const int k=5; const int n=7; int a[n]={5, 5, 1, 2 , 5, 4, 1}; int b[n]; int c[k+1]; int main() { int m

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

HDU 1718 Rank counting sort解法

本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的,奇怪的判断系统. 因为分数值的范围是0到100,非常小,而student 号码又非常大,故此天然的需要利用counting sort的情况. #include <stdio.h> #include <string.h> const int MAX_N = 101; int arr[MAX_N]; int main() { int Jackson, JackScore,

经典排序算法 - 计数排序Counting sort

经典排序算法 - 计数排序Counting sort 注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 看下具体的过程,一共需要三个数组,分别是待排数组,票箱数组,和桶数组 var unsorted = new int[] { 6, 2, 4, 1, 5, 9 };  //待排数组 var ballot = new int[unsorted.Len

[Algorithms] Counting Sort

For a nice introduction to counting sort, please refer to Introduction to Alogrithms, 3rd edition. The following code is basically a translation of the pseudo code above. 1 #include <iostream> 2 #include <vector> 3 #include <ctime> 4 5 u

counting sort

public class counting_sort {           //O(n)   it is stable(numbers with the same value appear in the output array in the same order as they do in the input                                                      //array) public static void sort(int[]

计数排序Counting sort

注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 看下具体的过程,一共需要三个数组,分别是待排数组,票箱数组,和桶数组 var unsorted = new int[] { 6, 2, 4, 1, 5, 9 };  //待排数组 var ballot = new int[unsorted.Length];          //票箱数组 var b

基数排序与桶排序,计数排序【详解】

桶排序简单入门篇^-^ 在我们生活的这个世界中到处都是被排序过的东东.站队的时候会按照身高排序,考试的名次需要按照分数排序,网上购物的时候会按照价格排序,电子邮箱中的邮件按照时间排序……总之很多东东都需要排序,可以说排序是无处不在.现在我们举个具体的例子来介绍一下排序算法. 首先出场的是我们的主人公小哼,上面这个可爱的娃就是啦.期末考试完了老师要将同学们的分数按照从高到低排序.小哼的班上只有5个同学,这5个同学分别考了5分.3分.5分.2分和8分,哎,考得真是惨不忍睹(满分是10分).接下来将分

Complexities

This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and