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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include<iostream>
#include<fstream>
#include<ctime>
#include<cstdlib>
using namespace std;
const short MAX = 20;
const short LISTNO = 3;
void createInput( fstream&, const char [], short );
void storeArray( fstream&, short [], short );
void printArray( short [], short );
void storeOutput( fstream&, const char [], short [], short );
void swap( short&, short& );
short findMin( short [], short, short );
void bubbleSort( short [], short );
void insertionSort( short [], short );
void selectionSort( short [], short );
int main()
{
fstream inFile;
short list[LISTNO][MAX];
srand( time( NULL ) );
createInput( inFile, "input.txt", MAX );
for ( short idx = 0; idx < LISTNO; idx++ )
storeArray( inFile, list[idx], MAX );
for ( short idx = 0; idx < LISTNO; idx++ )
{
cout << "Before sorting: list " << idx << endl;
printArray( list[idx], MAX );
}
bubbleSort( list[0], MAX );
insertionSort( list[1], MAX );
selectionSort( list[2], MAX );
for ( short idx = 0; idx < LISTNO; idx++ )
{
cout << "After sorting: list " << idx << endl;
printArray( list[idx], MAX );
}
storeOutput( inFile, "bubble.txt" , list[0], MAX );
storeOutput( inFile, "insertion.txt" , list[1], MAX );
storeOutput( inFile, "selection.txt" , list[2], MAX );
return 0;
}
void createInput( fstream& inFile, const char fileName[], short max )
{
inFile.open( fileName, ios::out);
for ( short idx = 0; idx < max; idx++ )
{
inFile << rand() % 30 + 1 << endl;
}
inFile.close();
return;
}
void storeArray( fstream& inFile, short list[], short size)
{
inFile.open("inFile.txt", ios::in );
for ( short idx = 0; idx < size; idx++ )
inFile >> list[idx];
inFile.close();
return;
}
void printArray( short list[], short size )
{
for ( short idx = 0; idx < size; idx++ )
cout << list[idx] << endl;
return;
}
void storeOutput( fstream& inFile, const char fileName[], short list[], short size)
{
inFile.open( fileName, ios::out );
for ( short idx = 0; idx < size; idx++ )
inFile << list[idx] << endl;
inFile.close();
return;
}
void swap( short& firstNum, short& secondNum)
{
short temp = firstNum;
firstNum = secondNum;
secondNum = temp;
return;
}
short findMin( short list[], short startIdx, short size )
{
short minIdx = startIdx;
for ( short idx = startIdx + 1; idx < size; idx++ )
{
if ( list[idx] < list[minIdx] )
minIdx = idx;
}
return minIdx;
}
void bubbleSort( short list[], short size)
{
bool sorted = false;
short wallIdx,
idx;
for ( wallIdx = 0; wallIdx < size - 1 && !sorted; wallIdx++ )
{
sorted = true;
for ( idx = size - 1; idx > wallIdx; idx-- )
{
if ( list[idx] < list[idx - 1] )
{
swap( list[idx], list[idx-1] );
sorted = false;
}
}
}
return;
}
void insertionSort( short list[], short size )
{
short wallIdx,
idx,
temp;
for ( wallIdx = 1; wallIdx < size; wallIdx++ )
{
temp = list[wallIdx];
for ( idx = wallIdx; idx > 0 && list[idx - 1] > temp; idx-- )
list[idx] = list[idx - 1];
list[idx] = temp;
}
return;
}
void selectionSort( short list[], short size )
{
short wallIdx,
minIdx;
for ( wallIdx = 0; wallIdx < size - 1; wallIdx++ )
{
minIdx = findMin( list, wallIdx, size );
if ( wallIdx != minIdx )
swap( list[wallIdx], list[minIdx] );
}
return;
}
|