C Language Exercises 1 (about quick Sort, binary find and run time)

Source: Internet
Author: User
Tags random seed

  Just completed a senior to a topic:  

Randomly generated 10,000 digits, after fast sorting, using the binary lookup method to locate a number to query (keyboard input a number to query), the results of the output query time, and whether to find

  Share your own problem-solving ideas:

  1, to know how to generate numbers randomly

2, to understand the rapid sorting and the dichotomy of thought

3, until how to test the program run time

  The following is the code of their own, welcome you to put forward valuable comments and opinions, the niche grateful

1 /*2 This code describes:3 4 randomly generate 10,000-digit numbers, and after a quick sort,5 using the binary lookup method to locate a number to be queried6 (the keyboard enters a number to be queried),7 the time of the result output query, and whether or not to check8   9   */Ten #defineN 10000 One#include <stdio.h> A#include <stdlib.h> -#include <time.h>//because the time () function is used later to return the current times to make a random number seed -  the //One of the quick-sort functions that returns an axis value - intPartition (intA[],intFirstintend) - { -     inti; +     intJ; -     inttemp; +i = First, j = end;/*default axis value bit a[0], which is the leftmost a[i]*/ A  at      while(I&LT;J)/*This cycle continues when I is not equal to J*/ -     { -          while(I<j && a[i]<=a[j])/*start scanning from right to left*/ -         { -j--;/*until the right J value is not greater than the I value on the left*/ -         } in         if(i<j) -         { totemp =A[i]; +A[i] =A[j]; -A[J] =temp; thei++;/*The coordinate value of the other party is increased by 1 since the Exchange position*/ *         } $ Panax Notoginseng          while(I<j && a[i] <= a[j])/*scan from left to right*/     -         { thei++; +         } A         if(i<j) the         { +temp =A[i]; -A[i] =A[j]; $A[J] =temp; $j--; -         }                             -     } the     returnI/*until i==j, return this axis value*/ - }Wuyi  the  - //One of the split functions of this quick sort Wu voidQuickSort (intA[],intFirstintend) - { About     intPivot//Record axis values $     if(first<end) -     { -Pivot =Partition (a,first,end); -QuickSort (a,first,pivot-1); AQuickSort (a,pivot+1, end); +     } the } -  $ //two-part method the intSearch (intA[],intTargetintLowintHigh//incoming a[] Ascending array with the first end of target and array to find the {                                             the     intMiddle; the              -Middle = (Low+high)/2;//End of initialization in  the      while(High >= Low)//binary Search Now start, play the national anthem, raise the national flag, salute the     { About          the  the         if(Target >A[middle]) the         { +Low = middle +1;  -             //middle = (low+high)/2; This code will affect the following a[middle], so error the         }Bayi         Else if(Target <A[middle]) the         { theHigh = middle-1;  -             //middle = (low+high)/2; Delete this line and the preceding line of code.  -         } the         Else                  //here else is target = = A[middle] the         { the             return(Middle +1);//The key here, find the target jump out of the loop and return to the target I the         } -Middle = (high + low)/2; the     } the     return-1;//-1 for no target value found the }94  the  the voidMain () the {98     intA[n] = {0}; About     inti =0; -     intK,target;101 clock_t begin,end;102Srand ((Time (NULL)));//take system time as random seed103     104  theBegin =clock ();106      for(i =0; i < N; i++)//random Assignment Array a[10000] come on.107     {108A[i] =rand (); 109     } theEnd =clock ();111printf"the time to assign a random number is:%f s\n",(Double) (End-begin)/clocks_per_sec); theprintf"\ n");113  thebegin = Clock ();//Quick Sort theQuickSort (A,0, N-1); theEnd =clock ();117printf"The time taken for quick sorting is:%f s\n",(Double) (End-begin)/clocks_per_sec);118printf"\ n");119  -printf"the 1234th bit after sorting is%d:\n\n", a[1233]);//the 1233th position is the detection point121     122 123printf"the 1230th to 1240th digit is: \ n");124      for(i=1229;i<1240; i++) theprintf"%d", A[i]);126printf"\ n");127      -printf"\ n");129printf"Enter the number of queries you want:");  thescanf"%d",&target);131  thebegin = Clock ();//find the desired target by dichotomy133K = Search (A,target,0, N-1);134     if(k!=-1)135     {136printf"%d has been found, at%d bits \ n", target,k);137     }138     Else139     { $printf"I'm sorry I didn't find it! ");141     }142End =clock ();143printf"time used for this query%d:%f s\n", Target, (Double) (End-begin)/clocks_per_sec);144printf"\ n");145 146 147 148System"Pause");149      Max}


C Language Exercises 1 (about quick Sort, binary find and run time)

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.