Quicksort algorithm and character count

Quicksort algorithm and character count
This C++ console application accepts a string as input and after storing them in a character array, sorts them alphabetically using the quicksort algorithm. It also counts the number of character occurences in the string.
1.  #include <iostream>
2.  
3.  using namespace std;
4.   
5.  int preProcess(char *, int);
6.  void quickSort(char *, int, int);
7.  int partition(char *, int, int);
8.  void swap(int &, int &);
9.  void specialSort(char *, int, int);
10. void genReport(char *, int);
11. // Checks if the user was planning to quit
12. bool isQuit(char *);
13. const int stringSize = 255;
14.  
15. int main()
16. {
17.     char toCheck[stringSize];
18.     // Do while the user doesn't enter ~quit
19.     do
20.     {
21.          cout << "Ready: ";
22.          cin.getline(toCheck, sizeof(toCheck));
23.          
24.          // Strip spaces
25.          int newSize = preProcess(toCheck, strlen(toCheck));
26.          
27.          // New array with new size
28.          char newToCheck[newSize];
29.          // Loop to fill the new array
30.          for(int n = 0; n < newSize; n++)
31.          {
32.              newToCheck[n] = toCheck[n];
33.          }
34.          newToCheck[newSize] = '\0';
35.          // Check if the user choosed to quit
36.          if(isQuit(newToCheck))
37.          {
38.              return false;
39.          }
40.          
41.          // Separate a to z into another array
42.          char alphaLower[newSize];
43.          int n = 0;
44.          for(int i = 0; i < newSize; i++)
45.          {
46.              if((int)newToCheck[i] >= 97 && (int)newToCheck[i] <= 122)
47.              {
48.                  alphaLower[n] = newToCheck[i];
49.                  n++;
50.              }
51.          }
52.          // Copy the new array into a smaller one
53.          char alphaLowerNew[n];
54.          for(int i = 0; i < n; i++)
55.          {
56.              alphaLowerNew[i] = alphaLower[i];
57.          }
58.          alphaLowerNew[n] = '\0';
59.          // Sort a to z
60.          quickSort(alphaLowerNew, 97, 122);
61.          
62.          // Separate 0 to 9 into another array
63.          char nums[newSize];
64.          n = 0;
65.          for(int i = 0; i < newSize; i++)
66.          {
67.              if((int)newToCheck[i] >= 48 && (int)newToCheck[i] <= 57)
68.              {
69.                  nums[n] = newToCheck[i];
70.                  n++;
71.              }
72.          }
73.          // Copy the new array into a smaller one
74.          char numsNew[n];
75.          for(int i = 0; i < n; i++)
76.          {
77.              numsNew[i] = nums[i];
78.          }
79.          numsNew[n] = '\0';
80.          // Sort 0 to 9
81.          quickSort(numsNew, 48, 57);
82.          
83.          // Separate A to Z into another array
84.          char alphaUpper[newSize];
85.          n = 0;
86.          for(int i = 0; i < newSize; i++)
87.          {
88.              if((int)newToCheck[i] >= 65 && (int)newToCheck[i] <= 90)
89.              {
90.                  alphaUpper[n] = newToCheck[i];
91.                  n++;
92.              }
93.          }
94.          // Copy the new array into a smaller one
95.          char alphaUpperNew[n];
96.          for(int i = 0; i < n; i++)
97.          {
98.              alphaUpperNew[i] = alphaUpper[i];
99.          }
100.         alphaUpperNew[n] = '\0';
101.         // Sort A to Z
102.         quickSort(alphaUpperNew, 65, 90);
103.         
104.         // Separate remaining characters into another array
105.         char specialChars[newSize];
106.         n = 0;
107.         for(int i = 0; i < newSize; i++)
108.         {
109.             if((int)newToCheck[i] == 63 || (int)newToCheck[i] == 33 || (int)newToCheck[i] == 44 || (int)newToCheck[i] == 35 || (int)newToCheck[i] == 42 || (int)newToCheck[i] == 46)
110.             {
111.                 specialChars[n] = newToCheck[i];
112.                 n++;
113.             }
114.         }
115.         // Copy the new array into a smaller one
116.         char specialCharsNew[n];
117.         for(int i = 0; i < n; i++)
118.         {
119.             specialCharsNew[i] = specialChars[i];
120.         }
121.         specialCharsNew[n] = '\0';
122.         // Sort ?, !, etc
123.         specialSort(specialCharsNew, sizeof(specialCharsNew), 46);
124.         specialSort(specialCharsNew, sizeof(specialCharsNew), 42);
125.         specialSort(specialCharsNew, sizeof(specialCharsNew), 35);
126.         specialSort(specialCharsNew, sizeof(specialCharsNew), 43);
127.         specialSort(specialCharsNew, sizeof(specialCharsNew), 33);
128.         specialSort(specialCharsNew, sizeof(specialCharsNew), 63);
129.         
130.         // Count the new size of the final array
131.         int newBigSize = sizeof(specialCharsNew) + sizeof(alphaLowerNew) + sizeof(numsNew) + sizeof(alphaUpperNew);
132.         // Build one array out of the three
133.         char bigArray[newBigSize - 1];
134.         int bigIndex = 0;
135.         for(int i = 0; i < sizeof(alphaLowerNew); i++)
136.         {
137.             bigArray[bigIndex] = alphaLowerNew[i];
138.             bigIndex++;
139.         }
140.         for(int i = 0; i < sizeof(numsNew); i++)
141.         {
142.             bigArray[bigIndex] = numsNew[i];
143.             bigIndex++;
144.         }
145.         for(int i = 0; i < sizeof(alphaUpperNew); i++)
146.         {
147.             bigArray[bigIndex] = alphaUpperNew[i];
148.             bigIndex++;
149.         }
150.         for(int i = 0; i < sizeof(specialCharsNew); i++)
151.         {
152.             bigArray[bigIndex] = specialCharsNew[i];
153.             bigIndex++;
154.         }
155.         bigArray[newBigSize] = '\0';
156.         genReport(bigArray, newBigSize);
157.         cout << "\n\n";
158.    }
159.    while(true);
160.    system("PAUSE");
161.    return 0;
162.}
163. 
164.void genReport(char* toRep, int size)
165.{
166.    int report[256];
167.    // Initialize the array to 0
168.    for(int n = 0; n < 256; n++)
169.    {
170.        report[n] = 0;
171.    }
172.    // Generate the report
173.    for(int n = 0; n < size; n++)
174.    {
175.        report[(int)toRep[n]]++;
176.    }
177.    // Display the report
178.    for(int n = 0; n < 256; n++)
179.    {
180.        if(report[n] > 0)
181.        {
182.             cout << char(n) << ": " << report[n] << "\n";
183.        }
184.    }
185.}
186. 
187.void specialSort(char* toSort, int size, int charAscii)
188.{
189.    char tempValue[1];
190.    int currValue, minIndex, minValue;
191.    for(int n = 0; n < (size - 1); n++)
192.    {
193.        minIndex = n;
194.        minValue = toSort[n];
195.        for(int i = n + 1; i < size; i++)
196.        {
197.            if(toSort[i] == charAscii)
198.            {
199.                 minIndex = i;
200.                 minValue = toSort[i];
201.            }
202.        }
203.        toSort[minIndex] = toSort[n];
204.        toSort[n] = minValue;
205.    }
206.}
207. 
208.void quickSort(char set[], int start, int end)
209.{
210.    char newSet[end];
211.    int pivotPoint;
212.    if (start < end)
213.    {
214.        // Get the pivot point.
215.        pivotPoint = partition(newSet, start, end);
216.        // Sort the first sub list.
217.        quickSort(newSet, start, pivotPoint - 1);
218.        // Sort the second sub list.
219.        quickSort(newSet, pivotPoint + 1, end);
220.    }
221.}
222. 
223.void swap(int &value1, int &value2)
224.{
225.    int temp = value1;
226.    value1 = value2;
227.    value2 = temp;
228.}
229. 
230.int partition(char set[], int start, int end)
231.{
232.    int pivotValue, pivotIndex, mid;
233.    mid = (start + end) / 2;
234.    swap(set[start], set[mid]);
235.    pivotIndex = start;
236.    pivotValue = set[start];
237.    for (int scan = start + 1; scan <= end; scan++)
238.    {
239.        if((set[scan] < pivotValue && ((int)set[scan] >= start && (int)set[scan] <= end)))
240.        {
241.            pivotIndex++;
242.            swap(set[pivotIndex], set[scan]);
243.        }
244.    }
245.    swap(set[start], set[pivotIndex]);
246.    return pivotIndex;
247.}
248. 
249.bool isQuit(char* phrase)
250.{
251.     char quit[6] = "~quit";
252.     // Compare the char array to '~quit'
253.     for(int i = 0; i < 5; i++)
254.     {
255.         if(phrase[i] != quit[i])
256.         {
257.             return false;
258.         }
259.     }
260.     return true;
261.}
262. 
263. 
264.int preProcess(char* phrase, int size)
265.{
266.    int currChar = 0;
267.    // Loop through the original array
268.    for(int i = 0; i < size; i++)
269.    {
270.         // Only add non-space characters to the new array
271.         if(phrase[i] != ' ')
272.         {
273.              phrase[currChar] = phrase[i];
274.              currChar++;
275.         }
276.    }
277.    // We're out with the new number of chars
278.    return currChar;
279.}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top