简体   繁体   中英

Sorting char arrays by swapping pointers, C++

I am trying to sort an array of char pointers (char * _string) by swapping pointers.

I have this method, and what I want to do is use the values I get from _string and sort them by not manipulating _string, but the empty helper array (char * _output) which I also hand over to the method.

Can anyone help me and tell me what I am doing wrong?

void sortAsc(char* _string, char* _output) 
{

    int length = strlen(_string);

        // output and string now point to the same area in the memory
    _output = _string; 

    for( int i = 0; i < length; i++) {
          for( int j = 0; j < length; j++) {
                if( *(_output) > (_output[j] ) ) {

                    // save the pointer
                    char* tmp = _output;

                    // now output points to the smaller value   
                    _output = _output+j; 

                    // move up the pointer to the smaller value
                    _output + j; 

                    // now the pointer of the smaller value points to the higher value
                    _output = tmp; 

                    // move down to where we were + 1
                    _output - j + 1; 

            }
        }
    }

    //_output[length]='\0';

    //delete chars;
 }

In my main-Method, I do something like this:

char * string = {"bcdae"};
char * output = new char[5];
sortAsc(string, output);

After that code, I want the output array to contain the sorted values.

This sorts the string into an already allocated buffer, and if the buffer isn't large enough tells you how big it has to be:

std::size_t sortAsc(char const* string, char* dest, std::size_t dest_length) {
  std::size_t str_length = strlen(string);
  char const* str_end = string + str_length;
  if (dest_length < str_length+1)
    return str_length+1;
  std::copy( string, str_end, output );
  output[str_length] = '\0';
  std::sort( output, output+strlen(output) );
  return str_length+1;
}

This does the poor "allocate a new string" pattern, using the above implementation:

char* allocate_and_sortAsc(char const* string) {
  std::size_t str_length = strlen(string);
  char* retval = new char[str_length+1];
  std::size_t count = sortAsc( string, retval, str_length+1);
  ASSERT( count <= str_length );
  return retval;
}

And don't use variable names that start with an _ , it is a bad practice because it wanders really near compiler reserved names. _Capital is reserved everywhere, and _lower in global scope, and foo__bar everywhere.

Let's do the selection sort for a 10 size int array using pointer notation, you can simply change it to an array list.

      *---*---*---*---*---* ........
a[] = | 1 | 2 | 4 | 0 | 3 | ........
      *---*---*---*---*---* ........
        ^--------We start here looking for the smaller numbers and sort the array.


for( i = 0; i < 10; i++ ){
    k = i;
    bypass = *( a + i );
    for( j = i + 1; j < 10; j++ ){

        /* To get Increasing order. */
        if( bypass > *( a + j ) ){
           bypass = *( a + j );
           k = j;
        }
    }
    if ( k != i ){
         *( a + k ) = *( a + i );
         *( a + i ) = bypass;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM