Thread: String selection sort fuction

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    String selection sort fuction

    Code:
    #include <stdio.h>
    #include <string.h>
    int get_min_range(const char *list, int first,int last)
    {
    	int i, j, min_pos;
    	for (i=first; i<=last; i++) {  
    		min_pos = i;
    		for (j=first; j<=last; j++) 
    		if( strcmp( list[j], list[i]) < 0)
    			min_pos = j;
    		}
    	printf("%d---\n", list[min_pos]);
    	return(min_pos);	
    }
    
    void select_sort(const char *list, int n)		
    {	
    	int fill, index_of_min;	
    	char temp[20];
    	
    	for (fill=0;fill<n-1;fill++){		
    		index_of_min = get_min_range(list, fill, n-1);
    		
    		/*Exchange elements at fill and index_of_min					*/
    		if(fill != index_of_min){
    			strcpy(temp, list[index_of_min]);
    			strcpy(list[index_of_min],list[fill]);
    			strcpy(list[fill],temp);
    		}
    	}
    }
    int main(void)
    {
    	char list[20];
    	printf("Enter word> ");
    	scanf("%s", list);
    	select_sort(list,strlen(list));	
    	return(0);
    }
    The code is not working.I couldn't found where is the problem.It gives Segmentation fault,

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Use a debugger and trace the values. Most likely you are forgetting the '\0' when you are allocating memory.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by claudiu View Post
    Use a debugger and trace the values. Most likely you are forgetting the '\0' when you are allocating memory.
    I installed Nemiver.When it calls select_sort funtion, it gives "Target received a signal: SIGSEGV, Segmentation fault"
    And also compiler gives
    make 9.4.1.o (in directory: /home/mustafa/Documents/My_C_Programs/programmings)
    cc -c -o 9.4.1.o 9.4.1.c
    9.4.1.c: In function ‘get_min_range’:
    9.4.1.c:9:3: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
    /usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
    9.4.1.c:9:3: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast
    /usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
    9.4.1.c: In function ‘select_sort’:
    9.4.1.c:26:4: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
    /usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
    9.4.1.c:27:4: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
    /usr/include/string.h:128:14: note: expected ‘char * __restrict__’ but argument is of type ‘char’
    9.4.1.c:27:4: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
    /usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
    9.4.1.c:28:4: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
    /usr/include/string.h:128:14: note: expected ‘char * __restrict__’ but argument is of type ‘char’
    Compilation finished successfully.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's because you are calling strcmp and strcpy incorrectly. Check their documentation pages. They take pointers as parameters not individual characters.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by claudiu View Post
    That's because you are calling strcmp and strcpy incorrectly. Check their documentation pages. They take pointers as parameters not individual characters.
    It is described in an example(in book).Book gives a comparison(numeric to string) and says write the string selection sort function described in example.
    I mean strcmp and strcpy from book's example...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you need to decide if you want to exchange whole chunks of letters or just single letters. (Your comment suggests the second.) If you just want to swap single letters, then strcpy is the wrong tool for the job (you should just use equal sign). If you want to swap chunks, you will need to make sure that each chunk is \0 terminated (which you don't appear to do).

    EDIT: The flip side of that is that the syntax you are using would be marvelous IF list were, not an array of chars, but an array of char* instead.
    Last edited by tabstop; 05-10-2011 at 10:21 AM.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, if what you posted previously is from your book I suggest you set the book on fire and get another one. There are plenty of recommendations in the book thread on this forum.

    If what you posted is your own work, then I suggest, like I said above, to look at the documentation pages for strcpy and strcmp and see the examples of their usage there.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by claudiu View Post
    Ok, if what you posted previously is from your book I suggest you set the book on fire and get another one. There are plenty of recommendations in the book thread on this forum.

    If what you posted is your own work, then I suggest, like I said above, to look at the documentation pages for strcpy and strcmp and see the examples of their usage there.
    Name of the book is Problem Solving and Program Design in C.
    What will you suggest.Will I start coding from beginning or can it be corrected.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    I have edited the code.It works well, but sometimes makes wrong sort.Compiler didn't give errors.But, I am not sure about code.
    Code:
    #include <stdio.h>
    #include <string.h>
    int get_min_range(const char *list, int first,int last);
    void select_sort(char *list, int n);	
    int main(void)
    {
    	char list[20];
    	printf("Enter word> ");
    	scanf("%s",list);
    	select_sort(list,strlen(list));
    	printf("%s", list);
    	return(0);
    }
    int get_min_range(const char *list, int first,int last)
    {
    	int min_pos;
    		min_pos = first;
    			//if(strcmp(&list[last], &list[first]) < 0)
    			if ( list[last] < list[first])
    			min_pos = last;
    	return(min_pos);
    }
    
    void select_sort(char *list, int n)	
    {	
    	int fill, index_of_min;	
    	char temp[20];
    	
    	for (fill=0;fill<n;fill++){		
    		index_of_min = get_min_range(list, fill, n-1);		
    		/*Exchange elements at fill and index_of_min					*/
    		if(fill != index_of_min){
    			strncpy(temp, &list[index_of_min], 1);
    			strncpy(&list[index_of_min], &list[fill], 1);
    			strncpy(&list[fill], temp, 1);
    		}
    	}
    }

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Can you supply examples of sorts that work and ones that don't?

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by mike65535 View Post
    Can you supply examples of sorts that work and ones that don't?
    not working:
    Enter word> asdfg
    agdfs
    working:
    Enter word> deneme
    deeemn

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It is wrong to use strcmp or strncpy inside the sorting algorithm or get_min_range here. You are not sorting strings, but sorting chars.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why does your get_min_range only look at the first and the last elements? What happens if your smallest element is somewhere in the middle?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. selection sort
    By kingkobra in forum C++ Programming
    Replies: 0
    Last Post: 12-03-2009, 02:55 PM
  2. Selection Sort
    By hopeolicious in forum C++ Programming
    Replies: 0
    Last Post: 03-13-2005, 12:17 PM
  3. Selection Sort
    By Drew in forum C++ Programming
    Replies: 16
    Last Post: 09-18-2003, 07:19 AM
  4. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  5. Selection-sort
    By Nutshell in forum C Programming
    Replies: 10
    Last Post: 01-11-2002, 04:32 AM