zoukankan      html  css  js  c++  java
  • 复习数据结构之简单排序(快排,直插,希尔,选择,冒泡)

    写了一遍自己的模板, 增加熟练度

    学校不考复杂一些的排序,暂就写这几种

    #include <iostream>
    #define Size 1010
    using namespace std;
    
    class UpSort{
    	public:
    		UpSort(){
    			cin >> lenth;
    			for(int i = 1; i <= lenth; i ++) cin >> a[i];
    		}
    		void quickSort();			//快速排序 
    		void insertSort();			//直接插入排序
    		void shellSort();			//希尔排序 
    		void easySelectSort();		//简单选择排序
    		void bubbleSort1();			//冒泡排序
    		void bubbleSort2();			//冒泡排序 (优化版)
    		void print(); 
    		
    	private:
    		int a[Size];
    		int lenth;
    		void _quickSort(int l, int r);
    };
    
    void UpSort::print(){
    	for(int i = 1; i <= lenth; i ++){
    		cout << a[i] << " ";
    	}
    	cout << endl;
    }
    //快速排序 
    void UpSort::quickSort()
    {
    	_quickSort(1, lenth);
    }
    
    void UpSort::_quickSort(int l, int r)
    {
    	if(l >= r) return ;
    	
    	int mid = a[(l+r)>>1];
    	int i = l, j = r;
    	while(i < j)
    	{
    		while(a[i] < mid) i ++;
    		while(a[j] > mid) j --;
    		
    		if(i < j) swap(a[i], a[j]);
    	}
    	_quickSort(l, j);
    	_quickSort(j+1, r);
    }
    //直接插入排序
    void UpSort::insertSort()
    {
    	for(int i = 2; i <= lenth; i ++){
    		a[0] = a[i];	//记录当前数据(a[0]相当于temp) 
    		int j = i-1;
    		for( ; j > 0 && a[0] < a[j]; j --)
    		{
    			a[j+1] = a[j];	
    		}
    		a[j+1] = a[0];	//后撤一步进行插入 
    	}
    }
    //希尔排序 
    void UpSort::shellSort()
    {
    	for(int d = lenth>>1; d >= 1; d>>=1)
    	{
    		for(int i = d+1; i <= lenth; i ++){
    			a[0] = a[i];
    			int j = i-d;
    			for( ; j > 0 && a[0] < a[j]; j -= d)
    			{
    				a[j+d] = a[j];	
    			}
    			a[j+d] = a[0];
    		}
    	} 
    } 
    //简单选择排序
    void UpSort::easySelectSort()
    {
    	for(int i = 1; i < lenth; i ++)	//选择lenth-1次 
    	{
    		int maxSite = i;
    		for(int j = i+1; j <= lenth; j ++)
    			if(a[j] < a[maxSite])	//找当前集合的最小
    				maxSite = j;
    		if(maxSite != i) swap(a[i], a[maxSite]); 
    	}
    }
    //冒泡排序
    void UpSort::bubbleSort1()
    {
    	for(int i = 1; i < lenth; i ++){
    		for(int j = 1; j <= lenth-i; j ++){
    			if(a[j] > a[j+1]){
    				swap(a[j], a[j+1]);
    			}
    		} 
    	}
    } 
    //冒泡排序 (优化版)
    void UpSort::bubbleSort2()
    {
    	int end = lenth;
    	for(int i = 1; i < lenth; i ++){
    		int tempEnd = 0;
    		for(int j = 1; j != end; j ++){
    			if(a[j] > a[j+1]){
    				swap(a[j], a[j+1]);
    				tempEnd = j;	//最后一次交换 
    			}
    		} 
    		end = tempEnd;			//更新最后一次交换的位置,之后不再涉足 
    	}
    } 
    
    int main()
    {
    	//快速排序 
    //	UpSort sort_byquick;
    //	sort_byquick.quickSort();
    //	sort_byquick.print();
    
    	//直接插入排序
    //	UpSort sort_byInsert;
    //	sort_byInsert.insertSort();
    //	sort_byInsert.print();
    
    	//希尔排序
    //	UpSort sort_byShell;
    //	sort_byShell.shellSort();
    //	sort_byShell.print();
    
    	//简单选择排序
    //	UpSort sort_byEasySelect;
    //	sort_byEasySelect.easySelectSort();
    //	sort_byEasySelect.print();
    
    	//冒泡排序 
    //	UpSort sort_byBubble1;
    //	sort_byBubble1.bubbleSort1();
    //	sort_byBubble1.print();	
    
    	//冒泡排序 (优化版)
    	UpSort sort_byBubble2;
    	sort_byBubble2.bubbleSort2();
    	sort_byBubble2.print();
    	
    	return 0;
    }
    /*
    10
    7 4 9 2 0 8 -2 15 11 2
    */ 

  • 相关阅读:
    OpenCV (一)图片读取、修改、保存
    用matplotlib进行数据分析(可视化)
    笔记
    vrpano
    四、threejs——模型交互
    三、threejs不显示英文就是乱码情况,中文解决方案
    二、GLTF模型支持
    一、threejs————灯光阴影
    node的httpserver简单创建
    em和rem区别
  • 原文地址:https://www.cnblogs.com/Knight02/p/15799009.html
Copyright © 2011-2022 走看看