SSD5 option 5

来源:互联网 发布:淘宝上卖汽车用品 编辑:程序博客网 时间:2024/06/07 16:23

 

Stable vs. Non-Stable Sorting

Prerequisites, Goals, and Outcomes

Prerequisites: Students should have mastered the following prerequisite skills.

·         Sorting - Knowledge of selection sort and quicksort

·         Complexity - Knowledge of basic asymptotic analysis and Big-Oh notation

Goals: This assignment is designed to reinforce the student's understanding of basic and fast sorting algorithms and complexity.

Outcomes: Students successfully completing this assignment would master the following outcomes.

·         Understand selection sort and quicksort

·         Understand the difference between stable and non-stable sorting algorithms

Background

A stable(稳定的) sort is a sort algorithm that performs(执行) the same amount of work regardless of the data being sorted. The amount of work anon-stable sort performs, on the other hand, can vary(不同) significantly(明显的) depending on the arrangement(安排) of data. This translates(翻译) to stable sorts having identical()相同的 best case, worst case, and average case complexity. The worst case complexity(复杂性) of a non-stable sort can be an entirely different runtime class than its best or average case.

Description

The program to be completed for this assessment demonstrates(演示) the runtime differences between a stable and non-stable sort. The stable sort examined in this assessment(评估) is selection sort and the non-stable algorithm examined in this assessment is quicksort.

Each algorithm is encapsulated(封装) in a corresponding(相应的) class. Method init(初始化) of these classes populates(填充) an array. This function populates the array with data that triggers either the average or worst case of the algorithm. For the stable sort algorithm, selection sort, the average and worst case are identical. The quicksort algorithm, on the other hand, has distinct worst and average cases. A supplied main routine repeatedly initializes and sorts these arrays to demonstrate the differences between the stable and non-stable sort algorithms.

The correct implementation of method init requires the generation of random numbers. Use the rand library function to generate a random integer.

Note to Viusal C++ users: The default stack size in Visual C++ is 1 MB. To execute your solution, you should increase the stack size in your system.

To increase the stack to 10 Mb:

1.  On menu Project, select the project Properties.

2.  Expand Configuration Properties.

3.  Expand Linker.

4.  Select System.

5.  On Stack Reserve Size, write 10485760.

6.  On Stack Commit Size, write 10485760.

Files

Following is a list of files needed to complete this assessment.

  • handout-files.zip contains all of the following necessary files:
    • main.cpp - This file contains the main routine.
    • sort.h - This declares a sort class.
    • sort.cpp - This defines class sort.
    • quicksort.h - This declares a quicksort class.
    • quicksort.cpp - This defines class quicksort.
    • selectionsort.h - This declares a selection sort class.
    • selectionsort.cpp - This defines class selectionsort.

Tasks

To complete this assessment, you need to complete the init methods of class quicksort and class selectionsort.

To begin, verify the files needed for this assessment.

  1. Extract the archive to retrieve the files needed to complete this assessment.

Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often.

  1. Begin by making sure you understand what makes selection sort a stable sort algorithm and what makes quicksort a non-stable sort algorithm. Specifically, find out what ordering of data causes the quicksort algorithm to degrade from anO(n log(n)) algorithm to an O(n2) algorithm.
  2. Next, complete the implementation of method init for class selectionsort. Since this algorithm is stable, this implementation should be the same for the worst and average case. The implementation should populate the inherited numbers array with data that, when sorted, triggers the average case.
  3. Then, finish the implementation of method init for class quicksort. Since this algorithm is non-stable, this method should populate the inherited numbers array with different data to trigger the worst and average cases. Test your implementation by compiling and running the supplied main.cpp. The times reported for the worst case of the quicksort should be relatively close to the times reported for the average case of the selection sort.

Submission

Submit only the following.

  1. quicksort.cpp - finished version of class quicksort
  2. selectionsort.cpp - finished version of class selectionsort

 

 

 

 

Go to top of question.

 这是数据结构课的排序的问题,给的源代码是包含了选择排序和快速排序,实验需要做的只是将给两个排序不同的顺序的数字,然后用两个算法进行排序,通过计算两个排序的时间对两个排序的算法进行评估,其中每个算法包括best顺序,average顺序,还有wares顺序,其中选择排序可以想冒泡排序,冒泡排序的最好顺序是天然有序的,最坏的顺序是天然逆序的,平均的是乱序的,快拍的最好顺序和平均顺序就是乱序,而最坏的顺序就是天然有序后者天然逆序,应为每次确定一个数要比较n次,但是只能将数组分成一段,这样加大了时间复杂度。

下面不展示全部代码,只是局部代码

sort.h 代码:

