Array indexes can also be sorted.

Source: Internet
Author: User
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.

Contact Us

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.