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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include "threeSorts.h"
threeSorts::threeSorts(int size)
{ // constructor
arraySize = size;
noOfItemsInArray = 0;
anArray = new int[size]; // create the array
}
//function to swap two items in the array
void threeSorts::swap(int* arrayPointer, int value1Index, int value2Index)
{
int temp;
temp = arrayPointer[value1Index];
arrayPointer[value1Index] = arrayPointer[value2Index];
arrayPointer[value1Index] = temp;
}
//function that adds an item to the array
void threeSorts::insert( int aValue)
{
anArray[noOfItemsInArray] = aValue;
noOfItemsInArray++;
}
//function to display the items in the array
void threeSorts::display()
{
for(int loop = 0; loop < noOfItemsInArray; loop++)
{
cout << anArray[loop] << " ";
}
cout << endl;
}
//This bubble/exchange sort function uses a slightly changed
//version of the algorithm on page 36 of the module handout.
//See the comments below to explain the change.
//Read the tutorial handout for a fuller explanation.
//
//You can improve this by making the adaptations suggested
//in the handout. I have not.
void threeSorts::bSort()
{
int temp = 0;
for( int i = 1; i<noOfItemsInArray; i++)
{
//The next line is slightly different from the algorithm
//in the handout. We start j at the first element in the array - 0 -
//and end j 2 short of the number of elements, otherwise in the
//last loop anArray[j+1] would be undefined.
for( int j = 0; j<(noOfItemsInArray-1); j++)
{
if(anArray[j] > anArray[j+1])
{ //if (j-1)th > jth
temp = anArray[j]; //swap the values
anArray[j] = anArray[j+1];
anArray[j+1] = temp;
}
}
}
}
//This insertion sort function uses
//the algorithm on page 39 of the module handout.
//Read the tutorial handout for a fuller explanation.
void threeSorts::iSort()
{
int inner, outer;
int temp = 0;
for( outer = 1; outer<noOfItemsInArray; outer++)
{
temp = anArray[outer];
inner = outer;
while(inner > 0 && anArray[inner-1]>=temp)
{
anArray[inner] = anArray[inner-1];
--inner;
}
anArray[inner] = temp;
}
}
//This Selection sort function uses
//the algorithm on page 38 of the module handout.
//Read the tutorial handout for a fuller explanation.
void threeSorts::sSort()
{
int inner, outer;
int position, value;
for( outer = 0; outer<noOfItemsInArray - 1; outer++)
{
position = outer;
value = anArray[position];
for(inner = outer+1; inner < noOfItemsInArray; inner++)
{
if(anArray[inner] < value)
{
position = inner;
value = anArray[position];
}
}
value = anArray[outer];
anArray[outer] = anArray[position];
anArray[position] = value;
}
}
int partition(int* arrayPointer, int value1Index, int value2Index)
{
int pivot;
int smallIndex;
pivot = arrayPointer[value1Index];
smallIndex = value1Index;
for(int index = value1Index+1; index<=value2Index; index++)
{
if(arrayPointer[index] < pivot)
{
smallIndex++;
swap(arrayPointer, smallIndex);
}
}
swap(arrayPointer, value1Index, index);
return smallIndex;
}
void threeSorts::qSort()
{
int pivotLocation;
if(value1Index < value2Index)
{
pivotLocation = partition(arrayPointer, value1Index, value2Index);
qSort(list, value1Index, pivotLocation-1);
qSort(list, pivotLocation+1, value2Index);
}
}
|