How do I sort an array of cstrings?

jj_evans's Avatar, Join Date: Jun 2010
Light Poster
I need to sort an array of cstrings in ascending order. the cstrings are names and i have to list them all in alphabetical order. The problem is I don't know how to determing which cstring comes first. Below is my code for sorting integers please let me know what to modify to sort cstrings in as much detail as you can. Thank you!

Code:
void swap(int& a, int& b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

int indexmin(int a[], int start, int arraysize)
{
    int min = a[start], indexofmin=start;
    
    for(int b=start+1; b<arraysize; b++)
        if(a[b] < min)
        {
            min = a[b];
            indexofmin = b;
            }
            return indexofmin;
}
void sort(int array[], int arraysize)
{
    int indexofnextsmallest;
    
    for(int x=0; x<arraysize; x++){
        indexofnextsmallest = indexmin(array, x, arraysize);
        swap(array[x], array[indexofnextsmallest]);
        }
        }

int search(int array[], int arraysize, int target)
{
    int index=0;
    bool found = false;
    while (!found && index<arraysize){
    
        if(target==array[index])
            found=true;
        else
        index++;
        }
    if(found)
    
           return index;
    else
     
        return -1;
    }

Last edited by shabbir; 31Jul2010 at 09:12.. Reason: Code blocks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
like this

Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
void swap(char **str1_ptr, char **str2_ptr)
{
  char *temp = *str1_ptr;
  *str1_ptr = *str2_ptr;
  *str2_ptr = temp;
}

int indexmin(char **a, int start, int arraysize){
    char *min = a[start];
    int indexofmin=start;
    for(int b=start+1; b<arraysize; b++)
    if(strcmp(a[b] ,min)<0){
        min = a[b];
        indexofmin = b;
    }
    return indexofmin;
}

void sort(char **array, int arraysize){
    int indexofnextsmallest;
    for(int x=0; x<arraysize; x++){
        
        indexofnextsmallest = indexmin(array, x, arraysize);
        
        swap(&array[x], &array[indexofnextsmallest]);
    }
}

int search(char **array, int arraysize, char *target){
    int index=0;
    bool found = false;
    while (!found && index<arraysize){
    if(strcmp(target,array[index])==0)
        found=true;
    else
        index++;
    }
    if(found)
        return index;
    else
        return -1;
} 

int main(){
char **a=(char **) malloc(4*sizeof(char *));
for (int i=0;i<4;i++)
    a[i]=(char *) malloc(50*sizeof(char));

a[0]="xxxxxxx";
a[1]="aaaaa";
a[2]="cccc";
a[3]="bbbbbbbbb";
printf("\nbefore sort....\n");
for (int i=0;i<4;i++)
     printf("%s\n",a[i]);
sort(a,4);
printf("\nafter sort....\n");
for (int i=0;i<4;i++)
     printf("%s\n",a[i]);
    getchar();
}
jj_evans, shabbir likes this
0
jj_evans's Avatar, Join Date: Jun 2010
Light Poster
virxen, thank you very much for your reply! It was extremely helpful. The only thing I don't get is isn't a c-string a multi-dimensional array? if so, shouldnt we use the two brackets when doing the prototype as in void sort(char array[][SIZE]?

btw, I only use the include <iostream> & include <fstream> & using namespace std;
0
Ancient Dragon's Avatar, Join Date: Jul 2010
Go4Expert Member
>>I need to sort an array of cstrings

Are you talking abot the CString c++ class that's in Microsoft's MFC library? Or are you talking about something else? (There is no such thing as standard c or C++ called cstring).