Data structures and algorithms: arrays (i)

Source: Internet
Author: User

Data structure and algorithm is the cornerstone of computer development, the origin of modern computer is mathematics, the core of mathematics is the algorithm, every big change in computer history is inseparable from the promotion of the algorithm. Even though "all roads lead to Rome", a good algorithm is always better than a hardware device.

    • Finds the number of occurrences of a given number in a sorted array
    • Computes the intersection of two ordered integer arrays
    • How to find the most repeated number in an array
    • Find out how many occurrences in an array are more than half in the time complexity of on
    • Find the unique repeating element in the array
      • Solving methods
      • Extension one does not require that each array be accessed only once cannot use secondary storage space
      • Extended Two

In the sorted array, find out the number of occurrences of a given number

In the sorted array, find out the number of occurrences of a given number. For example, 2 occurrences in "1,2,2,2,3" are 3 times ...

The simple and rough way is to iterate through it, counting it with a counter, and count the number of occurrences of 2.

Optimization method: The problem is improved on the basis of binary search. Set array to increment sequence, need to find the element is finddata, in order to solve the number of occurrences of a given number of times, you can find finddata in the first occurrence of the array and the last position, through the two arithmetic operation can get the number of occurrences. When encoding, using the variable last to store the location of the search, and then change the direction according to the situation, you can determine the first occurrence of the position of the subscript left and the final position subscript right value.

#include <iostream>using namespace std;intBinarySearch (int*a,intLengthintNUM, bool isleft) {int  Left=0;int  Right= Length-1;intLast =0; while( Left<= Right)    {int Mid= ( Left+ Right) /2;if(a[Mid] < num) { Left=Mid+1; }Else if(a[Mid] > num) { Right=Mid-1; }Else{last =Mid;if(Isleft) Right=Mid-1;Else  Left=Mid+1; }} return last>0? Last:-1;}intMain () {intData[] = {0,1,2,3,3,3,3,3,3,4,5,6,7, -, +};intLower = BinarySearch (data,sizeof (data)/sizeof (data[0]),3,true);intUpper = BinarySearch (data,sizeof (data)/sizeof (data[0]),3,false);intCount = Upper-lower +1;    cout << Count << Endl; Return0;}
Computes the intersection of two ordered integer arrays

For example, two sequential (non-descending) integer arrays A and B with n elements (no duplicate elements in arrays A and B) find common elements.
A = 0, 1, 2, 3, 4
b = 1, 3, 5, 7, 9
The intersection is {1,3}.

There are many ways to calculate the intersection of arrays, but the relative size of the array generally affects the efficiency of the algorithm, so you need to determine the method used based on the size of the two arrays.

(1) for two arrays of equal length, the following 3 methods can generally be taken.

    • Use two-way merge to traverse two arrays

      Set two arrays of array1[n1] and array2[n2], and iterate through two arrays from the beginning, respectively, with I and J. During traversal, if Array1[i] and array[j] are equal for the current traverse position, this number is the intersection of two arrays, is recorded, and continues to traverse array1 and array2 backwards. If Array1[i] is greater than array2[j], you need to continue traversing the array2 backwards. If Array1[i] is less than array2[j], you will continue to traverse the array1 backwards until an array ends the traversal and stops.

    • Sequentially traverses two arrays, storing array elements in a hash table, and counting the array elements of the statistic. If 2, the intersection element for both.

    • Iterate through any of the two arrays, store the traversed elements in a hash table, and then traverse the other array, querying the established hash table and, if present, the intersection element.

(2) for two array lengths, if the length of the array A is far greater than the length of array B, there are several methods that can be used.

    • Iterate through the array of small lengths, and then search through the resulting array elements in a long array for two points. Specifically, set two pointers to the elements at the end of the two array, take the smaller number in the second array, find it, have an intersection, and point the target array pointer to the previous position of that position. If it is not found, you can also find a location so that the number in the target array after that position is definitely not present in the other array, the pointer to the target array is moved directly to the previous position in that position, and then the loop is searched until an array is empty. Because the number of duplicates can occur in two arrays, when a binary lookup is found with the same number x, its subscript is "I", then the lower bound of the next binary lookup becomes i+1, avoiding x re-use.

    • Use a method similar to method, but each time the lookup is based on the previous lookup, this can greatly reduce the length of the lookup table.

    • A method similar to method two is used, but the way to traverse a small array is different, starting at the same time as the head and tail of the array, so that the length of the lookup table can be further reduced.

How to find the most repeated number in an array

For example, the array {1,1,2,2,4,4,4,4,5,5,6,6,6,}, element 1 appears two times, Element 2 appears 2 times, element 4 occurs 4 times, element 5 occurs two times, element 6 appears as many times as 3, The problem is to find out the number of repetitions that occur most, so the output should be element 4. You can use the following two methods to calculate the maximum number of repetitions in an array.

(1) define an array int Count[max] with space for time, and initialize its array elements to 0, then execute the for (int i=0;i<100;i++) count[a[i]]++; operation, find the maximum number in count, That is, the most repeated number.

(1) is a typical algorithm for space-time change. In general, this method is generally not used unless the memory space is large enough.

(2) Using Map Mapping table, by introducing map table (map is an associative container of STL, it provides one-to-one data processing ability, the first is a keyword, each keyword can only appear in the map once, the second is called the value of the keyword) to record the number of occurrences of each element, Then determine the number of times, and then find the most repeated elements.

