• 七大排序


    七大排序算法

    冒泡排序

    选择排序

    插入排序

    堆排序

    归并排序

    快速排序

    希尔排序

    冒泡排序

    void bubbleSort(int *a, int length){
    	for(int i = 0; i < length; i++){
    		for(int j = i+1; j < length; j++){
    			if (a[i] > a[j]){
    				int temp = a[i];
    				a[i] = a[j];
    				a[j] = temp;
    			}
    		}
    	}
    }
    

    选择排序

    void  selectionSort(int *a, int length){
    	for(int i = 0; i < length; i++){
    		int k = i;
    		for(int j = i+1; j < length; j++){
    			if(a[k] > a[j]){
    				k = j;
    			}
    		}
    		int temp = a[i];
    		a[i] = a[k];
    		a[k] = temp;
    	}
    } 
    

    插入排序

    void insertSort(int *a, int length){
    	/*
    	for(int i = 1; i < length; i++){
    		for(int j = i; j >= 0; j--){
    			if(a[j] < a[j-1]){
    				int temp = a[j];
    				a[j] = a[j-1];
    				a[j-1] = temp;
    			}else{
    				break;
    			}
    		}
    	}*/
    	for(int i = 1; i < length; i++){
    		int it = a[i];
    		int index = i-1;
    		while(index >= 0 && it < a[index]){
    			a[index+1] = a[index];
    			index--;
    		}
    		a[index+1] = it;
    	}
    } 
    

    堆排序

    void headSort(int *a, int length){
    	
    } 
    

    归并排序

    void merage(int *a, int left, int center, int right){
    	int tp[right-left];
    	int begin = left;
    	int mid = center+1;
    	int index = 0;
    	while(begin <= center && mid <= right){
    		if(a[begin] >= a[mid]){
    			tp[index++] = a[mid++];
    		}else{
    			tp[index++] = a[begin++];
    		}
    	}
    	while(begin <= center){
    		tp[index++] = a[begin++];
    	}
    	while(mid <= right){
    		tp[index++] = a[mid++];
    	}
    	for(int i = left; i <= right; i++){
    		a[i] = tp[i-left];
    	}
    }
    void merageSort(int *a, int left, int right){
    	if(left >= right){
    		return;
    	}
    	int center = (left + right)/2;
    	merageSort(a, left, center);
    	merageSort(a, center+1, right);
    	merage(a, left, center, right);
    }
    

    快速排序

    //	快速排序
    void quickSort(int *a, int low, int high){
    	/*
    	int start = low;
    	int end = high;
    	int key = a[low];
    	while(end > start){
    		while(end > start && a[end] >= key){
    			end--;
    		}
    		if(a[end] <= key){
    			int temp = a[end];
    			a[end] = a[start];
    			a[start] = temp;
    		}
    		while(end > start && a[start] <= key){
    			start++;
    		}
    		if(a[start] >= key){
    			int temp = a[start];
    			a[start] = a[end];
    			a[end] = temp;
    		}
    	}
    	if(start > low){
    		quickSort(a, low, start-1);
    	}
    	if(end < high){
    		quickSort(a, end+1, high);
    	}*/
    	if(low >= high){
    		return;
    	}
    	int start = low;
    	int end = high-1;
    	int key = a[high];
    	while(end > start){
    		while(end > start && a[start] <= key){
    			start++;
    		}
    		while(end > start && a[end] >= key){
    			end--;
    		}
    		if(end > start){
    			int temp = a[start];
    			a[start] = a[end];
    			a[end] = temp;
    		}
    	}
    	if(start == end && a[start] > key){
    		a[high] = a[start];
    		a[start] = key;
    	}
    	if(start > low){
    		quickSort(a, low, start-1);
    	}
    	if(end < high){
    		quickSort(a, end+1, high);
    	}
    }
    

    希尔排序

    //	希尔排序 
    void shellSort(int *a, int length){
    	
    }
    

    主函数

    int main(){
    	int a[7] = {11, 14, 90, 34, 67, 12, 68};
    	quickSort(a, 0, 6);
    	//merageSort(a, 0, 6);
    	//insertSort(a, 7);
    	//selectionSort(a, 7);
    	//bubbleSort(a, 7);
    	for(int i = 0; i < 7; i++){
    		printf("%d ", a[i]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Java 基础巩固:装箱拆箱 你真的熟悉吗
    Java数据类型的转换:隐式(自动)转换与强制转换
    oozie学习笔记
    flume学习笔记
    sqoop学习笔记
    Oracle故障排查之oracle解决锁表问题
    zookerper安装部署
    hadoop HA架构安装部署(QJM HA)
    hadoop第一部分-安装、测试
    hadoop完全分布式安装部署-笔记
  • 原文地址:https://www.cnblogs.com/ice-image/p/12077460.html
Copyright © 2020-2023  润新知