mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-04-16 10:08:36 +03:00
87 lines
2.6 KiB
Plaintext
Executable File
87 lines
2.6 KiB
Plaintext
Executable File
// Copyright (c) 1998-1999 Matra Datavision
|
|
// Copyright (c) 1999-2012 OPEN CASCADE SAS
|
|
//
|
|
// The content of this file is subject to the Open CASCADE Technology Public
|
|
// License Version 6.5 (the "License"). You may not use the content of this file
|
|
// except in compliance with the License. Please obtain a copy of the License
|
|
// at http://www.opencascade.org and read it completely before using this file.
|
|
//
|
|
// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
|
|
// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
|
|
//
|
|
// The Original Code and all software distributed under the License is
|
|
// distributed on an "AS IS" basis, without warranty of any kind, and the
|
|
// Initial Developer hereby disclaims all such warranties, including without
|
|
// limitation, any warranties of merchantability, fitness for a particular
|
|
// purpose or non-infringement. Please see the License for the specific terms
|
|
// and conditions governing the rights and limitations under the License.
|
|
|
|
// SortTools_QuickSort.gxx
|
|
// cree le 04/11/91 par ASI
|
|
// Reference : Software Conponents with ADA, Grady Booch.
|
|
|
|
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());
|
|
}
|
|
|
|
|
|
|
|
|
|
|