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 117 118 119 120 121 122 123
|
//Tests the Divide-and-Conquer Sorting pattern.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//This is the file sortpattern.cpp.
template <class T>
int split(T a[], int begin, int end);
//Rearranges elements [begin, end) of array a into two intervals
//[begin, splitPt) and [splitPt, end), such that the Sorting pattern works.
//Returns splitPt.
template <class T>
void join(T a[], int begin, int splitPt, int end);
//Combines the elements in the two intervals [begin, split) and
//[splitPt, end) in such a way that the Sorting pattern works.
void fillArray(int a[], int size, int& numberUsed);
//Precondition: size is the declared size of the array a.
//Postcondition: numberUsed is the number of values stored in a.
//a[0] through a[numberUsed - 1] have been filled with
//nonnegative integers read from the keyboard.
template <class T>
void sort(T a[], int begin, int end)
//Precondition: Interval [begin, end) of a has elements.
//Postcondition: The values in the interval [begin, end) have
//been rearranged so that a[0] <= a[1] <= ... <= a[(end - begin) - 1].
{
if ((end - begin) > 1)
{
int splitPt = split(a, begin, end);
sort(a, begin, splitPt);
sort(a, splitPt, end);
join(a, begin, splitPt, end);
}//else sorting one (or fewer) elements, so do nothing.
}
template <class T>
void sort(T a[], int numberUsed)
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
{
sort(a, 0, numberUsed);
}
int main( )
{
cout << "This program sorts numbers from lowest to highest.\n";
int sampleArray[10], numberUsed;
fillArray(sampleArray, 10, numberUsed);
sort(sampleArray, numberUsed);
cout << "In sorted order the numbers are:\n";
for (int index = 0; index < numberUsed; index++)
cout << sampleArray[index] << " ";
cout << endl;
return 0;
}
//File mergesort.cpp: the merge sort realization of the Sorting pattern.
template <class T>
int split(T a[], int begin, int end)
{
return ((begin + end)/2);
}
template <class T>
void join(T a[], int begin, int splitPt, int end)
{
T *temp;
int intervalSize = (end - begin);
temp = new T[intervalSize];
int nextLeft = begin; //index for first chunk
int nextRight = splitPt; //index for second chunk
int i = 0; //index for temp
//Merge till one side is exhausted:
while ((nextLeft < splitPt) && (nextRight < end))
{
if (a[nextLeft] < a[nextRight])
{
temp[i] = a[nextLeft];
i++; nextLeft++;
}
else
{
temp[i] = a[nextRight];
i++; nextRight++;
}
}
while (nextLeft < splitPt)//Copy rest of left chunk, if any.
{
temp[i] = a[nextLeft];
i++; nextLeft++;
}
while (nextRight < end) //Copy rest of right chunk, if any.
{
temp[i] = a[nextRight];
i++; nextRight++;
}
for (i = 0; i < intervalSize; i++)
a[begin + i] = temp[i];
}
void fillArray(int a[], int size, int& numberUsed)
{
cout << "Enter up to " << size << " nonnegative whole numbers.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
}
|