#include <iostream>#include <map>using namespace STD;BOOLFindmostfrequentinarray (int*a,intSizeint&val) {if(size==0)return false; map<int, int>M for(inti =0; i<size; i++) {if(++m[a[i]]>=m[val]) val = a[i]; }return true;}intMain () {intA[] = {1,2,3,4,4,4,5,5,5,5,6};intval =0;if(Findmostfrequentinarray (A, One, Val))cout<< Val << Endl;return 0;}
Find out how many occurrences in an array are more than half in the time complexity of O (n)

If there is no requirement for time complexity, many methods can be used.

    • The first method is to create a two-dimensional array, one-dimensional storage array of data, two-dimensional memory of the number of occurrences, the number of occurrences of the most number is to find the number. Because the number of occurrences is more than half the length of the array, the length of the two-dimensional array requires only half of the array, but the time complexity and spatial complexity of this method are relatively large.

    • The second method is to sort the array first, then take the middle element, because if an element is more than half the number, then the element must occupy the middle of the array after sorting. If the largest number is the smallest, then n+1/2 is the number, and if the largest number is the maximum, then (n-1)/2-n is the number, if not the smallest is not the largest, when the number from the smallest to the largest number, you will find the value of the middle number is constant, So the middle number is the number you're looking for. Time complexity is the time for sorting, i.e. the time complexity O (NLOGN) of the quickest sorting algorithm.

However, due to the complexity of time requirements, the above methods are obviously not up to the requirements, undesirable, need to take unconventional methods, using some other techniques to achieve.

    • Each time you remove two different numbers, the number that repeats in the remaining numbers is definitely more than the other numbers, reducing the scale. If you delete two different numbers at a time (regardless of the maximum number of packages), then in the remaining digits, the frequency of the original high frequency is more than 50%, repeating the process, the last remaining will be the city of the same number, that is, the highest frequency number. This algorithm avoids sorting, with only O (n) of time complexity.
#include <iostream>using namespace STD;intFindmostapperse (int*num,intLen) {intcandidate =0;intCount =0; for(intI=0; i<len; i++) {if(count==0) {candidate = Num[i]; Count =1; }Else{if(candidate = = Num[i]) count++;Elsecount--; }    }returnCandidate;}intMain () {intArr[] = {2,1,1,2,3,1,1,1};intLen =sizeof(arr)/sizeof(arr[0]);cout<< findmostapperse (Arr,len) << Endl;return 0;}
    • Hash method. First, create a hash_map, where key is an array element value, and the number of times that value appears. Iterate through the array, use HASH_MAP to count the number of occurrences of each number, and two values to store the current number of occurrences and corresponding occurrences of the times, at this time the complexity of O (n), the space complexity of O (n) to meet the requirements of the topic.
    • Use two variables A and B, where variable a stores the number in an array, and variable B is used to count. Start by initializing variable B to 0 and iterating through the array:

      If the current number differs from a, you need to discuss it in two different situations
      (1) If B equals 0, then a equals the current number, and B equals 1.
      (2) If B is greater than 0, make b=b-1.
      If the current number is the same as a, make b=b+1. At the end of the traversal, the number stored in a is the number you are looking for. The time complexity of this algorithm is O (n), the spatial complexity is O (1)

#include <iostream>using namespace Std;int main () {int I,AB;Inta[Ten] = {1,2,3,1,2,1,1,6,1,1};    A=a[5];D0;for (i=0;i<10; i++){if(b==0)        {A=aI;B =1;}Else if(A==a[i]) {b++;}Else if(A!=a[i]) {b--;}} cout <<A<<endl;    return 0;}
Find the unique repeating element in the array

Array A[n], 1 to N-1 the number of N-1 is stored in a[n], where a number repeats once, writing a function to find the duplicated number. Requires that each array element be accessible only once and cannot use secondary storage space

Solving methods

Because the topic requires that each array element can only be accessed once, without secondary storage space, you can start with the principle of mathematical summation, because only one number repeats once, and the number is continuous, according to the summation and principle, the sum of all the items of the array, and then minus 1 to N-1, that is, the number of repetitions.

Extension One: does not require each array to be accessed only once, cannot use the secondary storage space

If the topic does not require that each array element be accessible only once, without secondary storage space, it can also be solved using XOR and bitmap methods.

    • Different or law

      According to the method of calculation of XOR, each two different number performs the XOR operation, the result is 1, each two identical number is different or after, the result is 0, so the N number in array a[n] is different or the result with 1 to N-1 is different or, the obtained value is the request.

      To solve the problem of contrast with a different or ingenious solution

    • Bitmap method

The principle of the bitmap method is to first apply for a length of N-1 and all are ' 0 ' a string, and then iterate through the array a[n], take each array element A[i] value, the corresponding string in its corresponding position 1, if already set 1, then the number is the number of repetitions. Because the bitmap method is used, the space complexity is large, O (n).

Extended Two

This problem takes a variant: The value is "1,n-1" an array of integers with n elements, at least one repetition number, that is, there may be multiple repetitions, and O (n) time to find any one of the repetitions. For example, array "" = {1,2,2,4,5,4}, then 2 and 4 are repeating elements.

    • Bitmap method. Use the size of the N bitmap to record whether each element has occurred, and once it encounters an element that has already appeared, it is output directly. Time complexity is O (n), spatial complexity is O (n)

    • Array sorting method. First, the array is counted, and then it is scanned in sequence, until an element that has occurred is encountered and output directly. The time complexity is O (n) and the spatial complexity is O (n)

    • Hash method

      The above two scenarios require additional storage space, can not use additional storage space? If the array element is a signed int, this method is feasible. The array element as an index, for element Array[i], if array[array[i]] is greater than 0, set array[array[i]] =-array[array[i], or if array[array[i]] is less than 0, then array[ Array[i]] is a repeating number

Data structures and algorithms: arrays (i)

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.