1

This is the code I have for my selection sort program, I want to know if there's any way of improving the code without using additional methods or classes.

public class Selection_Sort {


public static void main(String[] args) {
int arr[]={234,151,123,4,5342,76,48};
int min=0; int temp;
for(int i=0;i<=arr.length-1;i++){
    min=i;
    for (int k=i+1;k<arr.length;k++){
        if(arr[k]<arr[i]){
            temp=arr[i];
            arr[i]=arr[k];
            arr[k]=temp;
        }
    }
}
for (int j=0;j<=arr.length-1;j++)
    System.out.println(arr[j]+" ");

}

}

7 Answers 7

3
public static void main(String[] args) {
    int arr[]={234,151,123,4,5342,76,48};
    int arrLength = arr.length;
    for(int i=0;i<arrLength-1;i++){
        int min=i;
        for (int k=i+1;k<arrLength;k++){
            if(arr[k]<arr[min]){
                min = k;
            }
        }
        if (i != min) {
            int temp=arr[i];
            arr[i]=arr[min];
            arr[min]=temp;
        }
    }
    for (int j=0;j<arrLength;j++) {
        System.out.println(arr[j]+" ");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you are using the bubblesort algorithm which is very slow. If you want to improve your code, i would recommend to use an algorithm like ripplesort or quicksort.

Comments

0

Slight improvement should be like this :

int arrayLength = arr.length;  
// Then use it in conditional statement of for loop.

So that it won't invoke length property of Array every time in loop. For small number of loops it doesn't impact much but it will help to reduce the time when loops are more or number of iteration of loop are more.

1 Comment

arr.length invocation is of O(1) complexity in Java, so extracting array length in a variable and then using it in the conditional statement doesn't look like an improvement
0
  1. The value of the local variable min is not used
  2. k <= arr.length-1

-->

k < arr.length

Comments

0

Use this

class Selection {
    public static void main(String[] args) {
        int arr[]={234,151,123,4,5342,76,48}; /* arr[0] to arr[n-1] is the array to sort */
        int lowest, i, j;
        for(i = 0 ; i < arr.length-1; i++) {  /* advance the position through the entire array */
            lowest = i;                       /* assume the min is the first element */
            for(j = i+1 ; j < arr.length; j++) { /* if this element is less, then it is the new minimum */ 
                if(arr[j] < arr[lowest]) {
                    lowest = j;                  /* found new minimum; remember its index */
                }
            }
            if(lowest != i) {                   /* lowest is the index of the minimum element. Swap it with the current position */
                int temp = arr[i];
                arr[i] = arr[lowest];
                arr[lowest] = temp;
            }
        }
        for (int k = 0; k <= arr.length-1 ; k++) {
        System.out.println(arr[k] + " ");
        }
    }
}

This is the selection sort algorithm you asked.

Comments

0

Here is original Selection sort implementation. The implementation in question in not using min to perform the swap operation.

public static void sort(int[] arr) {
        int min=-1;

        for (int i = 0; i < arr.length; i++) {
            min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
            }
        }
}

Comments

-1
public class JavaApplication55 {

    public static void main(String[] args) {

        int[] array ={234,435,567,768,123,456,789,789,5670,6789};
     for(int j =0;j< array.length;j++){  
        for(int i =j+1;i < array.length;i++ ){
        int temp;

            if(array[j]>array[i]){

            temp =array[j];
            array[j] =array[i];

            array[i] =temp;

            }

            else{}


        }}





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

            System.out.println(array[k]);

        }

    }
enter code here

}

2 Comments

hi,whts bad in it if you have any solution paste it here
It's not necessarily bad, but if you add some explanation, then people will be more likely to accept it, because then there is something to back it. See here: stackoverflow.com/questions/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.