Algorithm experiment--linear time

Source: Internet
Author: User

First, the purpose of the experiment:

Familiar with the design techniques of divide and conquer algorithm.

Second, the experimental requirements:

1, according to the content requirements of teaching materials, complete the "Linear Time selection Problem" algorithm. Get a complete and correct program.

2, problem size: not less than

3, output the final result.

Third, the experimental equipment:

PC Machine One set

Iv. description of the problem:

By using the principle of divide-and-conquer method, the number of randomly generated numbers is searched, and the number of K - size is found, and then the number of the output is discovered.

Five, Algorithm analysis:

Yes

No

1. functions used:

void swap (int& x,int& y); the elements that are used on both sides of the datum element want to be exchanged when searching.

void Getrand (int *a); randomly generated - a -5000-5000 an integer.

int partition (int a[],int left,int right);

int line_select (int a[],int p,int r,int k);

2. Algorithm Analysis:

Using the idea of random partition, the average time complexity is o(n), the worst time complexity is o(n2)

The idea of divide and conquer algorithm:simulate a quick sort on an input arrayADivision of the two divisions,make a sub-arrayA1the Elements<=A2the elements in,Split PointJgenerated by random numbers,ifK<j,thekto beA1the firstkXiao Yuan, ifK>j,thekto beA2the firstk-jXiao Yuan.linear time selection is calculated by a quick sort point for the first point.

3. Progress towards

main="getrand () =" input i=> judgment i=0? ="no ="line_select () ="exit

Yes ="Exit"

Six, the code:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define P 0

#define R 2000

#define N 2000

#define M 10000

void swap (int& x,int& y);

void Getrand (int *a);

int partition (int a[],int left,int right);

int line_select (int a[],int p,int r,int k);

void Main ()

{printf ("-----------------");

int a[n];

int k;

Getrand (a);

printf ("The number of random numbers ( -5000 ~ x) has been generated -----------------\ n example ( First 10 ):\ n ");

for (int l=0;l<10;l++)

printf ("%d", a[l]);

printf ("\ n");

while (1) {

printf (" Please enter K to get A small number of K (0 exit ):");

scanf ("%d", &k);

if (k==0) {break;}

int I=line_select (A,P,R,K);

printf (" the small number of%d is :%d\n\n", k,i);

}

}

implements the exchange of integers x and y

void swap (int& x,int& y)

{

int t;

T=x;

X=y;

y=t;

}

generate random integers for -5000-5000

void Getrand (int *a)

{

Srand ((unsigned) time (NULL)); Initialize random number

for (int i=0;i<n;i++)

{

a[i]=5000-(int) rand ()%M;

}

}

query for element K small

int line_select (int a[],int p,int r,int k)

{

if (p==r) return a[p];

int i=partition (A,P,R);

int j=i-p+1;

if (K==J) return a[i];

else if (k<j)

Return Line_select (A,P,I-1,K);

Else

Return Line_select (A,I+1,R,K-J);

}

int partition (int a[],int p,int R)

{

int i=p;

int j=r+1;

int x=a[i];

while (true)// loop query, input 0 is the exit loop

{

while (A[++I]<X&&I<=R);

while (A[--J]>X);

if (i>j) break;

Swap (a[i],a[j]);

}

A[P]=A[J];

A[j]=x;

Return J;

}

Seven, commissioning and operation:

1. output 1, IV,1600, , The number of small (at the interval)

2. output 1,2000, Small number (at a distance of. )

2. outputs 1,IV,1800, Small number (at intervals of.)

Viii. Summary of the experiment

By reviewing the selection of the time limit of the dividing and administering method, the method and basic idea of the quick sort are given a deeper understanding, and the a[p of a certain datum element is used to divide the a[p:r] . Can quickly sort an array, this experiment not only allows me to review the essence of the fast sorting, but also practiced the once thought that it is impossible to find the linear time in the array of small number I algorithm.

Algorithm experiment--linear 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.