Stable vs. Non-Stable Sorting
(2011-11-01 16:27:17)
标签:
it |
分类: 数据结构 |
Stable vs. Non-Stable Sorting
Prerequisites, Goals, and Outcomes
Prerequisites: Students should have mastered the following prerequisite skills.
·
·
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.
·
·
·
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 a non-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.
In this assessment, you should firstly implement the sort methods mentioned above corresponding in functions Bubblesort, Insertionsort, Selestsort and Shellsort.
To test the complexity of these four sort methods, you should considerate the best case, worst case, and average case, and correspondingly generate the data. For the stable sort algorithm, the average and worst case are identical. The non-stable sort 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 data initialization requires the generation of random numbers. Use the rand library function to generate a random integer.
Tasks
To complete this assessment, you need to implement four sort algorithm mentioned above and complete the data initialization in main.cpp.
Submission
Submit only the following.
1.
参考代码:
#include "iostream.h"
#include "stdlib.h"
#include "string.h"
//////////////////// "Compare.h"/////////////////////
class Compare{
public:
};
//////////////// "Sorter.h"///////////////////////////
template <class Record,class
Compare>
class Sorter{
protected:
public:
};
//交换数组中的两个记录
template <class Record,class
Compare>
void
Sorter<Record,Compare>::swap(Record
Array[],int i,int j)
{
}
//输出数组内容
template <class Record,class
Compare>
void
Sorter<Record,Compare>::PrintArray(Record
Array[], int n)
{
}
template <class Record,class
Compare>
class InsertSorter:public
Sorter<Record,Compare>{};
//////////////////////"BubbleSorter.h"/////////////////////////
template <class Record,class
Compare>
class BubbleSorter:public
Sorter<Record,Compare>
{
public:
};
//起泡排序,Array[]为待排序数组,n为数组长度
template <class Record,class
Compare>
void
BubbleSorter<Record,Compare>::Sort(Record
Array[], int n)
{
}
//////////////////////StraightInsertSorter///////////////
template <class Record,class
Compare>
class StraightInsertSorter:public
InsertSorter<Record,Compare>
{
public:
};
////////直接插入排序,Array[]为待排序数组,n为数组长度
template <class Record,class
Compare>
void
StraightInsertSorter<Record,Compare>::Sort(Record
Array[], int n)
{
}
template <class Record,class
Compare>
class StraightSelectSorter:public
Sorter<Record,Compare>
{
public:
};
//直接选择排序,Array[]为待排序数组,n为数组长度
template <class Record,class
Compare>
void
StraightSelectSorter<Record,Compare>::Sort(Record
Array[], int n)
{
}
template <class Record,class
Compare>
class ShellSorter:public
Sorter<Record,Compare>
{
private:
public:
};
//Shell排序,Array[]为待排序数组,n为数组长度
template <class Record,class
Compare>
void
ShellSorter<Record,Compare>::Sort(Record
Array[], int n)
{
}
//Shell排序,Array[]为待排序数组,n为数组长度
template <class Record,class
int_intCompare>
void
ShellSorter<Record,int_intCompare>::delta3Sort(Record
Array[], int n)
{
}
// 针对变化的增量而修改的插入排序算法,参数delta表示当前的增量
template <class Record,class
Compare>
void
ShellSorter<Record,Compare>::ModifiedInsertSort(Record
Array[],int n, int delta)
{
//
//
}
// 设定随即函数的种子
inline void Randomize()
//返回一个0到n-1之间的随机数
inline int Random(int n)
void main()
{
}