Thread: sorting array

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    sorting array

    Hello!

    Code:
    main() {
    
      char nome[] = "iiooeeuuaa"; 
      
      int i;
        
      for(i = 0; nome[i] != '\0'; i++) { 
        
        int j;
        for(j = 0; j < nome[i] != '\0'; j++) { 
          
    	if(nome[i] < nome[j]){ 
    	  
    	  char aux = nome[i]; 
    	  nome[i] = nome[j]; 
    	  nome[j] = aux; 
    	  
    	} 
          } 
        } 
    
      int j;
      for(j = 0; nome[j] != '\0'; j++){  
    
        printf("%c", nome[j]);
      }
        printf("\n");
    }
    My program objective is to sort a string.

    the output must be: aeiou

    but this is what happens: aaeeiioouu

    thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You've reached your objective. The reason it's doubling every letter is because your string has 2 of each letter in it. Are you saying that your objective is also to eliminate duplicates or...?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    my objective is to sort the string, but the letters can't be duplicated

    eeiiuuooaa is an example

    programming -> agimnopr

  4. #4

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >my objective is to sort the string, but the letters can't be duplicated
    The simplest solution is to sort the string, then remove adjacent duplicates:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sort(char str[])
    {
      int i, j;
      char save;
    
      if (str[0] == '\0')
        return;
    
      for (i = 1; str[i] != '\0'; i++) {
        save = str[i];
        for (j = i; j >= 1 && save < str[j - 1]; j--)
          str[j] = str[j - 1];
        str[j] = save;
      }
    }
    
    void unique(char str[])
    {
      int i, j;
    
      for (i = 0; str[i] != '\0';) {
        if (str[i] == str[i + 1]) {
          for (j = i + 1; str[j] != '\0'; j++)
            str[j] = str[j + 1];
        }
        else
          ++i;
      }
    }
    
    int main(void)
    {
      char str[] = "programming";
    
      sort(str);
      unique(str);
      puts(str);
    
      return 0;
    }
    Notice that I used an insertion sort instead of the bubble sort. It's shorter, easier to understand, and far more efficient in all but the most exceptional circumstances. Here's some homework for you:
    1. Why does the sort function terminate if the first element is a null character?
    2. What purpose does the save variable serve in my sort function?
    3. Why is the increment section of the outer loop in unique empty?
    4. Do either of these functions access indices outside of the string's boundaries?
    5. Make sure that this pair of functions actually works for any input. Consider long strings of duplicates, duplicates at the front and back of the string, and a string with nothing but duplicates.

    With that out of the way, can you think of any other ways to solve the problem? What I showed you is not the best solution by any stretch of the imagination.
    My best code is written with the delete key.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hlo al. i ned a prgm 2 do this
    1. Why does the sort function terminate if the first element is a null character?
    2. What purpose does the save variable serve in my sort function?
    3. Why is the increment section of the outer loop in unique empty?
    4. Do either of these functions access indices outside of the string's boundaries?
    5. Make sure that this pair of functions actually works for any input. Consider long strings of duplicates, duplicates at the front and back of the string, and a string with nothing but duplicates.

    With that out of the way, can you think of any other ways to solve the problem?



    m n a hry. plz email me the prgm thx

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM