Bubble sort of sorting + use of the metronome

Bubble sorting process: In each loop, the largest number traversed in this loop is placed at the end of the array by swapping, and then the loop range is reduced by 1 to finally complete the sorting. The time complexity is O (n ^ 2), and the extra space complexity is O (1).

void BubbleSort(int* &arr,int n)
{

    if( arr == NULL|| n<=1 ) return ;

    for(int i=n-1;i>0;i--)
    {
        for(int j=0;j<i;j++)
        {
            if(arr[j]>arr[j+1])
                Swap(arr[j],arr[j+1]);
        }
    }
}

 

void Swap(int &a,int &b)
{
    if(a==b) return;
    a = (a) ^ (b);
    b = (a) ^ (b);
    a = (a) ^ (b);
}

 

Countermeasure: You can use a randomly generated array to find out whether there is an error in the program through multiple input samples. You can customize the boundary data and the amount of test data. It is important to write a completely correct program in the countermeasure program, that is, it does not consider space-time complexity, but requires absolute results. You can modify the pairing program first, and then modify the source program through the artificial verification data of the pairing device.

The following is the pairing program for array sorting:

#include<iostream>
#include<algorithm>
#include<time.h>
using namespace std;

//数组对拍--排序

int realsize;
void Swap(int &a,int &b)
{
    if(a==b) return;
    a = (a) ^ (b);
    b = (a) ^ (b);
    a = (a) ^ (b);
}

int *CreateRandomArr(int maxsize,int maxvalue)
{
    srand((unsigned)time(NULL));
    realsize=rand()%maxsize;
    //int realvalue=random()%maxvalue;
    int *arr=(int *)malloc(sizeof(arr)*realsize);
    for(int i=0;i<realsize;i++)
    {
        arr[i]=rand()%maxvalue;
    }
    return arr;
}

//自定义冒泡排序void Comparator(int* &arr,int n)
{    
    if( arr ==NULL || n<=1 ) return;
    else sort(arr,arr+n);
}


int * copyArr(int *a,int n)
{
    if( a == NULL || n<=0) return NULL;
    int *arr=(int *)malloc(sizeof(int)*n);
    for(int i=0;i<n;i++)
    {
        arr[i]=a[i];
    }
    return arr;
}

void printArr(int* arr,int n)
{
    if( arr ==NULL || n<=0 )return;
    for(int i=0; i<n; i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

bool isEqual(int *a,int alen,int *b,int blen)
{
    if(alen!=blen) return false;
    if(alen == 0 || blen==0 ) return false;
    if(alen==0 || blen==0) return false;
    for(int i=0;i<alen;i++)
    {
        if(a[i]!=b[i])
        {
            printArr(a,alen);
            printArr(b,blen);
            return false;
        }
    }
    return true;
}


int main()
{
    int maxsize=500;
    int maxvalue=1000;
    int testtime=1000;
    
    bool flag=true;
    for(int i=0;i<testtime;i++)
    {
        int *arr=CreateRandomArr(maxsize,maxvalue);
        int *barr=copyArr(arr,realsize);

        BubbleSort(arr,realsize);
        Comparator(barr,realsize);

        if(!isEqual(arr,realsize,barr,realsize))
        {
            flag=false;
            break;
        }
    }
    cout<<(flag==true?"nice":"fuck fucked")<<endl;
    int *a=CreateRandomArr(maxsize,maxvalue);
    BubbleSort(a,realsize);
    printArr(a,realsize);

}

 

Guess you like

Origin www.cnblogs.com/RainzzZ/p/12743744.html