基础排序算法——汇总

1、冒泡排序:

C++ Code:

void BubbleSort(int* a,int n)  
{  
    int tmp;  
    for(int i=n;i>=1 ;i--)  
    {  
        for (int j = 0; j < i - 1; j++)  
        {  
            if (a[j] > a[j + 1]) //降序改为"" 
            {  
                tmp = a[j];  
                a[j] = a[j + 1];  
                a[j + 1] = tmp;  
            }  
        }  
    }  
} 
C# Code

public void BubbleSort(int []array)  
{  
    int temp;  
    int count = array.Length;  
    for(int i=count;i>=1 ;i--)  
    {  
        for (int j = 0; j < i - 1; j++)  
        {  
            if (array[j] > array[j + 1]) //降序改为"<" 
            {  
                temp = array[j];  
                array[j] = array[j + 1];  
                array[j + 1] = temp;  
            }  
        }  
    }  
}

public void BubbleSort(List<Int32> array)
{
    int temp;
    int count = array.Count;
    for (int i = count; i >= 1; i--)
    {
        for (int j = 0; j < i - 1; j++)
        {
            if (array[j] > array[j + 1])//降序改为"<" 
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}
Java Code

	public void bubbleSort(int []array)  
	{  
	    int temp;  
	    int count = array.length;  
	    for(int i=count;i>=1 ;i--)  
	    {  
	        for (int j = 0; j < i - 1; j++)  
	        {  
	            if (array[j] > array[j + 1])//降序改为"<"   
	            {  
	                temp = array[j];  
	                array[j] = array[j + 1];  
	                array[j + 1] = temp;  
	            }  
	        }  
	    }  
	}
	public void bubbleSort(List<Integer>array)  
	{  
	    int temp;  
	    int count = array.size();  
	    for(int i=count;i>=1 ;i--)  
	    {  
	        for (int j = 0; j < i - 1; j++)  
	        {  
	            if (array.get(j) > array.get(j + 1)) //降序改为"<"  
	            {  
	                temp = array.get(j);  
	                array.set(j, array.get(j + 1));  
	                array.set(j + 1,temp);  
	            }  
	        }  
	    }  
	}


2、插入排序

C++ Code

void InsertSort(int* a,int n)
{
	int j = 0,temp;
	for (int i = 1; i < n; i++)
	{
		temp = a[i];
		j = i;
		while (j > 0 && a[j - 1] >= temp)
		{
			a[j] = a[j - 1];
			j -= 1;
		}
		a[j] = temp;
	}
}


C# Code

public void InsertSort(int[] array)  
{    
    int j = 0,temp;    
    int count = array.Length;    
    for (int i = 1; i < count; i++)    
    {    
        temp = array[i];    
        j = i;    
        while (j > 0 && array[j - 1] >= temp)    
        {    
            array[j] = array[j - 1];    
            j -= 1;    
        }    
        array[j] = temp;    
        PrintArray(array);    
    }    
}  
Java Code

	public void InsertSort(int[] array)  
	{    
	    int j = 0,temp;    
	    int count = array.length;    
	    for (int i = 1; i < count; i++)    
	    {    
	        temp = array[i];    
	        j = i;    
	        while (j > 0 && array[j - 1] >= temp)    
	        {    
	            array[j] = array[j - 1];    
	            j -= 1;    
	        }    
	        array[j] = temp;   
	    }    
	} 
	public void InsertSort(List<Integer> array)  
	{    
	    int j = 0,temp;    
	    int count = array.size();    
	    for (int i = 1; i < count; i++)    
	    {    
	        temp = array.get(i);    
	        j = i;    
	        while (j > 0 && array.get(j - 1) >= temp)    
	        {    
	        	array.set(j, array.get(j-1));    
	            j -= 1;    
	        }    
	        array.set(j, temp);   
	    }    
	}


3、对半插入排序算法
C++ Code
void HalfInsertSort(int* a,int n)
{
	int j = 0, temp, low, high, mid;
	for (int i = 1; i < n; i++)
	{
		temp = a[i];
		low = 0;
		high = i - 1;
		while (low <= high)
		{
			mid = (low + high) / 2;
			if (temp < a[mid]) high = mid - 1;
			else low = mid + 1;
		}
		for (j = i - 1; j >= low; j--) a[j + 1] = a[j];
		a[low] = temp;
	}
}

C# Code
        public void HalfInsertSort(int[] array)
        {
            int j = 0, temp, low, high, mid;
            int count = array.Length;
            for (int i = 1; i < count; i++)
            {
                temp = array[i];
                low = 0;
                high = i - 1;
                while (low <= high)
                {
                    mid = (low + high) / 2;
                    if (temp < array[mid]) high = mid - 1;
                    else low = mid + 1;
                }
                for (j = i - 1; j >= low; j--) array[j + 1] = array[j];
                array[low] = temp;
            }
        }
Java Code
    public void HalfInsertSort(int[] array)
    {
        int j = 0, temp, low, high, mid;
        int count = array.length;
        for (int i = 1; i < count; i++)
        {
            temp = array[i];
            low = 0;
            high = i - 1;
            while (low <= high)
            {
                mid = (low + high) / 2;
                if (temp < array[mid]) high = mid - 1;
                else low = mid + 1;
            }
            for (j = i - 1; j >= low; j--) array[j + 1] = array[j];
            array[low] = temp;
        }
    }
    public void HalfInsertSort(List<Integer> array)
    {
        int j = 0, temp, low, high, mid;
        int count = array.size();
        for (int i = 1; i < count; i++)
        {
            temp = array.get(i);
            low = 0;
            high = i - 1;
            while (low <= high)
            {
                mid = (low + high) / 2;
                if (temp < array.get(mid)) high = mid - 1;
                else low = mid + 1;
            }
            for (j = i - 1; j >= low; j--) array.set(j+1,array.get(j));
            array.set(low, temp);
        }
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值