Thread: Sorting strings using selection sort method?

  1. #1
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18

    Sorting strings using selection sort method?

    I am trying to write a program to sort the characters in a word alphabetically. For example, if you input 'what', the computer will sort it into 'ahtw'. But, it fails to work. I didn't know why.

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
        char word[50], temp[5];
        int x,y, l, c, value, bigs;
        printf("input a word: ");
        scanf("%s", &word);
        l=strlen(word);
        strlwr(word);
        c=0;
        bigs=0;
        for(x=0; x<l-c; x++)
        {
            for(y=0; y<l; y++)
            bigs=strcmp(word[x], word[x+1])>0?bigs:word[x];
            strcpy(word[l-c], temp);
            strcpy(bigs, word[l-c]);
            strcpy(temp, bigs);
            c++;
        }
        puts("The sorted one is:");
        for(x=0; x<l; x++)
        {
            printf("%c", word[x]);
        }
    }
    This is my first post here, I still don't know anything. So, please help me..

    Thanks in advance

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,750
    You're sorting characters within a string, so perhaps using strcmp() and strcpy() isn't a great idea

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't use strcpy on characters, only on strings. You assign characters normally, that is, using "=".

  4. #4
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    I think you're right. I didn't know that. Thanks

    But, it isn't actually the program I am trying to write. My homework is to write a code to sort some words inputted by user. So far, this is all I got. And then sadly I am stuck

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
        int a, b, c, d, bigs, n, l[15], m;
        char word[15][15], temp[15];
        puts("How many words would you like to input?");
        scanf("%d", &n);
        for(a=0; a<n; a++)
        {
            printf("word no. %d: ", a+1);
            scanf("%s", &word[a]);
            strlwr(word[a]);
            l[a]=strlen(word[a]);
        }
        for(a=0; a<n-1; a++)//to define which word has the longest chars
        {
            m=l[a]<l[a+1]?m:l[a];
        }
        for(a=0; a<m; a++)
        {
            for(b=0; b<n-1; b++)
            {
                d=0;
                for(c=0; c<n-1; c++)
                bigs=word[c][a]<word[c+1][a]?bigs:word[c][a];
                printf("%c", bigs);
            }
        }
    }
    the last looping was meant to find a word having the biggest character (according to ASCII). But to do that we have to look at the first character of each word first, right? That's basically what I am doing. And after we find it, the whole word should be put in the last array. That's something I can't figure out how. We know that 'value' stores the character. But I need to move the whole word, not just the first character. I hope you get what I mean.

    Can somebody help me please?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, welcome to the forum, soawperL25!

    Selection sort is a little tricky with strings, because of the end of string marker - you have to stay away from it, by one char. Also it's a bit tricky anyway, because of the variable "min", and is quite a slow sorter, but here it is:

    Code:
    #define MAX 7
    
    void selectionSort(char word[MAX]); //prototype 
    
    int main() {
       char word[MAX]={"Sierra"};
    
       selectionSort(word);
       printf("%s\n",word);
       return 0;
    }
    
    void selectionSort(char word[MAX])
    {
       int i,j,n=MAX,min;
       char temp;
        
       //note the i<n-2, instead of the normal n-1
       for(i = 0; i < n-2; i++) {
          min = i;                 //set min to the firt index  
          for(j = i+1; j < n-1; j++) {  //and n-1 limit here
             if (word[j] < word[min]) {
                min = j;
             }
          }
     
          if( min != i ) {
             //the line below is an example of a simple debug aid
             //not a part of this algorithm FYI only.
             //printf("i:%d min:%d word[i]:%c  word[min]:%c\n",i,min,word[i],word[min]); getchar();
             temp=word[i];
             word[i]=word[min];
             word[min]=temp; 
    
          }
       }
    }
    P.S. OH, I just saw your last post. I answered your first post, but not your last one.

    If these were your words, how do you want them sorted, finally. Show me, so I'm clear on it.

    bravo
    whisky
    sierra
    alfa
    india
    romeo

    The selection sort above, sorts only a single word, of course.
    Last edited by Adak; 12-07-2013 at 11:58 PM.

  6. #6
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Thank you for your welcome and help I am sorry if you didn't see my latest post , I am writing a code to sort any words. Just any words and the output should become like a dictionary. And I am stuck there.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want a sorted word list - just the words, not the definitions, there are many of them freely available on the net.

    Code:
    #include <stdio.h>
    #include <string.h>  //for strcmp()
    
    #define WORDNUM 7    //how many words there are
    #define SIZE 30      //how large the largest word can be - 29 letters
                         //+1 for the end of string char marker  
    
    void selectionSort(char words[WORDNUM][SIZE]);  
    
    
    int main()
    {
       int i;
       char words[WORDNUM][SIZE]={
          {"sierra"},
          {"hotel"},
          {"whiskey"},
          {"romeo"},
          {"bravo"},
          {"juliet"},
          {"alpha"}
       };
       printf("Original order\n");
       for(i=0;i<WORDNUM;i++) {
         printf("%s\n",words[i]);
       }
       printf("\nSorted order\n");
       
       selectionSort(words);
       
       for(i=0;i<WORDNUM;i++) {
         printf("%s\n",words[i]);
       }
       
       printf("\n");   
       
       return 0;
    }
    void selectionSort(char words[WORDNUM][SIZE])
    {
       int i,j,n=WORDNUM,min;
       char temp[SIZE];
        
       for(i = 0; i < n-1 ; i++) {
          min = i;                 //set min to the firt index  
          for(j = i+1; j < n; j++) {
             if ((strcmp(words[j],words[min]))<0) {
                min = j;
             }
          }
     
          if( min != i ) {
             //printf("i:%d min:%d word[i]:%c  word[min]:%c\n",i,min,word[i],word[min]); getchar();
             strcpy(temp,words[i]);       //copy into temp, words[i]
             strcpy(words[i],words[min]); //copy into words[i], words[min] 
             strcpy(words[min],temp);     //copy into words[min],temp
    
          }
       }
    }

  8. #8
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Thank you very much. It took me a while to understand the code. I have changed some parts of it to meet my needs. But, I have a question, so the computer will find the lowest character in the string instead of the highest one? Because that's what I assume from 'min' variable.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by soawperL25 View Post
    Thank you very much. It took me a while to understand the code. I have changed some parts of it to meet my needs. But, I have a question, so the computer will find the lowest character in the string instead of the highest one? Because that's what I assume from 'min' variable.
    Selection sort can work either way - the normal sort is ascending value (a-z), but descending can be done by a simple change of the comparison it makes.

    In the first example, it found the lowest char in the word, and sorted each word. In the second example, it finds the lowest remaining word, in the list of words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-14-2013, 04:02 PM
  2. Selection Method
    By peacealida in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2008, 08:42 AM
  3. Selection sort, not sorting
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 04-23-2007, 11:17 AM
  4. best sorting method to sort half end of array
    By nitinmhetre in forum C Programming
    Replies: 2
    Last Post: 01-11-2007, 12:18 PM
  5. 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

Tags for this Thread