miatech
miatech

Reputation: 2278

sorting algorithm is not working

I need to sort in ascending order as I add object to my generic class (I'm using strings).

I'm using selection sort, but it is not working.

I don't know if it is the correct way of doing this, so would appreciate the input.

OrderSet class

public class OrderSet<T extends Comparable> implements Set<T> {

    private T[] items;
    private int size;

    public OrderSet()
    {
        items = (T[]) new Comparable[5];        
    }

    @Override
    public void add(T s)
    {
        if(size >= items.length)
        {
            items = grow(items);
        }

        for(int i = 0; i < items.length; i++) 
        {
            if(items[i] == null)
            {
                items[i] = s;
                size++;
                break;
            }
        }

        if(size > 1)
        {
            for (int i = 0; i < size-1; i++)
            {
                for(int j = 1; j < size; j++)
                {
                    T tmp;
                    if (items[i].compareTo(items[j]) > 0)
                    {
                        tmp = items[i];
                        items[i] = items[j];
                        items[j] = tmp;
                    }                    
                }                
            }
        }
    }

    @Override
    public void show()
    {        
        for(T a : items)
        { 
            if(a != null)
                System.out.print(a+", ");            
        }
    }

    public T[] grow(T[] a)
    {
        T[] newA = (T[]) new Comparable[a.length+5];
        System.arraycopy(a, 0, newA, 0, a.length);
        return newA;
    }

}

main

public class Main {

    public static void main(String[] args) throws IOException
    {
        OrderSet<String> s1 = new OrderSet<>();
        WordCount s2 = new WordCount();

        Scanner input = new Scanner("the boy plays in the park with dog");
        while (input.hasNext()) 
        {
            String w = input.next();
            s1.add(w);
        }

        s1.show();

        System.out.println();
    } 
}

Upvotes: 0

Views: 231

Answers (4)

Alok
Alok

Reputation: 1

It's better to use String compare concept in this case

class City {

public static void main (String args[])
{ 
int i,j;
String temp;
String s[] = new String[6];
for(i=0; i<6; i++)
 s[i]=args[i];
for(i=0;i<6;i++){
  for(j=0;j<6-i-1;j++){
 if(s[j].compareTo(s[j+1])>0)
 {
   temp=s[j];
   s[j]=s[j+1];
   s[j+1]=temp;
 } 
}
 }
 for(i=0; i<6; i++)
 {
 System.out.println(s[i]);
}
}}

Upvotes: 0

ardent
ardent

Reputation: 2513

What you seem to be doing is a bubble sort as you add in items, the generic form of a selection sort is as follows:

for(int i = 0; i<arr.length - 1; i++)
{
   int smallest = i;
   for(int j = i + 1; j< arr.length; j++)
   {
       if(arr[j].compareTo(arr[smallest]) > 0)
           smallest = j;
   }
   if(smallest < arr.length && smallest != i)
       swap(arr[i], arr[smallest]);
}

You could do it swapping the largest into the last index, but this should work as well. Note, swap is just a placeholder psuedocode for the actual swapping.

Upvotes: 1

Yuri
Yuri

Reputation: 81

Use java.util.TreeSet< T >, which implements SortedSet< T >.

Upvotes: 0

DrLudwig3
DrLudwig3

Reputation: 421

I think it's your Sort Algorithm that is wrong. Ardentsonata is right, you use a Bubblesort algorithm but there is a mistake:

for (int i = 0; i < size-1; i++) {
    for(int j = 1; j < size; j++){
        T tmp;
        if (items[i].compareTo(items[j]) > 0) {
            tmp = items[i];
            items[i] = items[j];
            items[j] = tmp;
        }                    
    }                
}

The Problem is the start value of the second loop, you want to check if any other element - except the ones you already sorted is bigger than the element you want to sort at the moment. So your second loop needs this head:

for(int j = (i+1); j < size; j++)

so you really sort the array.

Otherwise you were uncontrollable switching your values aroung, because after you switched something to the second slot you switch it back in the next iteration.

Hope that helps !

Upvotes: 1

Related Questions