Are there any algorithms that can be used to take as many widths as possible before and after the values?

Asked 2 years ago, Updated 2 years ago, 100 views

Are there algorithms that can be used to take as many widths as possible before and after the values?

For example, if the width is 15 based on the average value of 99 at this input,

98
174
58
49
78
90
75
170

I look forward to the following results.

36-51
52-67
68-83
84-99
100-115
116-131
132-147
148-163
164-179

This is obtained by finding the average value and adding the width in order, but this technique requires two loops, one higher than the reference value and the other smaller. Is there an algorithm that can do this sequentially and efficiently?

Since this is used for frequency distribution, we will count the number of numbers that fall within each range. If there is a better method, including this background, please let us know.

algorithm

2022-09-30 21:46

1 Answers

You can think that there is no sequential algorithm that is much faster than the algorithm that the questioner is thinking about.

First, you can calculate the average, minimum, and maximum values when a numeric column is given as input data.At this time, the input data is scanned only once.This is the minimum number of scans since you must scan at least once to know the average value.

After that, the width is calculated, but the smaller one and the larger one have two loops, but the width that should be output is only output once at a time.Note that the calculation time order here is O(N), not O(N2) (not loop nested).In order to output the entire width, it must be scanned at least once, so it is the minimum number of times.Depending on the specific method of output, there may be a little waste, but you can think of it as almost no waste.

Therefore, the algorithm that the questioner is thinking about is already efficient enough.

If you want the loop to be one, you can calculate the average value + (width +1)*((v-average value)/(width +1))) (/ is integer division) for the minimum value v, so you can start the loop from here.


2022-09-30 21:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.