Program to alphabetizes a list of strings...

This is a discussion on Program to alphabetizes a list of strings... within the C Programming forums, part of the General Programming Boards category; If i want to alphabetizes a list of strings using a sorting algorithm and string comparison functions, do i have ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Program to alphabetizes a list of strings...

    If i want to alphabetizes a list of strings using a sorting algorithm and string comparison functions, do i have to use an array of pointers to achieve this? Since i have to sort the array subscripts first and then swap the elements ( which are strings ) if i'm using bubblesort?

    Hope i explained clear.

    thnx in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An array of pointers-to-chars would probably work the best (ie: the easiest).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    any Other alternatives? Coz i always like to know things done the other way.

    thnx quzah

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    the above reply was mine, forgot to log in...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose you could use a multidimensional array, and copy the data from one array cell to another. You also could use a linked list of strings, and order the list. There's probably a few other ways to do this.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    also, can i ONLY use bubblesort since i can only compare two strings at a time with strcmp() ?

    thnx

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This really isn't that tough. Here's how I strongly reccomend doing this...

    1) Write a function that bubble sorts an array of ints.
    2) Change the array type from int [] to char * []
    3) The temp value you use to swap two values, change it to a char *
    4) The line that goes something like...
    if (array[i] < array[i + 1])
    Change from using the < to using strcmp.

    There is practically no difference between bubble sorting ints and strings, or anything really.
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    And no, practically all sorting methods involve comparing two elements at a time, although bubble sort is really one of the easiest to implement.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *sigh*
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
         char *char_array[255];
         ///some stuff
         qsort(char_array, 0, strlen(char_array), (int(*)(const void *, const void *))strcmp);
         //more code
         return 0;
    }
    ;

  10. #10
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    can i hav the source code for quickSort pls?

    thnx mastr and questionC

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    int IsLess(char * x, char * mid)
    {
    	//control the order of alphebetizing here with < or >
    	if(strcmp( x,mid) < 0)
    		return -1;
    	else
    		return 1;
    }
    
    char * GetRecord(char * a[], int x)
    {
    	char * temp = (char*) malloc (sizeof(char) * 20);
    	temp = a[x];
    	return temp;
    }
    
    void QSort(char * a[], int left, int right)
    {
    	int i = left;
    	int j = right;
    	char * temp = NULL;
    
    	//frees placeholder during recursive call
    	free(temp);
    
    	//get middle record
    	temp = GetRecord(a, (i+j) / 2);
    
    	do
    	{
    		while (IsLess(GetRecord(a,i), temp) < 0 && i < right)
    			i++;
    
    		while (IsLess(temp,GetRecord(a,j)) < 0 && j > left)
    			j--;
    
    		//swap
    		if( i <= j)
    		{
    			temp =  a[i];
    			a[i] =  a[j];
    			a[j] = temp;
    			i++;
    			j--;
    		}
    	}while( i <= j);
    	//recursive calls
    	if(left < j)
    		QSort(a,left,j);
    	if(i < right)
    		QSort(a,i,right);
    }
    
    int main()
    {
    	int i;
    	const int size = 5;
    
    	//jagged array rather than a smooth array[x][size]
    	char * array[size] = {"namev","namee","namex","namea","namen" };
    
    	QSort(array, 0, size - 1);
    
    	printf("Sorted array of strings.\n");
    	for(i = 0; i < size; i++)
    	{
    			printf("%s\n",array[i]);
    	}
    	return 0;
    }
    Last edited by Troll_King; 01-22-2002 at 08:34 AM.

  12. #12
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok, any way to do it WITHOUT using an array of pointers. As i say, i want to know two ways of doing things. If there isn't, then thats ok then.

    TO QuestionC, your way only applies to alphatizing a list of characters right? How can you just change the array from type int to char? And what will be the result of that?

    thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM

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