2012-09-19 13:31:09 +00:00
|
|
|
/* Implementation of ShellSort for GNUStep
|
|
|
|
Copyright (C) 1995-2012 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
|
|
|
Modified by: Niels Grewe <niels.grewe@halbordnung.de>
|
|
|
|
Date: September 2012
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2012-09-19 13:31:09 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
2012-09-19 13:31:09 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 09:32:00 +00:00
|
|
|
#import "common.h"
|
2012-09-19 13:31:09 +00:00
|
|
|
#import "Foundation/NSSortDescriptor.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSObjCRuntime.h"
|
|
|
|
#import "GSSorting.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
_GSShellSort(id *objects,
|
2012-09-19 14:20:01 +00:00
|
|
|
NSRange sortRange,
|
|
|
|
id comparisonEntity,
|
|
|
|
GSComparisonType type,
|
|
|
|
void *context)
|
2012-09-19 13:31:09 +00:00
|
|
|
{
|
|
|
|
/* Shell sort algorithm taken from SortingInAction - a NeXT example */
|
|
|
|
#define STRIDE_FACTOR 3 // good value for stride factor is not well-understood
|
|
|
|
// 3 is a fairly good choice (Sedgewick)
|
|
|
|
NSUInteger c;
|
|
|
|
NSUInteger d;
|
|
|
|
NSUInteger stride = 1;
|
|
|
|
BOOL found;
|
|
|
|
NSUInteger count = NSMaxRange(sortRange);
|
|
|
|
#ifdef GSWARN
|
|
|
|
BOOL badComparison = NO;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (stride <= count)
|
|
|
|
{
|
|
|
|
stride = stride * STRIDE_FACTOR + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (stride > (STRIDE_FACTOR - 1))
|
|
|
|
{
|
|
|
|
// loop to sort for each value of stride
|
|
|
|
stride = stride / STRIDE_FACTOR;
|
|
|
|
for (c = (sortRange.location + stride); c < count; c++)
|
|
|
|
{
|
|
|
|
found = NO;
|
|
|
|
if (stride > c)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
d = c - stride;
|
|
|
|
while (!found) /* move to left until correct place */
|
|
|
|
{
|
|
|
|
id a = objects[d + stride];
|
|
|
|
id b = objects[d];
|
|
|
|
NSComparisonResult r;
|
2012-09-19 14:20:01 +00:00
|
|
|
|
|
|
|
r = GSCompareUsingDescriptorOrComparator(a, b,
|
2012-09-20 09:32:00 +00:00
|
|
|
comparisonEntity, type, context);
|
2012-09-19 13:31:09 +00:00
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
#ifdef GSWARN
|
|
|
|
if (r != NSOrderedAscending)
|
|
|
|
{
|
|
|
|
badComparison = YES;
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-19 14:20:01 +00:00
|
|
|
objects[d + stride] = b;
|
2012-09-19 13:31:09 +00:00
|
|
|
objects[d] = a;
|
|
|
|
if (stride > d)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
d -= stride; // jump by stride factor
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef GSWARN
|
|
|
|
if (r != NSOrderedDescending && r != NSOrderedSame)
|
|
|
|
{
|
|
|
|
badComparison = YES;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
found = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef GSWARN
|
|
|
|
if (badComparison == YES)
|
|
|
|
{
|
|
|
|
NSWarnFLog(@"Detected bad return value from comparison");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@interface GSShellSortPlaceHolder : NSObject
|
2016-07-15 11:30:07 +00:00
|
|
|
+ (void) setUnstable;
|
2012-09-19 13:31:09 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSShellSortPlaceHolder
|
2016-07-15 11:30:07 +00:00
|
|
|
+ (void) setUnstable
|
2012-09-19 13:31:09 +00:00
|
|
|
{
|
|
|
|
_GSSortUnstable = _GSShellSort;
|
|
|
|
}
|
|
|
|
@end
|
2016-07-15 11:30:07 +00:00
|
|
|
|