Radix Sort and Usage of STL Sort Algorithm
(2011-11-01 16:32:13)
标签:
it |
分类: 数据结构 |
Radix Sort and Usage of STL Sort Algorithm
Prerequisites, Goals, and Outcomes
Prerequisites: Students should have mastered the following prerequisite skills.
- Radix Sort - Knowledge of the radix sort algorithm
- C++ STL – Basic knowledge of the usage of C++ STL
- C++ function pointer – Basic Knowledge of C++ function pointer
Goals: This assignment is designed to reinforce the student's understanding of radix sort with time complexity . And also as the final assignment of sort algorithm, the students should master the usage of STL sort algorithm.
Outcomes: Students successfully completing this assignment would master the following outcomes.
- Understand the linear time sort algorithm
- Master the usage of STL sort algorithm
Background
Radix Sort
Radix sort follows the non-intuitive
order of sorting on the least significant feature first and
recombining all the lists immediately after each completed
splitting.
Do small example: for simplicity only 3 possible values in each place
Initial data |
first pass |
second pass |
last pass |
combined |
123 |
231 |
111 |
111 |
111 |
222 |
111 |
312 |
113 |
113 |
312 |
121 |
113 |
121 |
121 |
113 |
+ |
+ |
123 |
123 |
231 |
222 |
121 |
+ |
222 |
111 |
312 |
222 |
222 |
231 |
333 |
+ |
123 |
231 |
312 |
121 |
123 |
+ |
+ |
333 |
|
113 |
231 |
312 |
|
|
333 |
333 |
333 |
|
STL Sort Algorithm
The header <algorithm> defines a collection of functions especially designed to be used on ranges of elements.
A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers. Notice though, that algorithms operate through iterators directly on the values, not affecting in any way the structure of any possible container (it never affects the size or storage allocation of the container).
There are five sort algorithms in STL, and the function prototypes are listed below:
sort |
Sort elements in range (function template) |
stable_sort |
Sort elements preserving order of equivalents (function template) |
partial_sort |
Partially Sort elements in range (function template) |
partial_sort_copy |
Copy and partially sort range (function template) |
nth_element |
Sort element in range (function template) |
Sort
template <class RandomAccessIterator>
template <class RandomAccessIterator, class Compare>
Description
Sorts the elements in the range [first,last) into ascending order.The elements are compared using operator< for the first version, and comp for the second.Elements that would compare equal to each other are not guaranteed to keep their original relative order.
Parameters
first, last
Random-Access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
stable_sort
template <class RandomAccessIterator>
template <class RandomAccessIterator, class Compare>
Description
Sorts the elements in the range [first,last) into ascending order, like sort, but stable_sort grants that the relative order of the elements with equivalent values is preserved.The elements are compared using operator< for the first version, and comp for the second.
Parameters
first, last
Random-Access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
partial_sort
template <class RandomAccessIterator>
template <class RandomAccessIterator, class Compare>
Description
Rearranges the elements in the range [first,last), in such a way that the subrange [first,middle) contains the smallest elements of the entire range sorted in ascending order, and the subrange [middle,end) contains the remaining elements without any specific order.
The elements are compared using operator< for the first version, and comp for the second.
Parameters
first, last
Random-Access iterators to the initial and final positions of the sequence to be used. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Notice that in this function, these are not consecutive parameters, but the first and third ones.
middle
Random-Access iterator pointing to the element within the range [first,last) that is used as the upper boundary of the elements that are completely sorted.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
partial_sort_copy
template <class InputIterator, class RandomAccessIterator>
template <class InputIterator, class RandomAccessIterator, class Compare>
Description
Copies the smallest elements in the range [first,last) to [result_first,result_last), sorting the elements copied. The amount of elements copied is (result_last-result_first) (unless this is more than the amount of elements in [first,last)). [first,last) remains unmodified.
The elements are compared using operator< for the first version, and comp for the second.
Parameters
first, last
Input iterators to the initial and final positions of the sequence to copy from. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result_first, result_last
Random-Access iterators to the initial and final positions of the destination sequence. The range used is [result_first,result_last).
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
nth_element
template <class RandomAccessIterator>
template <class RandomAccessIterator, class Comapre>
Description
Rearranges the elements in the range [first,last), in such a way that the element at the resulting nth position is the element that would be in that position in a sorted sequence, with all the elements preceding it being smaller and all the elements following it greater than it. Neither the elements preceding it nor the elements following it are granted to be ordered.
The elements are compared using operator< for the first version, and comp for the second.
Parameters
first, last
Random-Access iterators to the initial and final positions of the sequence to be used. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Notice that in this function, these are not consecutive parameters, but the first and third ones.
nth
Random-Access iterator pointing to the location within the range [first,last) that will have the sorted element.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
Task
Your task this time is composed of two independent parts.
Firstly, you should implement the radix sort algorithm, and your program is supposed to sort a sequence of English words. For simplicity, all the English words only contain 3 capital letters. For example, you can test your program on the sequence {COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX}.
Secondly, a very simple student class is given below. It has two member variables. Your program is supposed to sort a list of students with the STL sort functions. And you can add some other member function as you need. Then you are supposed to write a program to use the entire STL sort algorithms listed above. You can choose to store a collection of students in an array or some containers in STL, whichever you like.
class student{
…
};
参考代码:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
class student{
};
void RadixSort(string Array[], int n,int d, int r)
{
}
void main()
{