using System.Collections.Generic;
using System.Diagnostics;
public static void Main()
Random randNum = new Random();
double[] timeResults1 = new double[runs];
double[] timeResults2 = new double[runs];
Console.WriteLine( randNum.Next(0, 1) );
for(int run = 0; run < runs; run++)
int Min = randNum.Next(0, runs);
int Max = Min + randNum.Next(1, maxMaxInt);
int n = randNum.Next(1, maxLengthdata);
double[] values = new double[n];
for (int i = 0; i < values.Length; i++)
values[i] = randNum.Next(Min, Max);
double alpha = randNum.Next(0, 1);
double percentage = alpha * 100.0 + 1;
Console.Write("Percentage: " + percentage + ", i.e. Alpha: " + alpha + ", \n" + "Values (#"+ n + "): [ ");
for (int i = 0; i < values.Length - 1; i++) Console.Write(values[i] + ", ");
Console.WriteLine(values[values.Length - 1] + " ]");
var sortedValues = values.OrderBy(d => d).ToList();
timeResults1[run] = Benchmark(() => { AlphaQuantile(values, alpha); }, 10);
timeResults2[run] = Benchmark(() => { AlphaQuantile2(sortedValues, alpha); }, 10);
private static double Benchmark(Action act, int iterations)
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
return (sw.ElapsedMilliseconds / iterations);
private static T AlphaQuantile2<T>(IReadOnlyList<T> values, double alpha)
if (values == null || values.Count == 0)
throw new ArgumentException("Invalid paramater values. Array can not be empty.");
if (alpha < 0 || alpha > 1)
throw new ArgumentException("Invalid paramater alpha. Values have to be between 0 and 1.");
else if (alpha == 0) return values[0];
else if (alpha == 1) return values[values.Count - 1];
var shiftedIndex = System.Math.Floor((values.Count - 1) * alpha + 1.0d);
int index = shiftedIndex > 1 ? (int)(shiftedIndex - 1) : 0;
Console.WriteLine("index: " + index);
private static T AlphaQuantile<T>(IReadOnlyList<T> values, double alpha)
values = values.OrderBy(d => d).ToList();
if (values == null || values.Count == 0)
throw new ArgumentException("Invalid paramater values. Array can not be empty.");
if (alpha < 0 || alpha > 1)
throw new ArgumentException("Invalid paramater alpha. Values have to be between 0 and 1.");
else if (alpha == 0) return values[0];
else if (alpha == 1) return values[values.Count - 1];
var shiftedIndex = System.Math.Floor((values.Count - 1) * alpha + 1.0d);
int index = shiftedIndex > 1 ? (int)(shiftedIndex - 1) : 0;
Console.WriteLine("index: " + index);