直通BAT面试算法精讲课2

对于一个int数组,请编写一个冒泡排序算法,对数组元素排序。

给定一个int数组A及数组的大小n,请返回排序后的数组。 

测试样例:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
冒泡: 依次比较相邻,大的放后面。
class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        // write code here
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(A[i]>A[j]){
                    swap(A[i],A[j]);

                }
            } 
        }
        return A;
    }
};

 

直通BAT面试算法精讲课2

class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        // write code here
        for(int i=0;i<n;i++){
            for(int j=n-1;j>i;j--){
                if(A[j]<A[j-1]){
                    int tmp = A[j];
                    A[j]=A[j-1];
                    A[j-1]=tmp;
                }
            }
        }
        return A;
    }
};

View Code

直通BAT面试算法精讲课2

class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        int i,j;
        for(i=0;i<n-1;i++){
            for(j=i+1;j<n;j++)
                {
                if(A[i] > A[j])
                    {
                    int temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
            }
        }

          return A;  

        // write code here
    }
};

View Code

 选择:选择最小放第一,第二小放第二。

直通BAT面试算法精讲课2

class SelectionSort {
public:
    int* selectionSort(int* A, int n) {
        // write code here
        for(int i=0;i<n-1;i++){
            int minIndex=i;
            for(int j=i+1;j<n;j++){
                if(A[j]<A[minIndex]){
                    minIndex=j;
                }
            }
            if(minIndex!=i){
                int temp=A[i];
                A[i]=A[minIndex];
                A[minIndex]=temp;
            }         
        }
        return A;
    }
};

View Code

 

class SelectionSort {
public:
    int* selectionSort(int* A, int n) {
        // write code here

        for(int i=0; i<n; i++){
            int min = A[i];
            int idx = i;
            for(int j=i+1;j<n;j++){
                if(A[j]<min){
                    min = A[j];
                    idx = j;
                }  
            }
            if(idx != i)
               swap(A[i],A[idx]);
        }
        return A;
    }
};

 插入: 从第二个开始,小于就交互, 内部维护一个冒泡。

class InsertionSort {
public:
    int* insertionSort(int* A, int n) {
        // write code here
        for(int i=1;i<n;i++){
            for(int j=i-1; j>=0; j--){  
                if(A[j+1]<A[j]){
                    swap(A[j+1],A[j]);
                }
            }
        }
        return A;


    }
};

归并排序: 本质上是一个递归的调用。 递归拆分然后合并。每个归并的子集都是有序的。

class MergeSort {
public:
    int* mergeSort(int* A, int n) {
        // write code here
        if( n==1){
            return A;
        }
        __mergeSort(A,0,n-1);
        return A;
    }
    //[l,r]
    void __mergeSort(int* A,int l,int r){
        if(l>=r){
            return;
        }
        int mid = (l+r)/2;
        __mergeSort(A,l,mid);
        __mergeSort(A,mid+1,r);
        __merge(A,l,mid,r);

    }
    //将[l ,mid] [mid,r]进行归并
    void __merge(int* A, int l,int mid,int r){
        int aux[r-l+1];  //使用临时变量记录数据。
        for (int i=l; i<=r; i++){
            aux[i-l] = A[i];
        }

        int i=l; int j = mid + 1;
        for(int k =l;k<=r; k++ ){
            if(i>mid){
                A[k]=aux[j-l];  //留意l的偏移量。
                j++;
            }else if(j>r){
                A[k] = aux[i-l];
                i++;
            }
            else if(aux[i-l]<aux[j-l]){
                A[k]= aux[i-l];
                i++;
            }else{
                A[k]= aux[j-l];
                j++;
            }
        }
    }
};

 快速排序

// 对arr[l...r]部分进行partition操作
// 返回p, 使得arr[l...p-1] < arr[p] ; arr[p+1...r] > arr[p]
template <typename T>
int __partition(T arr[], int l, int r){

    T v = arr[l];

    int j = l; // arr[l+1...j] < v ; arr[j+1...i) > v
    for( int i = l + 1 ; i <= r ; i ++ )
        if( arr[i] < v ){
            j ++;
            swap( arr[j] , arr[i] );
        }

    swap( arr[l] , arr[j]);

    return j;
}

// 对arr[l...r]部分进行快速排序
template <typename T>
void __quickSort(T arr[], int l, int r){

    if( l >= r )
        return;

    int p = __partition(arr, l, r);
    __quickSort(arr, l, p-1 );
    __quickSort(arr, p+1, r);
}

template <typename T>
void quickSort(T arr[], int n){

    __quickSort(arr, 0, n-1);
}

 

原文链接: https://www.cnblogs.com/yuguangyuan/p/6127748.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    直通BAT面试算法精讲课2

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/397859

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月11日 上午9:47
下一篇 2023年4月11日 上午9:48

相关推荐