排序是计算机内经常进行的一种操作,其目的是将一组 “无序” 的数据元素调整为 “有序” 的数据元素。



按总评排序后为什么张无忌的排名比郭靖靠前呢?
因为采用的不稳定排序法。



多关键字排序是否比单关键字排序更复杂?
对于多关键字排序,只需要在比较操作时同时考虑多个关键字即可!!!
代码在最后,整合在一起。

代码在最后,整合在一起。
每次(例如第i次,i = 0,1,..., n-2)从后面n-i个待排的数据元素中选出关键字最小的元素,作为有序元素序列第i个元素。



代码在最后,整合在一起。
当插入第i(i≥ 1)个数据元素时,前面的V[0],V[1] ,V[i-1]已经排好序;这时,用Vi]的关键字与V[i-1] ,Vii-2],...,V[O]的关键字进行比较,
找到位置后将V[ü]插入,原来位置上的对象向后顺移。




代码在最后,整合在一起。
每次从后向前进行(假设为第i次),j = n-1, n-2, ..., i ,两两比较Vlj-1]和V[j]的关键字﹔如果发生逆序,则交换vlj-1]和Vlj]。



代码在最后,整合在一起。
将待排序列划分为若干组,在每一组内进行插入排序,以使整个序列基本有序,然后再对整个序列进行插入排序。




代码在最后,整合在一起。
将两个或两个以上的有序序列合并成一个新的有序序列




代码在最后,整合在一起。


代码在最后,整合在一起。


Array.h
Sort.h
代码在最后,整合在一起。
当待排数据元素为体积庞大的对象时,如何提高排序的效率?



Proxy Pattern



