Mike Jacoub
Mike Jacoub

Reputation: 1

How to use selection sort in objects and classes

I'm creating two classes called stop watch and random numbers, which I have already done, but I needed to create a test program that would measure the execution time of sorting 100,000 numbers using selection sort. I know how to create a selection sort, I just don't know how to take the random numbers class and put it together with the selection sort, I get the error message "incompatible types random numbers cannot be converted to int" I hope someone can help me.

My random numbers class

import java.util.Random;

public class randomnumbers {

Random ran1 = new Random();

private int size;

 public randomnumbers(){
       size = 100000; 
    }

public int getSize(){
    return size;
}

public void setSize(int newSize){
size = newSize;

}

public int [] createArray(int [] size){

    for (int i = 0; i < size.length; i++){
        size[i] = ran1.nextInt();

    }
   return size;
}

public static void printArray (int [] array){

      for (int i = 0; i < array.length; i++){

          if (i < 0){

           System.out.println(array[i] + " ");
        }
     }
   } 
}

My test Program

public static void main (String [] args){

    // Create a StopWatch object 
    StopWatch timer = new StopWatch(); 

    //create random numbers
    randomnumbers numbers = new randomnumbers();

    //Create the size of the array
    numbers.getSize();

    // Invoke the start method in StopWatch class 
    timer.start(); 

    //sort random numbers
    selectionSort();


    // Invoke the stop method in StopWatch class 
    timer.stop(); 


    // Display the execution time 
    System.out.println("The execution time for sorting 100,000 " + 
    "numbers using selection sort: " + timer.getElapsedTime() + 
    " milliseconds"); 



}

// selectionSort performs a selection sort on an array  
    public static void selectionSort(int[] array) { 
        for (int i = 0; i < array.length - 1; i++) { 
        int min = array[i]; 
        int minIndex = i; 


    for (int j = i + 1; j < array.length; j++) { 
        if (array[j] < min) { 
        min = array[j]; 
        minIndex = j; 
    } 
 } 


    if (i != minIndex) { 
    array[minIndex] = array[i]; 
    array[i] = min; 
            } 
        } 
    }  
 }

Upvotes: 0

Views: 101

Answers (1)

Emperor Orionii
Emperor Orionii

Reputation: 752

Where exactly are you getting "incompatible types random numbers cannot be converted to int" error?

There are multiple issues with the code:

  1. Unconventional naming
  2. size field is in randomnumbers class is used as actual array size in constructor but in createArray it's overshadowed with a parameter of the same name but different type and meaning.
  3. You are not passing any array to selectionSort in Main. This is where I get compile error on your code.
  4. printArray has if (i < 0) condition that is false for all ran1.nextInt() numbers so it will not print anything.

Feeding selectionSort with numbers.createArray(new int[numbers.getSize()]) compiles and ends up sorting the array.

Upvotes: 1

Related Questions