各种排序算法的实现

1.冒泡排序

class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        // write code here
        for(int i=0;i<n-1;i++)
            {
            for(int j=0;j<n-i-1;j++)
                {
                if(A[j+1]<A[j])
                    swap(A[j],A[j+1]);
            }
        }
        return A;
    }
};
2.选择排序
class SelectionSort {
public:
    int* selectionSort(int* A, int n) {
        // write code here
        for(int i=0;i<n;i++)
            {
            int temp=i;
            for(int j=i+1;j<n;j++)
                {
                if(A[j]<A[temp])
                    {
                    temp=j;
                }
            }
            swap(A[temp],A[i]);
        }
        return A;
    }
};
3.插入排序
class InsertionSort {
public:
    int* insertionSort(int* A, int n) {
        // write code here
        for(int i=1;i<n;i++)
            {
            if(A[i]<A[i-1])
                {
                int temp=A[i];
                int j=i;
                while(j>0&&A[j-1]>temp)
                    {
                    A[j]=A[j-1];
                    j--;
                }
                A[j]=temp;
            }
        }
        return A;
    }
};
4归并排序


class MergeSort {
public:
    int* mergeSort(int* A, int n) {
        // write code here
        merge(A,0,n-1);
        return A;
    }
    void merge(int *A,int l,int r)
    {
    	if(l<r)
    	{
    		int mid=(l+r)/2;
    		merge(A,l,mid);
    		merge(A,mid+1,r);
    		Sort(A,l,mid,r);
    	}
    }
    void Sort(int *A,int l,int mid,int r)
    {
    	int temp[r-l+1];
    	int x=l,y=mid+1;
    	int t=0;
    	while(x<=mid&&y<=r)
    	{
    		if(A[x]<A[y])
    		{
    			temp[t++]=A[x];
    			x++;    			
    		}
    		else
    		{
    			temp[t++]=A[y];
    			y++;
    		}
    	}
    	while(x<=mid)
    	{
    		temp[t++]=A[x];
            x++;
    	}
    	while(y<=r)
    	{
    		temp[t++]=A[y];
            y++;
    	}
    	for(int i=l;i<=r;i++)
    	{
    		A[i]=temp[i-l];
    	}
    }
};
5.快速排序

class QuickSort {
public:
    int* quickSort(int* A, int n) {
        // write code here
        quicksort(A,0,n-1);
        return A;
    }
    void quicksort(int *A,int l,int r)
    {
    	if(l<r)
    	{
    		int i=l,j=r,x=A[l];
    	while(i<j)
    	{
    		while(i<j&&A[j]>=x)
    			j--;
    		if(i<j)
    			A[i++]=A[j];
    		while(i<j&&A[i]<x)
    			i++;
    		if(i<j)
    			A[j--]=A[i];
    	}
    	A[i]=x;
    	quicksort(A,l,i-1);
    	quicksort(A,i+1,r);
    	}
    }
};

6.堆排序

class HeapSort {
public:
    int* heapSort(int* A, int n) {
        // write code here
        for(int i=(n-1)/2;i>=0;i--)
        {
        	Adjust(A,i,n);
        }
        for(int i=n-1;i>0;i--)
        {
        	swap(A[i],A[0]);
        	Adjust(A,0,i);
        }
        return A;
    }
    void Adjust(int* A,int x,int n)
    {
    	int temp,child;
    	for(temp=A[x];x*2+1<n;x=child)
    	{
    		child=x*2+1;
    		if(child+1<n&&A[child+1]>A[child])
    			child++;
    		if(temp<A[child])
    			A[x]=A[child];
    		else
    			break;
    	}
    	A[x]=temp;
    }
};

7.希尔排序

class ShellSort {
public:
    int* shellSort(int* A, int n) {
        // write code here
        int i,j,gap;
        for(gap=n/2;gap>0;gap/=2)
        {
        	for(int i=gap;i<n;i++)
        	{
        		for(int j=i-gap;j>=0&&A[j]>A[j+gap];j-=gap)
        			swap(A[j],A[j+gap]);
        	}
        }
        return A;
    }
};










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没想好叫什么名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值