#ifndef SORT_H#define SORT_H#include <ctime>#include <cstdlib>#include <iostream>using namespace std;const int ITERATIONS = 10; ///数组排序的次数,求平均排序顺序const int MIN_SIZE = 3000; /// Smallest size list to testconst int MAX_SIZE = 24000; /// largest size list to testconst int BEST_CASE = 1;const int AVG_CASE = 2;const int WORST_CASE = 3;class sort {protected:    clock_t start_time;    int numbers[MAX_SIZE]; /// list of numbers to sort (and then sorted)    ///要排序的数组    long numswaps; ///incremented after each swap    ///交换的次数    int how_many, workload_type;    ///数组的长度和这个顺序的类别,是最好的还是平均或者最坏的    void swapNumbers (int x, int y);///交换个数    void startTiming();///    clock_t getElapsedTime();    virtual void init(int how_many, int workload_type) = 0;    ///初始化函数,将数组初始化为不同的类别public:    void reset();///重置函数,    void report ();///进行多次排序,然后进行排序评估    void printNumbers();///输出数组的数字    virtual void sortNumbers() = 0;};#endif
两个初始化的算法:

首先是快排的初始化算法

void quicksort::init (int how_many, int workload_type) {    /**     * Insert your code here     *     * IMPORTANT NOTE: You need to allocate the numbers array declared in     * the sort class so that it can hold "how_many" numbers, and you     * need to fill numbers (integers) into this array. The ordering     * should be determined by the workload.     *     * Since you may be using the same workload for many different sorts     * and configurations of the same sort, you might want to use     * helper methods. Helper methods that should be accessible to     * all of the sorts should be "protected (not public or private) and     * placed in the Sort class. Helper methods used by only this sort     * can stay in this file and be made "private".     *     * This method should NOT sort the array.     */    srand(clock() / 1000);    switch (workload_type)    {    case WORST_CASE:        for (int i = 0;i < how_many; i++)        {            numbers[i] = i;        }    case AVG_CASE:    case BEST_CASE:        for ( int i = 0; i < how_many; i++)        {            numbers[i] = rand()%100000000;        }    }    /**    * Your code goes here    */}
下来是选择排序的算法:

void selectionsort::init (int how_many, int workload_type){    /**     * Insert your code here     *     * IMPORTANT NOTE: You need to allocate (分配) the numbers array declared in     * the sort class so that it can hold "how_many" numbers, and you     * need to fill numbers (integers) into this array. The ordering     * should be determined by the workload.     *     * Since you may be using the same workload for many different sorts     * and configurations of the same sort, you might want to use     * helper methods. Helper methods that should be accessible to     * all of the sorts should be "protected (not public or private) and     * placed in the Sort class. Helper methods used by only this sort     * can stay in this file and be made "private".     *     * This method should NOT sort the array.     */     // seed the random number generator     srand(clock()/1000);     switch ( workload_type )     {     case BEST_CASE:        for ( int i = 0; i < how_many; i++ )        {            numbers [i] = i;        }            break;    case AVG_CASE:        for ( int i = 0; i < how_many; i++)        {            numbers[i] = rand() % 100000000;        }            break;    case WORST_CASE:        for ( int i = 0; i < how_many; i++)        {            numbers[i] = how_many - i;        }            break;     }     /*     * Your code goes here     */}
main函数改不改无所谓,我为了测试一下所有的就改了一下:

#include <iostream>#include <cstdlib>#include "sort.h"#include "selectionsort.h"#include "quicksort.h"using namespace std;/** * This runs the sorts with your workloads and collects * timing information. Depending on the speed of your machine, it * could take hours to run. We suggest that, to collect your results, * you run it paramaterized as shown above. But, you probably want to * test your workloads with much smaller lists and fewer iterations, * first. * * If you abolutely can't wait for the results, reduce the number * of ITERATIONS, perhaps all the way to 1. This makes your results * a little less reliable, since outliers can get in the way, but... * If it is still going to slowly for you to finish on time, try * only MAX_SIZE=12000 ...or even MAX_SIZE=6000 iterations. * If you have to reduce MAX_SIZE below 24000, also reduce MIN_LIST * to 1500 or 750. */int main (int argc, char *argv[]) {    cout << "Selection sort: Average case" << endl;    for (int n = MIN_SIZE; n <= MAX_SIZE; n *= 2) {        selectionsort *selection = new selectionsort(n, AVG_CASE);        selection->report();        delete selection;    }    cout << endl;    cout << "Selection sort: Worse case" << endl;    for (int n = MIN_SIZE; n <= MAX_SIZE; n *= 2) {        selectionsort *selection = new selectionsort(n, WORST_CASE);        selection->report();        delete selection;    }    cout << endl;    cout << "Selection sort: Best case" << endl;    for (int n = MIN_SIZE; n <= MAX_SIZE; n *= 2) {        selectionsort *selection = new selectionsort(n, BEST_CASE);        selection->report();        delete selection;    }    cout << endl;    cout << "Quick sort: Average case" << endl;    for (int n = MIN_SIZE; n <= MAX_SIZE; n *= 2) {        quicksort *quick = new quicksort(n, AVG_CASE);        quick->report();        delete quick;    }    cout << "Quick sort: Worst case" << endl;    for (int n = MIN_SIZE; n <= MAX_SIZE; n *= 2) {        quicksort *quick = new quicksort(n, WORST_CASE);        quick->report();        delete quick;    }    return EXIT_SUCCESS;}








0 0
原创粉丝点击