zoukankan      html  css  js  c++  java
  • 排序

    #include <iostream>
    
    using namespace std;
    
    void QuickSort(int a[], int low, int high)//0--- n-1
    {
    	int i, j, flagNum;
    	if(low < high)
    	{
    		i = low;
    		j = high;
    		flagNum = a[low];
    		while(i < j)
    		{
    			while(i < j && a[j] >= flagNum)
    				j--;
    			if(i < j)
    				a[i++] = a[j];
    			while(i < j && a[i] <= flagNum)
    				i++;
    			if(i < j)
    				a[j--] = a[i];
    		}
    		a[j] = flagNum;
    		QuickSort(a, low, j-1);
    		QuickSort(a, j+1, high);
    	}
    }
    
    void bubbleSort(int a[], int n)
    {
    	for(int i = 0; i <= n;i++)
    	{
    		for(int j = n; j > i ;j--)
    		{
    			if(a[j] <= a[j-1])
    			{
    				int temp = a[j];
    				a[j] = a[j-1];
    				a[j-1] = temp;
    			}
    		}	
    	}
    }
    
    int InsertSort(int a[], int n)
    {
    	int i, j,temp;
    	for(i = 1; i <= n; i++)
    	{
    		temp = a[i];
    		for(j = i-1; j >=0 && temp <= a[j]; j--)
    		{
    			a[j+1] = a[j];
    		}
    		a[j+1] = temp;
    	}	
    }
    
    int SelectSort(int a[], int n)
    {
    	int i, j;
    	for(i = 0;i <= n; i++)
    	{
    		for(j = i+1; j <= n; j++)
    		{
    			if(a[i] >= a[j])
    			{
    				int temp = a[i];
    				a[i] = a[j];
    				a[j] = temp;	
    			}
    		}
    	}
    }
    
    int ShellSort(int a[], int n)
    {
    	int i, h, j, temp;
    	for(h = n/2; h > 0; h = h/2)
    	{
    		for(i = h; i <= n; i++)
    		{
    			temp = a[i];
    			for(j = i-h; j >=0 && temp <= a[j]; j = j-h)
    			{
    				a[j+h] = a[j];
    			}
    			a[j+h] = temp;
    		}
    	}
    }
    
    int main()
    {
    	int a[10] = {1, 2, 5, 3, 4, 6, 8, 7, 10, 9};
    	//QuickSort(a, 0, 9);
    	//bubbleSort(a, 9);
    	//InsertSort(a, 9);
    	//SelectSort(a, 9);
    	ShellSort(a, 9);
    	for(int i = 0; i < 10; i++)
    	{
    		cout << a[i] << " ";
    	}
    	cout <<endl;
    	
    	return 0;
    }
    

      

  • 相关阅读:
    docker入门
    IAR屏蔽警告的方法
    在MDK 中忽略(suppress) 某一个警告
    stm32 F40x CCM数据区的使用
    如何理解Stand SPI Dual SPI 和Quad SPI??
    stm32F1在sram中调试运行代码
    stm32低功耗的一点总结
    C语言中__attribute__ ((at())绝对定位的应用
    系统栈和任务栈——freertos
    软件模拟spi的注意事项
  • 原文地址:https://www.cnblogs.com/devinblog/p/4911487.html
Copyright © 2011-2022 走看看