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
|
#include <iostream>
#include <cstring>
using namespace std;
const int NUMBER_OF_WORDS = 12233; // number of words in input file
const int MAX_LINE_LENGTH = 9; // 8 + 1 for NULL
void displayArray( char theWords[NUMBER_OF_WORDS][ MAX_LINE_LENGTH], // Array to store all the words
int start, int end)
{
for( int i=start; i<end; i++) {
printf("%s\n", theWords[i]);
}
printf("\n");
}//end displayArray(...)
// Swap two array elements. Used in sorting.
void swap(
char theWords[NUMBER_OF_WORDS][ MAX_LINE_LENGTH], // Array to store all the words
int i, int j)
{
char temp[ MAX_LINE_LENGTH];
strcpy( temp, theWords[i]); // store temporarily
strcpy( theWords[i], theWords[j]);
strcpy( theWords[j], temp);
}
int partition( char theWords[NUMBER_OF_WORDS][ MAX_LINE_LENGTH], // Array to store all the words
int left, // index of left-most element
int right) // index of right-most element
{
// To find index of middle do it the funky way shown below to avoid integer overflow
int pivotIndex = left + (right-left) / 2;
char pivotValue[ MAX_LINE_LENGTH];
strcpy( pivotValue, theWords[ pivotIndex]);
int storeIndex = left; // destination on the left for first swap
// Number of elements in subarray is: right-left+1
// Put pivot value out of the way at far right of array, to be swapped back
// into place at the end.
swap( theWords, pivotIndex, right);
// Go through the current range being considered of array elements,
// moving each element into the left part of the partition if its value
// is less than the pivot value.
for( int i=left; i<right; i++) {
if( strcmp(theWords[i], pivotValue) < 0) {
swap( theWords, i, storeIndex);
storeIndex++;
}
}
// storeIndex is now the index of the location just to the right of the last
// element that is < pivot, so its value >= pivot. Swap the pivot back
// into this spot.
swap( theWords, storeIndex, right); // Move pivot to its final place
return storeIndex;
} // end partition()
void quickSort(
char theWords[NUMBER_OF_WORDS][ MAX_LINE_LENGTH], // Array to store all the words
int first, int last)
{
int pivotIndex;
if( first < last) {
// Partition around a pivot and return index of pivot
pivotIndex = partition( theWords, first, last);
// recursively sort the portion to the left of the pivot
quickSort(theWords, first, pivotIndex - 1);
// recursively sort the portion to the RIGHT of the pivot
quickSort(theWords, pivotIndex + 1, last);
}
}// end quickSort()
int main()
{
char fileName[] = "words.txt"; // Make a C string (array of char) to store filename
FILE *pInputFile; // file pointer
char inputLine[ 9]; // space for an input line
char theWords[NUMBER_OF_WORDS][ MAX_LINE_LENGTH]; // Array to store all the words
// Associate the file pointer with the actual file name and try to open it
pInputFile = fopen(fileName, "r"); // open with "r" for "read" access
// verify that file open worked
if (pInputFile == NULL) {
printf("Can't open %s. Verify it is in correct location\n", fileName);
exit(-1);
}
// read from file and echo output. Note how leading spaces from input are ignored
int numberOfWords = 0; // count how many words there are
while( fscanf(pInputFile, "%s", inputLine) != EOF) {
strcpy( theWords[ numberOfWords], inputLine); // copy word into array
numberOfWords++;
}
fclose( pInputFile); // close the input file
// Go through array and print out all the words
printf("Before sorting, array is: \n");
displayArray( theWords, 0, NUMBER_OF_WORDS);
quickSort( theWords, 0, NUMBER_OF_WORDS);
//printf("After sorting, array is: \n");
//displayArray( theWords, 0, NUMBER_OF_WORDS);
return 0;
}//end main()
|