-1

Would I just rewrite this twice over or is there a more efficient way I can go about doing this? How can I get items to sort in the same pattern(low to high) as price?

public class W14_3 {

    public static void main(String [] args){

        double[] price={73.25, 48.0, 345.0, 152.35, 196.50};
        String[] items={"bag", "stationary", "books", "shoes","clothing"};

        selectionSort(price , items);
        for(int i = 0; i<price.length; i++)
        System.out.println(price[i]);

        for(int j=0; j<items.length; j++){
        System.out.println(items[j]);
        }

    }

    public static void selectionSort(double[] P , String[] I ){

        for(int startIndex=0; startIndex <P.length-1; startIndex++)
        {
            double min = P[startIndex];
            int indexOfMin = startIndex;

            for(int j= startIndex +1; j< P.length; j++)
                if(P[j] < min)
                {
                    min =P[j];
                    indexOfMin=j;
                    }
            P[indexOfMin] = P[startIndex];
            P[startIndex] = min;
                }
        }
    }
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Don't have things in separate arrays: build a class to hold the item and its corresponding price, and have just one array of those. – Andy Turner May 02 '17 at 21:10

1 Answers1

0

Considering you're almost there then I'd suggest you turn your arrays into a class where the numbers and strings are members of the class. e.g.

public class someName {
    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    private String item;
    private double price;

    public someName(String item, double price){
        this.item = item;
        this.price = price;
    }
}

Now going on to the selection sort, we'll need to alter yours a bit.

public static void selectionSort(someName[] arr){
        for(int i = 0; i < arr.length-1; i++){
            int minIndex = i;  // smallest element index
            for(int j = i + 1; j < arr.length; j++){
                if(arr[j].getPrice() < arr[i].getPrice()){  // find smallest element
                    if(arr[j].getPrice() < arr[minIndex].getPrice())
                        minIndex = j; // update smallest element index
                }
            }

            if(i != minIndex){  // swap
                someName temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
}

then within your main method:

public static void main(String[] args) {
        someName[] someIdentifier = new someName[5];
        someIdentifier[0] =  new someName("bag",73.25);
        someIdentifier[1] =  new someName("stationary",48.0);
        someIdentifier[2] =  new someName("books",345.0);
        someIdentifier[3] =  new someName("shoes",152.35);
        someIdentifier[4] =  new someName("clothing",196.50);
        selectionSort(someIdentifier);
        for (someName item : someIdentifier) {
            System.out.println(item.getItem() + " : " + item.getPrice());
        }
}

For additional information regarding how the Selection Sort works and why I've done it that way, refer to my previous post on Selection Sort.

Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126