- #ifndef SORT_H
- #define SORT_H
-
- #include "Object.h"
- #include "Array.h"
-
- namespace DTLib
- {
-
- class Sort : public Object
- {
- private:
- Sort();
- Sort(const Sort&);
- Sort& operator = (const Sort&);
-
- template < typename T >
- static void Swap(T& a, T& b)
- {
- T c(a);
- a = b;
- b = c;
- }
-
- template < typename T >
- static void Merge(T src[], T helper[], int begin, int mid, int end, bool min2max)
- {
- int i = begin;
- int j = mid + 1;
- int k = begin;
-
- while( (i <= mid) && (j <= end) )
- {
- if( min2max ? (src[i] < src[j]) : (src[i] > src[j]) )
- {
- helper[k++] = src[i++];
- }
- else
- {
- helper[k++] = src[j++];
- }
- }
-
- while( i <= mid )
- {
- helper[k++] = src[i++];
- }
-
- while( j <= end )
- {
- helper[k++] = src[j++];
- }
-
- for(i=begin; i<=end; i++)
- {
- src[i] = helper[i];
- }
- }
-
- template < typename T >
- static void Merge(T src[], T helper[], int begin, int end, bool min2max)
- {
- if( begin < end )
- {
- int mid = (begin + end) / 2;
-
- Merge(src, helper, begin, mid, min2max);
- Merge(src, helper, mid+1, end, min2max);
- Merge(src, helper, begin, mid, end, min2max);
- }
- }
-
- template < typename T >
- static int Partition(T array[], int begin, int end, bool min2max)
- {
- T pv = array[begin];
-
- while( begin < end )
- {
- while( (begin < end) && (min2max ? (array[end] > pv) : (array[end] < pv)) )
- {
- end--;
- }
-
- Swap(array[begin], array[end]);
-
- while( (begin < end) && (min2max ? (array[begin] <= pv) : (array[begin] >= pv)) )
- {
- begin++;
- }
-
- Swap(array[begin], array[end]);
- }
-
- array[begin] = pv;
-
- return begin;
- }
-
- template < typename T >
- static void Quick(T array[], int begin, int end, bool min2max)
- {
- if( begin < end )
- {
- int pivot = Partition(array, begin, end, min2max);
-
- Quick(array, begin, pivot-1, min2max);
- Quick(array, pivot+1, end, min2max);
- }
- }
-
- public:
- // 选择排序
- template < typename T >
- static void Select(T array[], int len, bool min2max = true)
- {
- for(int i=0; i<len; i++)
- {
- int min = i;
-
- for(int j=i+1; j<len; j++)
- {
- if( min2max ? (array[min] > array[j]) : (array[min] < array[j]) )
- {
- min = j;
- }
- }
-
- if( min != i )// 如果当前位置的值是若干最小值中的一个,就不再进行交换了。
- {
- Swap(array[i], array[min]);
- }
- }
- }
- // 插入排序
- template < typename T >
- static void Insert(T array[], int len, bool min2max = true)
- {
- for(int i=1; i<len; i++)
- {
- int k = i;
- T e = array[i];
-
- for(int j=i-1; (j>=0) && (min2max ? (array[j]>e) : (array[j]<e)); j--)
- {
- array[j+1] = array[j];
- k = j;
- }
-
- if( k != i )
- {
- array[k] = e; // 插入位置
- }
- }
- }
- // 冒泡排序
- template < typename T >
- static void Bubble(T array[], int len, bool min2max = true)
- {
- bool exchange = true;
-
- for(int i=0; (i<len) && exchange; i++)
- {
- exchange = false;
-
- for(int j=len-1; j>i; j--)
- {
- if( min2max ? (array[j] < array[j-1]) : (array[j] > array[j-1]) )
- {
- Swap(array[j], array[j-1]);
- exchange = true;
- }
- }
- }
- }
- // 希尔排序
- template < typename T >
- static void Shell(T array[], int len, bool min2max = true)
- {
- int d = len;
-
- do
- {
- d = d / 3 + 1; // 大量的理论实践证明,此方法效率最高
-
- for(int i=d; i<len; i+=d)
- {
- int k = i;
- T e = array[i];
-
- for(int j=i-d; (j>=0) && (min2max ? (array[j]>e) : (array[j]<e)); j-=d)
- {
- array[j+d] = array[j];
- k = j;
- }
-
- if( k != i )
- {
- array[k] = e;
- }
- }
-
- } while( d > 1 );
- }
-
- template < typename T >
- static void Merge(T array[], int len, bool min2max = true)
- {
- T* helper = new T[len];
-
- if( helper != NULL )
- {
- Merge(array, helper, 0, len-1, min2max);
- }
-
- delete[] helper;
- }
-
- template < typename T >
- static void Quick(T array[], int len, bool min2max = true)
- {
- Quick(array, 0, len-1, min2max);
- }
-
- template < typename T >
- static void Heap(T array[], int len, bool min2max = true)
- {
- DynamicHeap<T> heap(len, !min2max);
-
- for(int i=0; i<len; i++)
- {
- heap.add(array[i]);
- }
-
- for(int i=0; i<len; i++)
- {
- array[i] = heap.front();
-
- heap.remove();
- }
- }
-
- template < typename T >
- static void Heap(Array<T>& array, bool min2max = true)
- {
- Heap(array.array(), array.length(), min2max);
- }
-
- template < typename T >
- static void Select(Array<T>& array, bool min2max = true)
- {
- Select(array.array(), array.length(), min2max);
- }
-
- template < typename T >
- static void Insert(Array<T>& array, bool min2max = true)
- {
- Insert(array.array(), array.length(), min2max);
- }
-
- template < typename T >
- static void Bubble(Array<T>& array, bool min2max = true)
- {
- Bubble(array.array(), array.length(), min2max);
- }
-
- template < typename T >
- static void Shell(Array<T>& array, bool min2max = true)
- {
- Shell(array.array(), array.length(), min2max);
- }
-
- template < typename T >
- static void Merge(Array<T>& array, bool min2max = true)
- {
- Merge(array.array(), array.length(), min2max);
- }
-
- template < typename T >
- static void Quick(Array<T>& array, bool min2max = true)
- {
- Quick(array.array(), array.length(), min2max);
- }
- };
-
- }
-
- #endif // SORT_H