Data structure and algorithm analysis--c language description

Source: Internet
Author: User

P1.1 Select the problem, select the number of K, and draw N for the different values of the run time, K=N/2

Graduation 2.5, rewrite sort, feel good. The code uses a bubbling sort, and the library functions clock calculates the approximate run time.

1 //P1_1.cpp:Defines the entry point for the console application.2 //3 4#include"stdafx.h"5#include <malloc.h>6#include <stdlib.h>7#include <time.h>8 9 voidSwapint& A,int&b);Ten  One intMainintargcChar*argv[]) A { -  -     intN =40000; the     intK = n/2; -     inti,j; -  -     //Dynamic Arrays +     int*dynarr = (int*)mallocNsizeof(int)); -     //generating a random array + Srand ((unsigned) time (NULL)); A      //printf ("%d%c", Rand (), ' \ t '); at       for(i=0; i<n;i++) -      { -Dynarr[i] = rand ()% -; -          //printf ("%d%c", dynarr[i], ' \ t '); -      } -  in  -     intBeginTime = Clock ();//Timing Starts to  +     //Bubble Sort -     inttemp =0; the      for(i=0; i<n;i++) *          for(j=0; j<n;j++) $         {Panax Notoginseng             if(Dynarr[i]>dynarr[j]) {//left > right, swap - swap (dynarr[i],dynarr[j]); the             }     +}//The first layer ends, the maximum number is at the right A  the     intEndTime = Clock ();//End of timing +     intRunningtime = Endtime-begintime;//the calculated units are milliseconds . -  $  $  -      for(i=0; i<n;i++){ -     //Dynarr[i] = rand ()%100; the          //printf ("%d%c", dynarr[i], ' \ t '); -      }Wuyi      the     CharStr1[] ="number of K-Large numbers:"; -     CharStr2[] ="elapsed Time (milliseconds):"; Wuprintf"\n%d%s%d\t", N,str1,dynarr[k]); -printf"\n%s%d\n", str2,runningtime); About  $  -     return 0; - } -  A voidSwapint& Left,int&Right ) { +     inttemp; thetemp =Left ; -left =Right ; $right =temp; the}

Change the N value to test the elapsed time of different sample values.

If the data is a student's examination result of one subject, percentile. n indicates the number of students.

N Time (MS)
00
501
1005
50049
1000101
5000272

N (million) time (MS)
1281
21109
32506
44384
56764
69747
713296
817421
922078
1027307

Use this site to plot graphs:

Feeling is an increment. In fact, this is an illusion ... Look at these two small graphs:

The number of people is normal increment, but time is not increment, time cost is rising rapidly.

Data structure and algorithm analysis--c language description

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.