Answer:
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
Simple implementation: Jon Bentley shows a three-line C version, and a five-line optimized version[1]
Efficient for (quite) small data sets, much like other quadratic sorting algorithms
More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort
Adaptive, i.e., efficient for data sets that are already substantially sorted: the time complexity is O(kn) when each element in the input is no more than k places away from its sorted position
Stable; i.e., does not change the relative order of elements with equal keys
In-place; i.e., only requires a constant amount O(1) of additional memory space
Online; i.e., can sort a list as
Answer:
If there is a majority element in the array it will be the median. Thus,
just run the linear time median finding algorithm, and compare the result with all the elements of the array (also linear time). If elements are the same as the median, the median is a ma majority element. If not, there is none.
Suppose now that the elements of the array are not from some ordered domain like the integers, and so there can be no comparisons of the form "is the ith element of the array greater than the jth element of the array?" However you can answer questions of the form:
"Are the ith and jth elements of the array the same?" in constant time. Such
queries constitute the only way whereby you can access the array. (Think of the elements of the array as GIF files, say.) Notice that your solution above cannot be used now.
Answer:
The answer to this question can be given as:
Program:
#define class.
class PersonInfo:
def __init__(self): #constructor
self.num_kids = 0
def inc_num_kids(self): #define function inc_num_kids()
self.num_kids = self.num_kids + 1
return self.num_kids #return value.
p= PersonInfo() # creating object
print('Kids:', p.num_kids) #print value
p.inc_num_kids() #call function
print('New baby, kids now:', p.num_kids) #print value
Output:
Kids: 0
New baby, kids now: 1
Explanation:
The description of the above python program as follows:
- In this program firstly we define the class that is PersonInfo. In this class we define the constructor that is def __init__() and one function that is def inc_num_kids().
- The constructor is called automatically when the class object is created. In this constructor, we use the self as a parameter that is used to access variables that belong to class.
- In the constructor, we define the variable that is "num_kids". We assign value to a variable that is "0" and use the self for holding reference to the variable.
- Then we define the function. In this function, we increment the value of the variable by 1 and return its value.
- Then we create a class object that is p and call the function and print its value.