简体   繁体   English

实现混合插入和快速排序C ++

[英]Implement hybrid insertion and quick sort C++

I'm trying to implement a hybrid insertion and quick sort. 我正在尝试实现混合插入和快速排序。 The task is to use quicksort for large vectors and whenever the vectors become smaller than some specified value (crossover point) the algorithm should switch to insertion sort. 任务是对大型向量使用快速排序,每当向量变得小于某个指定值(交叉点)时,算法应切换到插入排序。 I have this code so far but it does not sort vectors and I don't know why. 到目前为止,我已经有了这段代码,但是它没有对向量进行排序,我也不知道为什么。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

#include <iostream>
#include "console.h"
#include "vector.h"  // for Vector
#include <cmath>
using namespace std;


/* Partition for quicksort algorithm */
int partition(Vector<int> &vec, int start, int end){
    int lh = start +1;
    int rh = end;
    int pivotVal = vec[start];

    while (true){
        while (lh<rh && vec[lh]<pivotVal) lh++;
        while (rh>lh && vec[rh]>=pivotVal) rh--;
        if (lh==rh) break;
        swap(vec[lh], vec[rh]);
    }

    if (pivotVal<vec[lh]) return start;
    swap(vec[start], vec[lh]);
    return lh;
}


/* Regular quicksort */
void quickSort(Vector<int> &vec, int start, int end){
    if(start<end){
        int pivotIndex = partition(vec, start, end);
        quickSort(vec, start, pivotIndex-1);
        quickSort(vec, pivotIndex+1, end);
    }
}


/* Insertion sort algorithm */
void insertionSort(Vector<int> &vec, int start, int end){
    int size = vec.size();
    for (int i=start; i<end; i++){
        int j=i;
        while (j>start && vec[j-1]>vec[j]){
            swap(vec[j-1], vec[j]);
            j--;
        }
    }
}



/* Hybrid quicksort & insertion sort, as soon as the part of the vector to 
   sort becomes less than the crossover value, the algorithm switches to 
   insertion sort, otherwise it 
   uses quicksort */
void hybridSort(Vector<int> &vec, int start, int end, int crossover){
    if(start < end){
        if (end-start <= crossover){
            insertionSort(vec, start, end);
        } else {
            int pivotIndex = partition(vec, start, end);
            hybridSort(vec, start, pivotIndex-1, crossover);
            hybridSort(vec, pivotIndex+1, end, crossover);
        }
    }
}



int main() {

    Vector<int> vec {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 14, 39, 30, 83, 92, 41};
    int end = vec.size()-1;
    hybridSort(vec, 0, end, 4);
    cout << vec.toString() <<endl;

    return 0;

}

I rewrote partition : 我重写partition

int partition(Vector<int>& vec, int start, int end) {
    int pivot = start;
    for(int i = start + 1; i <= end; ++i) {
        if(vec[i] < vec[pivot]) {
            swap(vec[pivot + 1], vec[i]);
            swap(vec[pivot], vec[pivot + 1]);
            ++pivot;
        }
    }
    return pivot;
}

There's also a bug in insertionSort . 还有中的错误insertionSort It should be i <= end instead of i < end . 应该是i <= end而不是i < end Here's the fixed version: 这是固定版本:

void insertionSort(Vector<int>& vec, int start, int end) {
    for(int i = start; i <= end; i++) {
        int j = i;
        while(j > start && vec[j-1] > vec[j]) {
            swap(vec[j-1], vec[j]);
            j--;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM