Find an algorithm - Drawing algorithm (if I tell you: no matter how much element, you only need to be aligned once, do you believe it or not?)

tags: data structure  algorithm  Signing algorithm  Find algorithm  

Basic ideas of the drawing algorithm

The lottery is the folk custom of China, which is one of the forms of divination. Then, the form of a lottery can be randomly identified in the sequence. If the luck is good enough, then no matter how much element, it only needs to be aligned once, this is the most mysterious place of this algorithm, and looks with Yang Shou.

step

  1. Generate a random number r a n d N u m randNum randNum
  2. Random number pairs of array lengths After obtaining the subscript of the pumping element i n d e x = r a n d N u m index=randNum%length index=randNum
  3. Compare the target elements, if not, repeat 1, 2; until found

Process demonstration

In the sequence [ 3 , 44 , 38 , 5 , 47 , 15 , 36 , 26 , 1 , 2 ] [3,44,38,5,47,15,36,26,1,2] [3,44,38,5,47,15,36,26,1,2] Find 2

  1. First result: Find 1 time

    image-20210908195234835

  2. The second result: Find 18 times

    image-20210908195513600

  3. The third result: Find 3 times

    image-20210908195608947

Algorithm code

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// Dump algorithm 
void DrawAlgorithm(int nums[],int num,int n){
	 SRAND ((int) time (0)); // Generate random seeds 
	int count=0,randNum,bound=n*n;		
	 While (True && Count <bound) {// Stop when the number of findings arrive in Bound 
		count++;
		 RandNum = rand ()% n; // Generate random number 
		if(nums[randNum]==num){			
			 Break; // If you find Num, Break exits the loop. 
		}
	}
	 Cout << "has found" << Num << ", looking for count:" << count;
} 

 // Print an array 
void printNum(int numbers[],int n){
	for(int i=0;i<n;i++){
		cout<<numbers[i]<<" ";
	}
	cout<<endl; 
}

int main()
{
	int numbers[10]={3,44,38,5,47,15,36,26,1,2};
	 INT n = sizeof (numbers) / sizeof (NumBers [0]); // array length 
	 Cout << "sequence is:"; 
	 Printnum (NumBers, N); // Print an array 
	 Drawalgorithm (NumBers, 2, N); // Call the DrawAlgorithm function to perform a lottery in the NumBers sequence 2 
    return 0;
}

Algorithm performance analysis

  • Spatial complexity: O ( 1 ) O(1) O(1)
  • Time complexity
    • Preferably, the time complexity: O ( 1 ) O(1) O(1)
    • Worst cases of time complexity: uncertain

Intelligent Recommendation

I tell you with CSS, how do I spend it every day ~

I tell you with CSS, how do I spend it every day ~ code show as below...

I said this is hbase, do you believe it (2)

4.3 MapReduce Through HBase's related JavaAPI, we can implement the MapReduce process accompanying HBase operations, such as using MapReduce to import data from the local file system into HBase tables...

I said this is hive, do you believe it (1)

1.1 What is Hive Hive: Data statistics tool open sourced by Facebook for solving massive structured logs. Hive is a Hadoop-based data warehouse tool that can map structured data files into a table and...

I said this is hdfs, do you believe it (2)

Chapter 5 NameNode and SecondaryNameNode (Interview Development Focus) 5.1 NN and 2NN working mechanism NN and 2NN working mechanism, as shown in Figure 3-14. The first stage: NameNode start (1) After...

I said this is hdfs, do you believe it (1)

Chapter 1 HDFS Overview 1.1 HDFS output background and definition 1.2 Advantages and disadvantages of HDFS 1.4 HDFS file block size (interview focus) Chapter 2 Shell Operation of HFDS (Development Foc...

More Recommendation

I said this is Azkaban, do you believe it

I. Overview 1.1 What is Azkaban Azkaban is a batch workflow task scheduler launched by Linkedin. It is mainly used to run a group of jobs and processes in a specific order within a workflow. Its confi...

I said it was sqoop do you believe

Chapter 1 Introduction to Sqoop Sqoop is an open source tool, mainly used for data transfer between Hadoop (Hive) and traditional databases (mysql, oracle...), which can be a relational database ( For...

I said this is hbase, do you believe it

Chapter 1 Introduction to HBase 1.1 HBase definition HBase is a distributed, scalable, NoSQL database that supports massive data storage and can perform real-time calculations. 1.2 HBase data model Lo...

I said this is Kafka, do you believe it

Chapter 1 Overview of Kafka 1.2 Message Queue 1.2.1 Application scenario of traditional message queue 1.2.2 Two modes of message queue (1) Point-to-point mode (one-to-one, consumers take the initiativ...

I said this is flume, do you believe it

Chapter 1 Overview of Flume 1.1 Flume definition Flume is a highly available, highly reliable, distributed mass log collection, aggregation and transmission system provided by Cloudera. Flume is based...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top