Process a string and store it in the order of letters, numbers, and symbols (the Character Sequence remains unchanged and there is no auxiliary space)

Source: Internet
Author: User

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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.