/// <summary> ///Pigeon Nest Sort///creates an array of tokens with a length greater than or equal to the maximum value in the array of arrays of sorts, Mark,///maps the number of element values in the array to the Mark array. ///that is, the element value in array arrays corresponds to the index of the mark Array,///the number of occurrences in the array array corresponds to the value of the Mark array index. ///The index value and the number of the mark element (not equal to 0) are then placed in the array in turn. /// </summary> /// <param name= "Array" >Array to sort</param> /// <param name= "Maxvalueinarray" >the maximum element value in an array of arrays</param> Public voidPigeonholesort (int[] Array,intMaxvalueinarray) { int[] Mark =New int[Maxvalueinarray +1]; for(inti =0; I < array. Length; i++) { ///to mark the number of occurrences of an element in an arraymark[array[i]]++; } intArrayindex =0; ///re-assign to array for(inti =0; I < mark. Length; i++) { ///The number of occurrences of I, starting with the array minimum element value I for(intj =0; J < Mark[i]; J + +) { ///updates the elements of an array of arrays to the minimum I and the number of Jarray[arrayindex++] =i; } } } /// <summary> ///gets the maximum element value in an array of arrays/// </summary> /// <param name= "Array" >Array</param> /// <returns>Maximum element value</returns> Public intMax (int[] Array) { intmax =-1; foreach(intIteminchArray)if(Item >max) Max=item; returnMax; }
Worst time complexity: O (N+n)
Best time complexity: O (N+n)
Average time complexity: O (N+n)
Worst spatial complexity: O (N*n)
C # implements Pigeon Nest sorting