【算法】不稳定排序 选择排序 快速排序

不稳定排序有 选择排序 快速排序


#include<iostream>
using namespace std;
#define swap(a,b){\
    __typeof(a) _temp = a; a = b; b = _temp;\
}
//选择排序
void select_sort(int *num,int n){
    for(int i = 0; i < n - 1; i++){
        int ind = i;
        for(int j = i + 1 ; j < n ; j++){
            if(num[ind] > num[j]) ind =j;
        }
        swap(num[i] , num[ind]);
    }

    return;
}
//快速排序
void quick_sort(int *num, int l, int r){
    if(r <= l){
        return ;
    }
    int x = l, y = r, z = num [l];
    while(x < y){
        while(x < y && num[y] >= z) --y;
        if(x < y) num[x++]  = num[y];
        while(x < y && num[x] <= z) ++x;
        if(x < y) num[y--] = num[x];
    }
    num[x] = z;
    quick_sort(num, l ,x - 1);
    quick_sort(num, x + 1 , r);


}
java 实现
import java.util.Arrays;

public class Quicksort {
    public static void main(String[] args) {

        int[]  a  ={2,1,3,4,5,8,9};

        quicksort(a,0,6);

        System.out.println(Arrays.toString(a));

    }


   public static void  quicksort(int[] num, int l ,int r){
        if(r <= l){
            return;
        }
        int x = l , y = r , z = num[l];
        while(x < y){
            while(x < y && num[y] > z ) y--;
            if(x < y) num[x++] = num[y];
            while(x < y && num[x] < z) x++;
            if(x < y) num[y--] = num[x];
        }
        num[x] = z;
        quicksort(num,l ,x - 1 );
        quicksort(num, x + 1, r);

   }
}





int main(){
    int num[10]={2,3,1,4,7,5,6,8,9,0};

    //select_sort(num, 10);
    quick_sort(num,0,9);
    for(int i = 0; i < 10; i++){
        cout<<num[i]<<" ";
    } 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值