You can refer to the Bubble Sorting idea, because Bubble Sorting is a stable sorting, and the relative position between equal elements will not change. All letters are considered to be a type of equal elements, all numbers are considered to be a type of equal elements, all symbols are considered to be a type of equal elements, and the size relationship between these three elements is: Letters <numbers <characters.
Optimized Bubble Sorting
The optimized Bubble Sorting introduces lastexchange to identify the position of the last exchange, which can significantly improve the sorting efficiency. The time complexity of an ordered array is O (n ).
Void bubblesort (int * arr, int N) <br/>{< br/> int lastexchange; <br/> int I = n-1; <br/> while (I> 0) <br/> {<br/> lastexchange = 0; <br/> for (Int J = 0; j <I; j ++) <br/>{< br/> If (ARR [J]> arr [J + 1]) <br/>{< br/> int temp; <br/> temp = arr [J + 1]; arr [J + 1] = arr [J]; arr [J] = temp; <br/> lastexchange = J + 1; <br/>}< br/> I = lastexchange; <br/>}< br/>}
Use the Bubble Sorting idea to process strings
// <Br/> // determine whether it is a number <br/> ///////////////////////// //////////////////////////////////////// //////// <br/> inline bool isnum (const char C) <br/>{< br/> If (C> = '0' & C <= '9') return true; <br/> else return false; <br/>}</P> <p> // <br/> // determines whether it is a letter. <br/> ///////////// //////////////////////////////////////// //// // <br/> inline bool isalpha (const char C) <br/>{< br/> If (C> = 'A' & C <= 'Z' | C> = 'A' & C <= 'Z') return true; <br/> else return false; <br/>}</P> <p> // <br/> // determine whether it is another symbol <br/> //////////// //////////////////////////////////////// /////////////////// <br/> inline bool issymbol (const char C) <br/>{< br/> If (! Isalpha (c )&&! Isnum (c) return true; <br/> else return false; <br/>}</P> <p> // <br/> // compare the two characters. <br/> ///////////// //////////////////////////////////////// ////////////////// <br/> int charcmp (const char LHS, const char RHs) <br/>{< br/> If (isalpha (LHS) & isalpha (RHs) return 0; <br/> If (isnum (LHS) & isnum (RHs) return 0; <br/> If (issymbol (LHS) & issymbol (RHs) return 0; <br/> If (isalpha (LHS) & isnum (RHs) retu Rn-1; <br/> If (isalpha (LHS) & issymbol (RHs) Return-1; <br/> If (isnum (LHS) & issymbol (RHs) Return-1; <br/> If (isnum (LHS) & isalpha (RHs) return 1; <br/> If (issymbol (LHS) & isalpha (RHs) return 1; <br/> If (issymbol (LHS) & isnum (RHs) return 1; <br/> return 0; <br/>}</P> <p> // <br/> // parses string functions <br/> ///////////// //////////////////////////////////////// /// // <br/> void parsestring (Char * Str) <br/>{< br/> size_t Len = strlen (STR); <br/> size_t lastexchange; <br/> size_t I = len-1; <br/> while (I> 0) <br/> {<br/> lastexchange = 0; <br/> for (size_t J = 0; j <I; j ++) <br/>{< br/> If (charcmp (STR [J], STR [J + 1])> 0) <br/>{< br/> char tchar; <br/> tchar = STR [J + 1]; STR [J + 1] = STR [J]; STR [J] = tchar; <br/> lastexchange = J + 1; <br/>}< br/> I = lastexchange; <br/>}</ P> <p> int main (void) <br/>{< br/> char s [50] = "ASB ,/? Fw843azx, '23 "; <br/> cout <S <Endl; <br/> parsestring (s); <br/> cout <S <Endl; <br/> return 0; <br/>}