iOS basic algorithm

using namespace std;
template <typename T> //Template function (generic)
/*
 Selection sort
 **/
void selectionSort ( T arr[] , int n ){
    
    //  10, 39, 8, 7, 6, 5, 4, 3, 2, 1
    //Take out the minimum value first and then exchange
    
    for ( int i  = 0 ;  i < n ; i ++ ){
        
        int minIndex = i;
        
        for ( int j = i + 1 ; j < n ; j ++ ){
            
            if( arr[j] < arr[minIndex]){
                minIndex = j;
            }
        }
        
        swap( arr[i] , arr[minIndex] );  //Exchange function
    }
}
/*
 Insertion sort
 **/
template <typename T> //Template function (generic)
void insertSort (T arr[] , int n ){
    
    //  10, 39, 8, 7, 6, 5, 4, 3, 2, 1
    //Take out the second value and exchange it with the first value,
    //Take out the third value, exchange with the second value, and exchange with the first value
    
    for ( int i = 1 ; i < n ; i ++ ){
        
        for( int j = i ; j > 0  ; j -- ){
            if( arr[j] < arr[j-1]){
                swap( arr[j] , arr[j-1] );
            }else{
                break;
            }
            
        }
        
    }
}
/*
 Optimize insertion sorting
 **/
template <typename T> //Template function (generic)
void insertOptimizeSort (T arr[] , int n ){
    
    //  10, 39, 8, 7, 6, 5, 4, 3, 2, 1
    //Take out the second value, copy it, and compare it with the first value. If you move the first value
    //Take out the third value, make a copy, and then compare with the second value. If you move the second value, then compare with the first value
    
    for ( int i = 1 ; i < n ; i ++ ){
        
        T e = arr[i];
        int j;
         for(j = i ; j > 0  ; j -- ){
            
            if( arr[j-1] >e){
                
                arr[j] = arr[j-1];
            }else{
                break;
            }
            
        }
        arr[j] = e;
    }
}
/*
 Bubble sort
 **/
template <typename T> //Template function (generic)
void bubblingSor(T arr[] , int n){
    
    //Take out the first number and the second number for comparison. If the first number is large, exchange until the largest number is put at the end
    //Repeat the comparison in the first step, so as to make a circular comparison
    //The first loop places the largest number at the end, and the second loop places the second digit smaller than the largest number at the second to last
    for( int i = 0 ; i < n ; i ++ ){
        
        for( int j = 0 ; j < n ; j ++){
            if(arr[i] < arr[j]){
                swap(arr[i], arr[j]);
            }
        }
        
    }
}
/*
 Optimize bubble sorting
 **/
template <typename T> //Template function (generic)
void bubblingOptimizeSor(T arr[] , int n){
    
    bool swappedn;
    do {
        swappedn = false;
        for( int i = 1; i < n ; i ++ ){
            
            if( arr[i-1] > arr[i] ){
                swap(arr[i-1], arr[i]);
                swappedn = true;
            }
        }
        n --;
    } while (swappedn);
  
}

I saw the video at the weekend and found that it was very good, so I wrote it down on the blog and basically understood it. I think the algorithm is a way of solving problems. When I was studying high mathematics, I always thought about the situation of the century to solve it. It's not very difficult to learn it. I haven't practiced for several years. I forgot it. I can come back after reviewing it. So I finished reading an article Or I think that all good things need to be noted, so that one day my skill tree will become more and more extensive. For a very simple example, ask you to realize simple things, one day you can continue to realize them, one week you can, I personally think it's one week, this week you need to carefully think about the best plan, the best plan you think is the most valuable for the scene and your ability Reflect. The value of one's own ability needs to be considered. This cycle lasts for 5 days, which is the most beautiful, basically the same

Keywords: iOS

Added by davespring on Sun, 01 Dec 2019 05:48:07 +0200