Sorting a string using bubble sort?!

This is a discussion on Sorting a string using bubble sort?! within the C Programming forums, part of the General Programming Boards category; I want to sort an array of strings. To do this I must use the bubble sort method correct? I ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Sorting a string using bubble sort?!

    I want to sort an array of strings. To do this I must use the bubble sort method correct? I just pull out the first letter of each string convert it to it's ascii value then sort correct?

    but how do I tell it to only pull out the first letter of the string? I would also need to sure the string header and copy the strings instead of using tmp. correct?

    void sort(char a[], int n){
    int i, j

    for (i=0; i < n - 1; ++i)
    for (j = n - 1; i < j; --j)
    if (a[j-1] > &a[j])
    swap(&a[j-1], &a[j]);
    }

    void swap(int *p, int *q)
    {
    int tmp;

    tmp = *p;
    *p = *q;
    *q = tmp;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Use strcmp(). It returns -1, 0, or 1 depending on if the first string is less than, equal to, or greater than the second string.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Look...

    Look my example, I'll sort strings with an index help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ROW 5
    #define SIZE 30
    
    void swap(int idx[], int j);
    void printf_string(const char sStrs[ROW][SIZE], int idx[]);
    
    int main(void)
    {
    	const char sStrs[ROW][SIZE] = {
    		"my fool string",
    		"another fool idiot string",
    		"blah, nothing",
    		"need to say more",
    		"in a year or twice",
    	};
    
    	int idx[ROW] = {0,1,2,3,4};
    	int i,j;
    
    	printf("Before sorting: ");
    	printf_string(sStrs, idx);
    
    	for(i=0; i<ROW; ++i) {
    		for(j=0; j<ROW - (i+1); ++j)
    			if(strcmp(sStrs[idx[j]], sStrs[idx[j+1]]) > 0)
    				swap(idx, j);
    	}
    	
    	printf("\nAfter sorting: ");
    	printf_string(sStrs, idx);
    	system("PAUSE");
    	return 0;
    }
    
    void swap(int idx[], int j)
    {
    	int tmp;
    
    	tmp = idx[j];
    	idx[j] = idx[j+1];
    	idx[j+1] = tmp;
    }
    
    void printf_string(const char sStrs[ROW][SIZE], int idx[])
    {
    	int i;
    
    	for(i=0; i<ROW; ++i)
    		printf("sStrs[%d] = %s\n",i,sStrs[idx[i]]);
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by orbitz
    Use strcmp(). It returns -1, 0, or 1 depending on if the first string is less than, equal to, or greater than the second string.
    No it doesn't. I suggest you read the manual for that function again.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    7.21.4.2 The strcmp function

    Synopsis
    1 #include <string.h>
    int strcmp(const char *s1, const char *s2);

    Description
    2 The strcmp function compares the string pointed to by s1 to
    the string pointed to by s2.

    Returns
    3 The strcmp function returns an integer greater than, equal to,
    or less than zero, accordingly as the string pointed to by s1 is
    greater than, equal to, or less than the string pointed to by s2.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Returns 3 The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.
    Unless I am confused, this is what I said.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Unless I am confused, this is what I said.
    You said -1, 0, or 1. strcmp() doesn't have to return those exact values except for 0. :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  3. Sorting a string
    By Roaring_Tiger in forum C Programming
    Replies: 12
    Last Post: 09-26-2004, 08:12 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21