面试官问:能手写一个非递归实现的快排吗?

简介: 快速排序算法是一个典型的采用分治思想的算法,时间复杂度为O(nlogn),下面先来看看它的递归实现:

Java版本


import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {9,8,7,6,5,4,3,2,1};
        quickSort(a,0,a.length-1);
        System.out.println(Arrays.toString(a));
    }
    public static void quickSort(int[] a,int start,int end){
        if (start < end){
            int mid = partion(a,start,end);
            qSort(a,start,mid-1);
            qSort(a,mid+1,end);
        }
    }
    public static int partion(int[] a,int start,int end){
        int x = a[start],j = start;//选择第一个作为基准
        //使得在区间[start,end]所有小于x的位于x的左边,大于x的位于右边
        for (int i = start+1; i <=end; i++) {
            if (a[i] < x){
                j++;
                swap(a,i,j);
            }
        }
        swap(a,start,j);
        return j;
    }
    public static void swap(int[] a,int i,int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    
}

C++版本

#include <bits/stdc++.h>
#include <algorithm>
#define GET_ARRAY_LEN(array,len){len = (sizeof(array) / sizeof(array[0]));}
using namespace std;

int partion(int a[],int start,int end){
    int x = a[start],j = start;
    for (int i = start + 1; i <= end; ++i) {
        if (a[i] < x){
            j++;
            swap(a[i],a[j]);
        }
    }
    swap(a[start],a[j]);
    return j;
}

void quickSort(int a[],int start,int end){
    if (start < end){
        int mid = partion(a,start,end);
        quickSort(a,start,mid - 1);
        quickSort(a,mid + 1,end);
    }
}


int main() {
    int a[] = {9,8,7,6,5,4,3,2,1};
    int len;
    GET_ARRAY_LEN(a,len);
    quickSort(a,0,len - 1);
    for (int i = 0; i < len; ++i) {
        cout << a[i] << " ";
    }
    cout <<endl;
    return 0;
}

其中核心的方法为partion,算法中主要通过该方法对数组元素进行了交换,每调用一次partion方法,都会使得你所选择的基元元素x的左边均小于x,右边均大于x
下面给出非递归的实现代码

Java版本

import java.util.ArrayDeque;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {9,8,7,6,5,4,3,2,1};
        quickSort(a);//非递归
        System.out.println(Arrays.toString(a));
    }
    
    public static int partion(int[] a,int start,int end){
        int x = a[start],j = start;
        for (int i = start+1; i <=end; i++) {
            if (a[i] < x){
                j++;
                swap(a,i,j);
            }
        }
        swap(a,start,j);
        return j;
    }
    public static void swap(int[] a,int i,int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    public static void quickSort(int[] a){
        int len;
        if (a == null || (len = a.length - 1) == 0) return;
        ArrayDeque<Section> stack = new ArrayDeque<>();
        Section section = new Section(0,len);
        stack.addLast(section);
        while (!stack.isEmpty()){
            section = stack.removeLast();
            int mid = partion(a,section.start,section.end);
            if (section.start < mid - 1) stack.addLast(new Section(section.start,mid - 1));
            if (mid + 1 < section.end) stack.addLast(new Section(mid + 1,section.end));
        }
    }
}

//区间类
class Section{
    int start,end;

    public Section(){}
    public Section(int start,int end){
        this.start = start;
        this.end = end;
    }
}

C++版本

#include <bits/stdc++.h>
#include <algorithm>
#define GET_ARRAY_LEN(array,len){len = (sizeof(array) / sizeof(array[0]));}
using namespace std;

//区间
struct Section{
    int start;
    int end;
};


int partion(int a[],int start,int end){
    int x = a[start],j = start;
    for (int i = start + 1; i <= end; ++i) {
        if (a[i] < x){
            j++;
            swap(a[i],a[j]);
        }
    }
    swap(a[start],a[j]);
    return j;
}

void quickSort(int a[],int len){
    int start = 0,end = len - 1;
    stack<Section> stk;
    Section section;
    section.start = 0;
    section.end = end;
    stk.push(section);
    while (!stk.empty()){
        section = stk.top();
        stk.pop();
        int mid = partion(a,section.start,section.end);
        Section temp;
        if (section.start < mid - 1){
            temp.start = section.start;
            temp.end = mid - 1;
            stk.push(temp);
        }
        if (mid + 1 < section.end){
            temp.start = mid + 1;
            temp.end = section.end;
            stk.push(temp);
        }
    }
}


int main() {
    int a[] = {9,8,7,6,5,4,3,2,1};
    int len;
    GET_ARRAY_LEN(a,len);
    quickSort(a,len);
    for (int i = 0; i < len; ++i) {
        cout << a[i] << " ";
    }
    cout <<endl;
    return 0;
}

非递归实现中,借助了一种数据结构——栈(后进先出),同时新增了一个区间类,用于存储需要排序的区间,核心方法还是partion

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

b17a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值