Results 1 to 6 of 6
  1. #1
    eIO
    eIO is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Help with algorithm for sorting an array of Strings

    Hi!
    I the beginning programmer.
    I want the sorted not repeating! sequence in outpute file.
    this code: sorted sequence in outpute file.(Alphabetically)
    Java Code:
    class SortString 
    {
      static String arr[] = 
        {"ddd", "sss", "aaa", "ggg", "aaa", "bbb",    
         "ddd", "ccc",  };
    
      public static void main(String args[]) 
     
      { 
        for (int j = 0; j < arr.length; j++) 
        {
        
          for (int i = j + 1; i < arr.length; i++) 
          { 
            if (arr[i].compareTo(arr[j]) < 0) 
            { 
              String t = arr[j];
              arr[j] = arr[i];
              arr[i] = t;
            } 
          }  
          System.out.println(arr[j]);
     
        } 
      
      }
    }

    My algorithm solving not repeating sequence:
    Java Code:
    for (int j = 0; j < arr.length; j++);
         {
         int j = 0;
     	for (int b = j + 1; b < arr.length +1; b++)
     	{
          if  ( arr[j].equals(arr[b]) )
         {
        	  
        	 clear( arr[b]);
        	  }
        }
       }
    i think - clear(arr[b]) - delete repeating lines
    Such algorithm is possible?
    (If yes)How to connect the top program to this algorithm?
    Please write me the answer shown in a code.
    If it is not difficult
    please hepl me.
    Last edited by Fubarable; 07-10-2011 at 11:09 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator edit: changed thread title from "help me" to "Help with algorithm for sorting an array of Strings".

    To the OP, please make your thread title informative if you want to help us help you.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    delete repeating lines
    For arrays, to remove an internal element means that you have to move all of the elements above the one to be removed down one. Also there must be a counter that keeps track of the number of good elements in the array.

  4. #4
    eIO
    eIO is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    here
    Java Code:
    String prevItem = null;
    for (final String item : arr){
        if (!item.equals(prevItem)){
            System.out.println(item);    
        }
        prevItem = item;
    }
    anyone can write an example of the counter?
    just wondering.

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You should be able to use the Array's sort method to put it in alphabetical order. Then check if the value of the array index is equal to the value of the index-1.
    Java Code:
    for(int i=0; i<myArray.length();i++)
    {
         if (myArray[i].equals(myArray[i-1])
              //dont print
         else
             //print
    }
    Of course you have to check to see if you can even do that check without getting a indexOutOfBounds error, but I'm not going to write your program for you.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    Java Code:
    for(int i=1; i<myArray.length();i++)  // Start with index = 1
    {
         if (myArray[i].equals(myArray[i-1])  // Compare this(i) to previous element(i-1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •