-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortStringArray.cpp
116 lines (96 loc) · 2.62 KB
/
SortStringArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SortStringArray.cpp: Implementierung der Klasse CSortStringArray.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SortStringArray.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
void CSortStringArray::Sort(BOOL bCase)
{
QuickSort(bCase);
}
void CSortStringArray::BubbleSort(BOOL bCase)
{
INT nBound = GetUpperBound();
if( nBound <= 0 ) return;
if( bCase ) BubbleSortInner(0, nBound);
else BubbleSortInnerNoCase(0, nBound);
}
void CSortStringArray::QuickSort(BOOL bCase)
{
INT nBound = GetUpperBound();
if( nBound <= 0 ) return;
if( bCase ) QuickSortInner(0, nBound);
else QuickSortInnerNoCase(0, nBound);
}
void CSortStringArray::BubbleSortInner(INT nLeft, INT nRight)
{
BOOL bNotDone = TRUE;
while( bNotDone ) {
bNotDone = FALSE;
for( INT nPos = nLeft; nPos < nRight; nPos++ ) {
bNotDone |= CompareAndSwap( nPos );
}
}
}
void CSortStringArray::BubbleSortInnerNoCase(INT nLeft, INT nRight)
{
BOOL bNotDone = TRUE;
while( bNotDone ) {
bNotDone = FALSE;
for( INT nPos = nLeft; nPos < nRight; nPos++ ) {
bNotDone |= CompareAndSwapNoCase( nPos );
}
}
}
void CSortStringArray::QuickSortInner(INT l, INT r)
{
INT i = l, j = r;
CString & szTest = GetAt( (l + r) / 2 );
do {
while( GetAt(i).Compare(szTest) < 0 ) i++;
while( GetAt(j).Compare(szTest) > 0 ) j--;
if( i <= j ) { SwapElements(i, j); i++; j--; }
} while( i <= j );
if( l < j ) QuickSortInner(l, j);
if( i < r ) QuickSortInner(i, r);
}
void CSortStringArray::QuickSortInnerNoCase(INT l, INT r)
{
INT i = l, j = r;
CString & szTest = GetAt( (l + r) / 2 );
do {
while( GetAt(i).CompareNoCase(szTest) < 0 ) i++;
while( GetAt(j).CompareNoCase(szTest) > 0 ) j--;
if( i <= j ) { SwapElements(i, j); i++; j--; }
} while( i <= j );
if( l < j ) QuickSortInnerNoCase(l, j);
if( i < r ) QuickSortInnerNoCase(i, r);
}
BOOL CSortStringArray::CompareAndSwap(INT nPos1)
{
INT nPos2 = nPos1 + 1;
if( GetAt(nPos1).Compare( GetAt(nPos2) ) > 0 ) {
SwapElements(nPos1, nPos2); return TRUE;
} else return FALSE;
}
BOOL CSortStringArray::CompareAndSwapNoCase(INT nPos1)
{
INT nPos2 = nPos1 + 1;
if( GetAt(nPos1).CompareNoCase(GetAt(nPos2)) > 0 ) {
SwapElements(nPos1, nPos2); return TRUE;
} else return FALSE;
}
void CSortStringArray::SwapElements(INT nPos1, INT nPos2)
{
static CString szTemp;
szTemp = GetAt(nPos1);
SetAt(nPos1, GetAt(nPos2));
SetAt(nPos2, szTemp);
}