各种排序算法汇总

选择排序

  • 每次都选择一个最值,放入以排序空间内,经过n次,就排序结束
  • 每次选择最值的时候,时间复杂度 O ( n ) O(n) O(n),所以选择排序的时间复杂度 是 O ( n 2 ) O(n^2) O(n2)
  • 优点是: 交换次数最少,是稳定排序
  • 最好/最坏时间复杂度都是 O ( n 2 ) O(n^2) O(n2)
void select_sort(vector<int>& nums){
	for(int i = 0; i < nums.size() - 1; ++i){
		int min_index = i;
		for(int j = i  +1; j < nums.size(); ++j){
			if(nums[j] < nums[min_index])
				min_index = j;
		}
		std::swap(nums[i],nums[min_index]);
	}
}

插入排序

  • 最好的情况: O ( n ) O(n) O(n)
  • 最差的情况: O ( n 2 ) O(n^2) O(n2)
void insert_sort(vector<int>& nums){
        for(int i = 1; i < nums.size();++i){
            int j = i;
            int temp = nums[i];
            j--;
            while(j>=0 && nums[j] > temp){
                nums[j+1] = nums[j];
                j--;
            }
            nums[j+1] = temp;
        }
    }

快速排序

  • 类似归并排序,也是分治思想算法。快排: 选取一个元素作为基准pivot,把array按照pivot分为2部分。
  • 依据不同的基准元素选择方法,快速排序也有不同的版本
    • 总是选择第一个元素作为基准
    • 总是选择最后一个元素作为基准
    • 随机选择一个元素作为基准
    • 选择中间元素作为基准
  • 时间复杂度: 最差情况 O ( n 2 ) O(n^2) O(n2) ,最好情况 O ( n log ⁡ 2 n ) O(n\log_2n) O(nlog2n)
// [l,r]
int partition(vector<int>& nums,int l,int r){
	// select nums[r] as pivot
	int pivot = nums[r];
	int j = l-1;
	for(int i = l; i < r;++i){
		if(nums[i] < pivot){
			j++;
			std::swap(nums[j],nums[i]);
		}
	}
	j++;
	std::swap(nums[j],nums[r]);
	return j;
}
void quick_sort(vector<int>&nums,int l,int r){
	if(l >= r) return;
	int j = partition(nums,l,r);
	quick_sort(nums,l,j-1);
	quick_sort(nums,j+1,r);
}

3-Way QuickSort

  • 在3-Way QuickSort中,数组被分为3部分:
    • arr[l … i] : 元素都小于pivot
    • arr[i+1 … j-1] 元素都等于pivot
    • arr[j … r] 元素都大于pivot
/* This function partitions a[] in three parts 
   a) a[l..i] contains all elements smaller than pivot 
   b) a[i+1..j-1] contains all occurrences of pivot 
   c) a[j..r] contains all elements greater than pivot */
void partition(int a[], int l, int r, int &i, int &j) 
{ 
    i = l-1, j = r; 
    int p = l-1, q = r; 
    int v = a[r]; 
    
    while (true) 
    { 
        // From left, find the first element greater than 
        // or equal to v. This loop will definitely terminate 
        // as v is last element 
        while (a[++i] < v); 
  
        // From right, find the first element smaller than or 
        // equal to v 
        while (v < a[--j]) 
            if (j == l) 
                break; 
  
        // If i and j cross, then we are done 
        if (i >= j) break; 
  
        // Swap, so that smaller goes on left greater goes on right 
        swap(a[i], a[j]); 
  
        // Move all same left occurrence of pivot to beginning of 
        // array and keep count using p 
        if (a[i] == v) 
        { 
            p++; 
            swap(a[p], a[i]); 
        } 
  
        // Move all same right occurrence of pivot to end of array 
        // and keep count using q 
        if (a[j] == v) 
        { 
            q--; 
            swap(a[j], a[q]); 
        } 
    } 
  
    // Move pivot element to its correct index 
    swap(a[i], a[r]); 
  
    // Move all left same occurrences from beginning 
    // to adjacent to arr[i] 
    j = i-1; 
    for (int k = l; k < p; k++, j--) 
        swap(a[k], a[j]); 
  
    // Move all right same occurrences from end 
    // to adjacent to arr[i] 
    i = i+1; 
    for (int k = r-1; k > q; k--, i++) 
        swap(a[i], a[k]); 
} 
  
// 3-way partition based quick sort 
void quicksort(int a[], int l, int r) 
{ 
    if (r <= l) return; 
  
    int i, j; 
  
    // Note that i and j are passed as reference 
    partition(a, l, r, i, j); 
  
    // Recur 
    quicksort(a, l, j); 
    quicksort(a, i, r); 
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值