One dimensional array learning summary

Posted by acemods on Sun, 31 Oct 2021 04:11:30 +0100

This week I learned about one-dimensional arrays, which can be divided into the following parts

1. Array definition: variable type array name [array length]

2. Classical algorithms: (1) selection sorting method

                      (2) Bubble sorting

                      (3) Array shift

                      (4) Hash array statistics like, find duplicate values

                      (5) Binary search method

(1) Select sort method (ascending or descending)

Idea: arrange a group of unordered numbers in order from large to small (from small to large)

  Code implementation:

#include<stdio.h>
#define MAXN 20
int main(){
    int n;
    scanf("%d",&n);
    int i,a[MAXN];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }    
    int k,index;
    for(k=0;k<n-1;k++){
        index=k;
        for(i=k+1;i<n;i++){//Each number is compared with all subsequent numbers 
            if(a[i]<a[index]){
                index=i;
            }
        }
        int temp=a[index];//Intermediate exchange t=a,a=b,b=t
        a[index]=a[k];
        a[k]=temp;
    }
    for(i=0;i<n;i++){
        printf("%d ",a[i]);
    }
    return 0;
    
}

(2) Bubble sorting

Idea: exchange between two adjacent numbers (ascending and descending)

Example of process diagram (from station B)

 

Ascending order example: compare the size of two adjacent numbers in each trip. If the previous digit is greater than the next digit, the position will be exchanged
After the previous step, the maximum number in the array has sunk to the lowest end, and the next comparison can be reduced once (5 12 6 2 23)
... and so on (2 5 6 12 23)

Code implementation:

#include<stdio.h>
#define MAXN 20
int main(){
    int n;
    scanf("%d",&n);
    int i,a[MAXN];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }    
    int j;
    for(i=0;i<n-1;i++){
        for(j=0;j<n-1-i;j++){//The number of next comparisons is reduced once per trip 
            if(a[j]>a[j+1]){
                int temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    for(i=0;i<n;i++){
        printf("%d ",a[i]);
    }    
    return 0;
}

 

  (3) Array shift:

#include<stdio.h>
#define MAXN 20
void shift(int n,int a[],int m);
int main(){
    int n;
    scanf("%d",&n);
    int i,a[MAXN];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }    
    int m;
    scanf("%d",&m);//Number of moves
    shift(n,a,m); 
    for(i=0;i<n;i++){
        printf("%d ",a[i]);
    }    
    return 0;
}
void shift(int n,int a[],int m){
    int i;
    int k,temp;
    for(k=0;k<m;k++){
        temp=a[0];    //Because the first step at the beginning of the cycle will a[1]Endow a[0],a[0]The value of has been changed and needs to be adjusted a[0]Make special treatment 
        for(i=0;i<n;i++){
            a[i]=a[i+1];
        }
        a[n-1]=temp;
    }
}

 

(4) Array statistics praise: (take the title on pta as an example)

 

Code implementation:  

#include<stdio.h>
#define MAXN 20
int main(){
    int n;
    scanf("%d",&n);
    int i;
    int date;
    int b[10]={0};//
    for(i=0;i<n;i++){
        scanf("%d",&date);//Count votes as Subscripts 
        b[date]++;
    } 
    for(i=1;i<=8;i++){
        printf("%d %d\n",i,b[i]);
    } 
    return 0;
}

Similar to hash array:

Important: the newly built array needs to be initialized first

Advantages: there is no need to calculate the addition one by one, reducing the complexity

Disadvantages: when the number of inputs is large, the range of the newly constructed array cannot be determined, and segment errors are easy to occur on pta

(5) Binary search method:

(limited to ordered arrays, unordered arrays should be sorted first)

Code implementation:

#include<stdio.h>
#define MAXN 20
int search(int n,int a[],int x);
int main(){
    int n,x;
    scanf("%d %d",&n,&x);
    int a[MAXN],i;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    } 
    int result=search(n,a,x);
    if(result==-1){
        printf("not found");
    }
    else
    printf("%d",result);
    return 0;
}
int search(int n,int a[],int x){
    int left=0;
    int right=n-1;
    int result=-1;
    while(left<=right){
        int mid=(left+right)/2;
        if(a[mid]==x){
            result=mid;
            break;
        }
        else if(a[mid]<x){
            left=mid+1;
        }
        else{
            right=mid-1;
        }
    } 
    return result;
}

Important: find out the conditions for jumping out of the loop

There are few test cases for the above code, and errors may occur. Please correct

Conclusion: if you have a thorough understanding of the classical algorithm of array, you can draw inferences from one example. The practice of one question can be deformed and applied to another, but one or two points will always be deducted for the questions on pta, and the code should be more rigorous.