Algorithm Design and Structure Chapter 2 Recursion and Divide and Conquer Strategy Homework

Knowledge supplement:
Insert picture description here

True or False

1-1
The two main aspects of algorithm analysis are the analysis of time complexity and space complexity.

T F

1-2
In a singly linked list with N nodes, the time complexity of accessing nodes and adding nodes corresponds to O(1) and O(N) respectively.

T F

1-3
The best "worst time complexity" that an algorithm based on comparison can get is O(NlogN).

T F

1-4
quickly sort N records. In the worst case, the time complexity is O(NlogN).

T F

1-5
(neuDS) The time complexity of the direct insertion sorting algorithm in the best case is O(n).

T F

Multiple choice

2-1
Use binary search to find a certain number from 100 ordered integers. In the worst case, the number of comparisons required is:

A.7
B.10
C.50
D.99

Knowledge supplement:
The maximum number of comparisons for
Insert picture description here
binary search is that the binary search method makes full use of the order relationship between elements, and adopts a divide-and-conquer strategy to complete the search task in O(logn) time in the worst case;

Analysis: log100 + 1 = 6 + 1 = 7;

2-2
Use divide and conquer to solve a problem of scale N. Which of the following methods is the slowest?

A. Each step divides the problem into 2 sub-problems with a scale of N/3, and the treatment step takes O(N)
B. Each step divides the problem into 2 sub-problems with a scale of N/3, and the treatment step Time-consuming O(NlogN)
C. At each step, the problem is divided into 3 sub-problems with a scale of N/2, and the treatment step takes O(N)
D. Each step divides the problem into 3 sub-problems with a scale of N/3, and the treatment step takes O(NlogN)

C is not only large in scale of sub-problems, but also large in number, and it takes a long time to merge sub-questions;

2-3
(NeuDS_C++) When performing binary search on the linear table, the linear table must be ().

A. Store in sequence
B. Store in chain
C. Stored in a sequential manner, and the nodes are sorted in order by keywords
D. Stored in the form of links, and the nodes are sorted in order by keywords

2-4
Which of the following sorting algorithms may appear: Before the start of the last pass, all elements are not in their final positions? (Suppose the number of elements to be arranged N>2)

A. Bubble sort
B. Insertion sort
C. Heap sort
D. Quick sort

2-5
Given 100,000,000 records to be arranged, each record is 256 bytes, and the memory is 128MB. How many rounds need to be done if simple 2-way merge is used?

(2 minutes)

A.10
B.9
C.8
D.7

1MB has 4 records, the total memory is 128×4 = 512 records, 512=2 9 , you need to do log 2 9 = 9 (log with 2 as the base) round;

2-6
Merge and sort N records, the order of magnitude of the number of merge passes is:

AO (logN)
BO (N)
CO (nlogn)
DO (N 2 )

2-7
Merge and sort N records, the space complexity is:

AO (logN)
B.O(N)
CO (NlogN)
DO (N 2 )

2-8
Use recursion to quickly sort the sequence table. Which of the following statements about the number of recursions is correct:

A. After each division, processing the longer partition first can reduce the number of recursion
B. After each division, processing the shorter partition first can reduce the number of recursion
C. The number of recursion has nothing to do with the partition processing order obtained after each division
D. The number of recursion has nothing to do with the order of the initial data

2-9
If the data element sequence {12, 13, 8, 11, 5, 16, 2, 9} is the result of the first sorting using one of the following sorting methods, the sorting algorithm can only be:

A. Quick sort
B. Select sort
C. Heap sort
D. Merge sort

In the 2-10
outer sort, suppose we have 5 ordered segments with lengths of 2 , 8, 9, 5, and 3. Which of the following merging order can get the shortest merging time?

A. Merge the ordered segments with lengths 2 and 3 to get the segment Run#1; merge Run#1 with the ordered segments with length 5 to get the segment Run#2; combine Run#2 with the ordered segments with length 8 Merging segments to get segment Run#3; Merging Run#3 with an ordered segment of length 9
B. Merge the ordered segments with lengths 2 and 3 to get the segment Run#1; merge Run#1 with the ordered segments with length 5 to get the segment Run#2; merge the ordered segments with lengths 8 and 9, Get section Run#3; merge Run#2 and Run#3
C. Merge the ordered sections with lengths 2 and 3 to obtain section Run#1; merge the ordered sections with lengths 5 and 8 to obtain section Run#2; merge Run#1 and Run#2 to obtain section Run#3 ; Merge Run#3 with the ordered section of length 9
D. Merge the ordered sections with lengths 2 and 3 to obtain the section Run#1; merge the ordered sections with lengths of 5 and 8 to obtain the section Run#2; Merge Run#2 with an ordered segment of length 9 to get segment Run#3; merge Run#1 and Run#3

Programming questions

7-1 Find the kth smallest number (20 points)

Design an algorithm with an average time of O(n) to find the kth smallest number among n(1<=n<=1000) unordered integers.

Tip: The function of the function int partition(int a[],int left,int right) is based on a certain element x in a[left]a[right] (such as a[left]) to a[left]a[right ] Is divided, the left segment of the divided x position is all less than or equal to x, and the right segment is all greater than or equal to x. At the same time, the position of x can also be used to calculate that x is the number of this batch of data in ascending non-descending order . Therefore, the int find (int a[], int left, int right, int k) function can be compiled, and the partition point can be obtained by calling the partition function to determine whether the partition point is the k-th smallest. If not, recursively call the find function to continue in the left segment or Find the right section.

Input format:
There are two lines of input: the
first line is n and k, 0<k<=n<=10000 and the
second line is n integers

Output format:
output the k-th smallest number

Input sample:
Here is a set of input. E.g:

10 4
2 8 9 0 1 3 6 7 8 2

Output sample:
The corresponding output is given here. E.g:

2
# include <iostream>
using namespace std;

int partition(int a[], int left, int right) {
    
    
	int i = left, j = right+1;
	int x = a[left];
	//将小于x的元素交换到左边区域,将大于x的元素交换到右边区域
	while(true) {
    
    
		while(a[++i] < x && i < right);
		while(a[--j] > x);
		if(i >= j)
			break;
		swap(a[i], a[j]);
	} 
	a[left] = a[j];
	a[j] = x;
	return j;
}

int find(int a[], int left, int right, int k) {
    
    
	int pos = partition(a, left, right);
	if(k-1 == pos){
    
    
		cout << a[k-1];
	}else if(k-1 < pos){
    
    
		find(a, left, pos-1, k);
	}else if(k-1 > pos){
    
    
		find(a, pos+1, right, k);
	}
	return 0;
}

int main() {
    
    
	int n, k;
	cin >> n >> k;
	int a[n];
	for(int i=0; i<n; i++){
    
    
		cin >> a[i];
	}
	find(a, 0, n-1, k);
	return 0;
} 

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/111870060