'sort alphabetically using pointer arrays
void sort(char *arr[])
{
char temp[50];
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(strcmp(arr[i],arr[i+1])>0)
{
strcpy(temp,arr[i]);
strcpy(arr[i],arr[i+1]);
strcpy(arr[i+1],temp);
}
}
}
}
int main()
{
char *arr[] = {"zxe","pzae","cazaae","daanans"};
sort(arr);
for(int i=0; i<4; i++)
{
cout<<arr[i]<<endl;
}
}
I want to use this code to sort the strings alphabetically, but this does not seem to work. I want to use pointers specifically for this task. Any leads?
Solution 1:[1]
You only need to swap the pointers, and just iterate from 0 to 2, because you are comparing with the next value i+1
void sort(char *arr[])
{
char * temp;
for(int j=0; j<3;j++)
{
for(int i=0; i<3;i++)
{
if(strncmp(arr[i],arr[i+1],50)>0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
}
Note:
I used strncmp because it is more safe, you can specify n: Maximum number of characters to compare.
Solution 2:[2]
it worked for me
void sort(string* arr[])
{
string* temp;
for (int i = 3; i >= 0; i--)
{
for (int j = 3; j >= 0; j--)
{
char word1 = (*arr[i]).at(0);
char word2 = (*arr[j]).at(0);
if (strncmp(&word1, &word2, 50) > 0)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
string arr1[] = { "zxe", "pzae","cazaae","daanans" };
string* arr[4];
for (int i = 0; i < 4; i++)
{
arr[i] = &arr1[i];
}
sort(arr);
for (int i = 0; i < 4; i++)
{
cout << *arr[i] << endl;
}
}
Solution 3:[3]
Smaller inner loop than before. A argument for the size of the array.
void sort(char *arr[], int size)
{
char * temp;
for(int j=0; j<size-1;j++)
{
for(int i=j+1; i<size; i++)
{
if(strcmp(arr[i],arr[j])>0)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int main()
{
char *arr[]={"zxe","pzae","cazaae","daanans"};
sort(arr, size);
for(int i=0; i<4;i++)
{
cout<<arr[i]<<endl;
}
}
Solution 4:[4]
For starters this declaration
char *arr[] = {"zxe","pzae","cazaae","daanans"};
declares an array of pointers to first characters of string literals. In C++ string literals have types of constant character arrays so a correct declaration of the array should look like
const char * arr[] = { "zxe", "pzae", "cazaae", "daanans" };
To sort the array means to rearrange the elements of the array that is the pointers. The string literals themselves will be unchanged. All you need is to swap pointers to string literals not the string literals. Moreover string literals are immutable. So in any case you may not do what you are trying to do in the program shown in your question. You could do that if instead of the array of pointers to string literals you used a two dimensional array of strings. That is if the array would be declared for example like
char arr[][8] = { "zxe", "pzae", "cazaae", "daanans" };
You could use standard C++ algorithm std::sort. For example
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstring>
int main()
{
const char * arr[] = { "zxe", "pzae", "cazaae", "daanans" };
for ( const char *s : arr ) std::cout << s << ' ';
std::cout << std::endl;
std::sort( std::begin( arr ), std::end( arr ),
[]( const char *s1, const char *s2 )
{
return std::strcmp( s1, s2 ) < 0;
} );
for ( const char *s : arr ) std::cout << s << ' ';
std::cout << std::endl;
return 0;
}
The program output is
zxe pzae cazaae daanans
cazaae daanans pzae zxe
If you want to use the bubble sort method to sort the array then the program can look like
#include <iostream>
#include <utility>
#include <cstring>
void bubble_sort( const char * arr[], size_t n )
{
for ( size_t last; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( std::strcmp( arr[i], arr[i - 1] ) < 0 )
{
std::swap( arr[i], arr[i - 1] );
last = i;
}
}
}
}
int main()
{
const char * arr[] = { "zxe", "pzae", "cazaae", "daanans" };
const size_t N = sizeof( arr ) / sizeof( *arr );
for ( const char *s : arr ) std::cout << s << ' ';
std::cout << std::endl;
bubble_sort( arr, N );
for ( const char *s : arr ) std::cout << s << ' ';
std::cout << std::endl;
return 0;
}
The program output is the same as it is shown above that is
zxe pzae cazaae daanans
cazaae daanans pzae zxe
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | artur108 |
| Solution 3 | Robert Jacobs |
| Solution 4 | Vlad from Moscow |
