细微的Quicksort稳定性问题

时间:2012-02-10 20:41:32

标签: c++ algorithm sorting quicksort

我正在尝试为一个学校项目创建一个快速排序实现,该项目对CSV文件进行排序,并且非常难以使用它。根据项目规范,按顺序对CSV文件的每一列进行排序将迫使不稳定的排序变得稳定,即:“。/ sort -algorithm quicksort -k 1,2,3,4,5 input.csv”应该产生与“./sort --algorithm insertion -k 1,2,3,4,5 input.csv”相同的结果

为了保留以前的排序,排序反向执行,如下所示:

for (int current_key = config.sort_columns.size()-1; current_key >= 0; current_key--){
    sorting_algorithm(records, config, config.sort_columns[current_key]-1);
}

其中config.sort_columns是-k参数指定的所有排序列的向量。

以下是我的意见:

name,breed,date of birth,date of death,avg eggs per week 
Marilyn,Isa Red,2011-04-24,N/A,6 
Brian,Derp,2010-01-15,2011-12-01,4 
Chrissy,Ent,2012-02-08,N/A,3 
Mildred,Araucana,2011-05-01,N/A,3 
Jimmy,Ent,2006-02-30,N/A,15 
Mabel,Isa Red,2011-04-26,N/A,5 
Myrtle,Araucana,2011-08-01,N/A,0 
Myrtle,Araucana,2011-05-01,2011-07-13,0 
Adam,Ent,2010-01-01,N/A,10 
Isabel,Ent,2009-04-01,N/A,2 
Jimmy,Ent,2006-02-30,2011-03-24,1 
Jimmy,Derp,2003-02-30,2010-03-24,8 
Myrtle,Herp,2011-08-01,N/A,0 

以下是我的插入排序的输出(应该并且看起来是正确的):

name,breed,date of birth,date of death,avg eggs per week 
Adam,Ent,2010-01-01,N/A,10 
Brian,Derp,2010-01-15,2011-12-01,4 
Chrissy,Ent,2012-02-08,N/A,3 
Isabel,Ent,2009-04-01,N/A,2 
Jimmy,Derp,2003-02-30,2010-03-24,8 
Jimmy,Ent,2006-02-30,2011-03-24,1 
Jimmy,Ent,2006-02-30,N/A,15 
Mabel,Isa Red,2011-04-26,N/A,5 
Marilyn,Isa Red,2011-04-24,N/A,6 
Mildred,Araucana,2011-05-01,N/A,3 
Myrtle,Araucana,2011-05-01,2011-07-13,0 
Myrtle,Araucana,2011-08-01,N/A,0 
Myrtle,Herp,2011-08-01,N/A,0

这是我的快速排序的输出:

name,breed,date of birth,date of death,avg eggs per week
Adam,Ent,2010-01-01,N/A,10
Brian,Derp,2010-01-15,2011-12-01,4
Chrissy,Ent,2012-02-08,N/A,3
Isabel,Ent,2009-04-01,N/A,2
Jimmy,Ent,2006-02-30,2011-03-24,1
Jimmy,Ent,2006-02-30,N/A,15
Jimmy,Derp,2003-02-30,2010-03-24,8
Mabel,Isa Red,2011-04-26,N/A,5
Marilyn,Isa Red,2011-04-24,N/A,6
Mildred,Araucana,2011-05-01,N/A,3
Myrtle,Herp,2011-08-01,N/A,0
Myrtle,Araucana,2011-08-01,N/A,0
Myrtle,Araucana,2011-05-01,2011-07-13,0

正如你所看到的,这几乎是正确的,只是当第一列匹配时第二列错误(例如“Derp”应该在两个“Ents”之前)。

最后,这是我的快速实施:

int sort_quick_partition(std::vector<Record> &records, bool (*comparison)(string, string), int sort_key, int left, int right){
    /*
    Partition the vector and return the address of the new pivot.

    @param less - Vector of elements less than pivot.
    @param greater - Vector of elements greater than pivot.
    */

    // Setup 
    int store_location;
    Record pivot = records[right];
    Record temp_record;

    // Loop through and partition the vector within the provided bounds
    store_location = left - 1;
    for (int j = left; j < right; j++){
        if (comparison(records[j].fields[sort_key],pivot.fields[sort_key])){
            store_location += 1;
            std::swap(records[store_location], records[j]);
        }
    }

    std::swap(records[store_location+1], records[right]);

    return store_location+1;
}

void sort_quick_helper(std::vector<Record> &records, bool (*comparison)(string, string), int sort_key, int left, int right){
    /*
    Actually performs the quick sort.

    @param sub_list - Vector to sort.
    @param *comparison - Comparison to perform.
    @param sort_key - Which column to sort by.
    @param left - Left side of active sort zone.
    @param right - Right side of active sort zone.
    */

    // Setup
    int new_pivot;

    // Make sure the list has 2 or more items
    if (left < right){
        // Partition the vector and get the new pivot value
        new_pivot = sort_quick_partition(records, comparison, sort_key, left, right);

        // Sort elements less than the pivot
        sort_quick_helper(records, comparison, sort_key, left, new_pivot-1);

        // Sort elements greater than the pivot
        sort_quick_helper(records, comparison, sort_key, new_pivot+1, right);
    }
}

void sort_quick(std::vector<Record> &records, Configuration &config, int sort_key){
    /*
    Perform a quick sort on the records.

    @param &records - Vector of Record structures representing the file.
    @param &config - Reference to a global configuration structure.
    @param sort_key - Which column to sort by.
    */

    // Decide if it needs to be reversed
    bool (*comparison)(string, string);
    if (config.reverse){
        comparison = &comparison_gte;
    } else {
        comparison = &comparison_lte;
    }

    // Call the sort
    sort_quick_helper(records, comparison, sort_key, 0, records.size()-1);
}

请注意,“sorting_algorithm”是指向活动排序的函数指针,在这种情况下为“sort_quick”。

有谁看到可能出错的地方?我一直试图解决这个问题几天,此时我正在拔头发。谢谢大家!

2 个答案:

答案 0 :(得分:3)

通过反复排序,你可以通过不稳定的方式进行稳定的排序。考虑最后的排序:当它看到相同的键时,不保证保留先前的排序(这就是不稳定的意思)。

相反,您需要做的是对明确排序输入的键进行排序 - 因此您需要进行一种排序,您排序的键是所有列而不是单个列。

因此,当您比较记录“Myrtle,Araucana,2011-08-01,N / A,0”和“Myrtle,Araucana,2011-05-01,2011-07-13,0”时,您需要比较字段按顺序排列,直到找到一对不相等的字段。 (这称为词典排序。)如果您需要保留完全相同记录的顺序,您甚至可能需要合并原始位置。

当然,如果这不是作业,你可能会看std::stable_sort。 (相反顺序的列上的一系列稳定排序就可以了。)

答案 1 :(得分:0)

好吧,由于您选择了枢轴作为right最多元素,因此您的排序看起来很稳定。但是,到最后一行。

std::swap(records[store_location+1], records[right]);

就像那样交换两个记录,即使它们是平等的。添加一个检查以仅在它们不相等时进行排序:

// You'll probably use your comparison() function here.
if ( records[store_location+1].fields[sort_key] != records[right].fields[sort_key] ) {
    std::swap(records[store_location+1], records[right]);
}