# eryar

posts - 490, comments - 590, trackbacks - 0, articles - 0

### Use the Sort Function in the Package SortTools of the Toolkit TKernel

eryar@163.com China

• SortTools_HeapSortOfInteger ——》 整数的堆排序；
• SortTools_HeapSortOfReal ——》实数的堆排序；
• SortTools_QuickSortOfInteger ——》整数的快速排序；
• SortTools_QuickSortOfReal ——》实数的快速排序；
• SortTools_ShellSortOfInteger ——》整数的希尔排序；
• SortTools_ShellSortOfReal ——》实数的希尔排序；
• SortTools_StraightInsertionSortOfInteger ——》整数的直接插入排序；
• SortTools_StraightInsertionSortOfReal ——》实数的直接插入排序；

`   1:  static void  Sort (TColStd_Array1OfInteger &TheArray, const TCollection_CompareOfInteger &Comp) `

`inline void Exchange(Item& Left, Item& Right)`
`{  `
`  Item Temp = Left;`
`  Left = Right;`
`  Right = Temp;`
`}`
` `
`static void SortRecursive(Array& TheArray,`
`              const Comparator& Comp,`
`              const Standard_Integer Left, `
`              const Standard_Integer Right)`
`{`
`  Item Pivot;`
`  Standard_Integer Front, Back, Middle;`
`  `
`  if(Left < Right) {`
`    Middle = (Left + Right) / 2;`
`    if(Comp.IsLower(TheArray(Middle), TheArray(Left))) {`
`      Exchange(TheArray(Middle), TheArray(Left));`
`    }`
`    if(Comp.IsLower(TheArray(Right), TheArray(Left))) {`
`      Exchange(TheArray(Right), TheArray(Left));`
`    }`
`    if(Comp.IsLower(TheArray(Right), TheArray(Middle))) {`
`      Exchange(TheArray(Right), TheArray(Middle));`
`    }`
`    Pivot = TheArray(Middle);`
`    Exchange(TheArray(Middle), TheArray(Right - 1));`
`    Front = Left + 1;`
`    Back = Right - 1;`
`    if(Back != TheArray.Lower()) {`
`      Back = Back - 1;`
`    }`
`    for(;;) {`
`      while (Comp.IsLower(TheArray(Front), Pivot)) {`
`    Front = Front + 1;`
`      }`
`      while (Comp.IsLower(Pivot, TheArray(Back))) {`
`    Back = Back - 1;`
`      }`
`      if(Front <= Back) {`
`    if(Front == TheArray.Upper()) return;`
`    if(Back == TheArray.Lower()) return;  `
`    Exchange(TheArray(Front), TheArray(Back));`
`    Front = Front + 1;`
`    Back = Back - 1;`
`      }`
`      if(Front > Back) break;`
`    }`
`    SortRecursive(TheArray, Comp, Left, Back);`
`    SortRecursive(TheArray, Comp, Front, Right);`
`  }`
`}`
` `
`void SortTools_QuickSort::Sort(Array& TheArray, `
`              const Comparator& Comp)`
`{`
`  SortRecursive(TheArray, Comp, TheArray.Lower(), TheArray.Upper()); `
`}`

`//------------------------------------------------------------------------------`
`//    Copyright (c) 2012 eryar All Rights Reserved.`
`//`
`//        File    : Main.cpp`
`//        Author  : eryar@163.com`
`//        Date    : 2012-6-23 8:32`
`//        Version : 1.0v`
`//`
`//    Description : Test SortTools Package in the OpenCASCADE.`
`//`
`//==============================================================================`
` `
`#include <TColStd_Array1OfInteger.hxx>`
`#include <TCollection_CompareOfInteger.hxx>`
`#include <SortTools_QuickSortOfInteger.hxx>`
` `
`void    DumpArray(const TColStd_Array1OfInteger& a);`
` `
`int main(int argc, char* argv[])`
`{`
`    TColStd_Array1OfInteger quickSortArray(1, 6);`
`    TCollection_CompareOfInteger    aComp;`
` `
`    // Use zero to initialize the array.`
`    quickSortArray.Init(0);`
` `
`    // Set value of the array.`
`    quickSortArray.SetValue(1, 2);`
`    quickSortArray.SetValue(2, 50);`
`    quickSortArray.SetValue(3, 3);`
`    quickSortArray.SetValue(4, 60);`
`    quickSortArray.SetValue(5, 100);`
`    quickSortArray.SetValue(6, 70);`
` `
`    // Before sort, dump the array.`
`    DumpArray(quickSortArray);`
` `
`    // Sort the array.`
`    // Because the Sort method is static, so can call it directly.`
`    SortTools_QuickSortOfInteger::Sort(quickSortArray, aComp);`
` `
`    // Dump information.`
`    DumpArray(quickSortArray);    `
` `
`    return 0;`
`}`
` `
`void DumpArray( const TColStd_Array1OfInteger& a )`
`{`
`    // Dump information.`
`    cout<<"Array items start:"<<endl;`
`    for (Standard_Integer i = a.Lower(); i <= a.Upper(); i++)`
`    {`
`        cout<<a.Value(i)<<endl;`
`    }`
`    cout<<"Array items end..."<<endl;`
`}`

`   1:  Array items start:`
`   2:  2`
`   3:  50`
`   4:  3`
`   5:  60`
`   6:  100`
`   7:  70`
`   8:  Array items end...`
`   9:  Array items start:`
`  10:  2`
`  11:  3`
`  12:  50`
`  13:  60`
`  14:  70`
`  15:  100`
`  16:  Array items end...`
`  17:  Press any key to continue . . .`