# Algorithm manual with 1 randomization input and Timer

Source: Internet
Author: User

1. randomization Input

In the use of the quick sorting algorithm, it is found that its performance is unstable. Its speed depends on the input, and the complexity is O (nlogn) in the best case ), the worst case is O (n ^ 2 ).

The simplest optimization method is to randomize the input and disrupt the input array order. This can minimize the possibility of poor input performance, which is very important for predicting the running time of the algorithm.

Considering that the STD standard library in C ++ already has the random_shuffle function, there are similar methods in other languages, but C # does not have this standard function, So I simply implement it myself.

Design:

Here we use the simplest O (n) Design and require n exchanges, which is undoubtedly very simple to implement.

Implementation:

`public class RandomShuffle    {        private static Random rd;        private static int N;        public static IComparable[] shuffle(IComparable[] a)        {            rd = new Random();            N = a.Length;            for (var i = 0; i < N; i++)            {                var newPosition = rd.Next(N);                var temp = a[i];                a[i] = a[newPosition];                a[newPosition] = temp;            }            return a;        }}`

Summary:

Here, iterative writing is more concise and clear, but large-scale iteration will lead to the stackoverflowexception of the thread stack, so it is safer to use the loop.

2. Timer

The performance of an algorithm is measured in two aspects: running time and memory performance. In algorithm analysis, running duration is an important indicator, but it is difficult to accurately measure the running duration of a given program, fortunately, we only need a similar result.

We can use the system API to implement a timer with excellent precision, which can help us verify the algorithm speed.

Design:

Design a stopwatch class. Its elapsedtime () method can output the program execution time. It is implemented based on the. NET Framework datetime class and can return results in milliseconds/second count.

Implementation:

` public class Stopwatch    {        private static DateTime startTime;        public Stopwatch(DateTime time)        {            startTime = time;        }        public void elapsedTime()        {            DateTime endTime = DateTime.Now;            double time = (endTime.Hour - startTime.Hour) * 3600 * 1000 + (endTime.Minute - startTime.Minute) * 60 * 1000                          + (endTime.Second - startTime.Second) * 1000 + (endTime.Millisecond - startTime.Millisecond);            if (time > 1000)            {                time /= 1000;                Console.WriteLine("time: " + time + "s");            }            else                Console.WriteLine("time: " + time + "ms");        }    }`

Summary:

Although the timer is not accurate, it is enough to evaluate the performance of the algorithm.

Algorithm manual with 1 randomization input and Timer

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.