【Algorithm Training: Linear Tables】Day 04

topic one

Removes all elements whose values ​​are duplicated from the sorted sequence table so that all elements have different values.

analyze

Idea one

  1. First, in an ordered list, all elements with duplicate values ​​are adjacent. To do this, we only need to sequentially access the elements in the sequence table, and find out the elements with duplicate values ​​and delete them.
  2. Therefore, we can set the two-dimensional variable array buzz to record the number of repeated elements. When traversing to the current element, compare the current element with the next element. If they are the same, add one to the buzz value. If not, add the number of repetitions of the next element.
  3. Finally, read the elements in the buzz array into the original sequence table to complete the topic requirements. The time complexity of this algorithm is O(n), and the space complexity is O(n).

Idea two

  1. According to the characteristics of the ordered sequence table, we can consider using the idea of ​​direct insertion sorting , use the zeroth element as a non-repeating element, compare the subsequent elements with the previous elements, if the values ​​are different, insert the sequence table, and update the comparison of non-repeating elements The base value is the current element. The time complexity is O(n), and the space complexity is O(1).

Code

The following code only implements the second idea

int LinearList::Question_06()
{
    
    
    if(arr.length <= 0)
        return -1;
    int i = 0,j = 1;
    for(i = 0,j= 1;j < arr.length;j ++)
    {
    
    
        if(arr.data[j] != arr.data[i])
            arr.data[++i] = arr.data[j];
    }
    arr.length = i + 1;
    return 0;
}

Effect

insert image description here

Topic two

The elements in the linear table (a1, a2, a3...,an) are in increasing order and stored in the computer in sequence. It is required to design an algorithm to complete the search for the element whose value is x in the table with the least amount of time, and if it is found, it will be exchanged with the position of the subsequent element. Otherwise, insert it into the table so that the elements in the table are still in ascending order.

analyze

  1. The topic involves element search and requires the shortest time. You can consider the algorithm idea of ​​half search :Divide the linear table into two, and compare the key value with the middle element. If they are equal, the search is successful. If not, the element must be in the left or right interval of the linear table. Repeat the above steps again, and when the length of the interval is equal to 1 (the search fails) or the search succeeds, the algorithm is launched.
  2. If the search is successful, the element is swapped with the next element. If the search fails, insert the key value at the last searched position just now to complete the question requirements. The time complexity is O(log2n), and the space complexity is O(1).

Code

Array LinearList::Question_09(int x)
{
    
    
    int key = x;
    //折半查找key
    int left = 0,right = arr.length-1,mid = 0;
    while(left <= right)
    {
    
    
        mid = (left+right)/2;
        cout<<"mid: "<<mid<<endl;
        if(arr.data[mid] == key)//若查找成功
        {
    
    
            int temp = arr.data[mid];
            arr.data[mid] = arr.data[mid+1];
            arr.data[mid+1] = temp;
            return arr;
        }
        if(arr.data[mid] > key)//key可能在左半区间
        {
    
    
            right = mid-1;
        }
        else if(arr.data[mid] < key)//key可能在右半区间
        {
    
    
            left = mid+1;
        }
    }

    //查找失败
    arr.length ++;
    for(int i = arr.length-1;i > left;i --)
    {
    
    
        arr.data[i] = arr.data[i-1];
    }
    arr.data[left] = key;
    return arr;
}

Effect

insert image description here

Supongo que te gusta

Origin blog.csdn.net/wddkxg/article/details/131605858
Recomendado
Clasificación