It is interesting to see an algorithm interview question. I did it myself in C. Question: randomly generate 10 integers less than 100, sort data in ascending order, and the algorithm complexity is only 1. This algorithm is interesting. First, we create an array B, whose number of elements is the maximum element value of array A, and then use the element of A as the subscript of array B, then assign values to the existing element B so that the subscript can be output in a loop.
It is interesting to see an algorithm interview question. I did it myself in C.
Question: randomly generate 10 integers less than 100, sort data in ascending order, and the algorithm complexity is only 1.
This algorithm is interesting. First, we create an array B, whose number of elements is the maximum element value of array A, and then use the element of A as the subscript of array B, then assign values to the existing element B so that the subscript can be output in a loop.
The C program is as follows:
# Include
# Include
# Include
# Define random (x) (rand () % x) main () {int lengthA, lengthB, I; int wait; int arrayA [10]; // define an array int arrayB [101]; srand (time (NULL); // Make the random number generated each time different from lengthA = sizeof (arrayA) /sizeof (arrayA [0]); lengthB = sizeof (arrayB)/sizeof (arrayB [0]); // assign an array value for (I = 0; I <10; I ++) arrayA [I] = random (100); printf ("The element of the randomly generated array A is \ n"); // output array for (I = 0; I <10; I ++) printf ("% d \ n", arrayA [I]); for (I = 0; I <lengthA; I ++) {arrayB [arrayA [I] = 101;} printf ("The sorted result is \ n"); for (I = 0; I <lengthB; I ++) {if (arrayB [I] = 101) printf ("% d \ n", I);} // printf ("% d", lengthA ); scanf ("% d", & wait );}
Program running result:
The elements of the randomly generated array A are 79628743325272884453 sorted and the result is 32434452536272798788.
This article is available at http://www.nowamagic.net/librarys/veda/detail/508.