Selection sort algorithm problems

1.5k views Asked by At

So I have this code for my selection sort:

public static void selectionSort(int[] arrayToSort){
    int smallest;
    for(int i = 0; i < arrayToSort.length; i++){
        smallest = i;
        for(int j = i+1; j < arrayToSort.length; j++){
            if(arrayToSort[j] < arrayToSort[smallest]){
                smallest = j;
            }
            if(smallest != i){
                int temp = arrayToSort[i];
                arrayToSort[i] = arrayToSort[smallest];
                arrayToSort[smallest] = temp;
            }
        }
    }
}

I am generating a int array with random numbers. My selection sort sometimes does sort the array, sometimes it does "almost" sort the array. The array will mostly be sorted except for a very few numbers which are in wrong places. I can't figure out what goes wrong here, any ideas?

Some test results where the array were not completely sorted:

***NON SORTED***
77
53
27
58
83
***SORTED***
27
53
77
58
83

and

***NON SORTED***
40
87
27
48
82
***SORTED***
27
40
82
48
87
3

There are 3 answers

1
Renzo On BEST ANSWER

You have a part of code inside the inner loop, put it outside the loop;

public static void selectionSort(int[] arrayToSort){
    int smallest;
    for(int i = 0; i < arrayToSort.length; i++){
        smallest = i;
        for(int j = i+1; j < arrayToSort.length; j++){
            if(arrayToSort[j] < arrayToSort[smallest]){
                smallest = j;
            }
        }
        if(smallest != i){
            int temp = arrayToSort[i];
            arrayToSort[i] = arrayToSort[smallest];
            arrayToSort[smallest] = temp;
        }
    }
}

See for instance the algorithm in Wikipedia.

0
Bhagwati Malav On

Here inside 2nd foor loop, if you find any elements which is less than i(or smallest) you are doing swap operation which is not required here. In selection sort, you need to get smallest element from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. Just keep swap outside of 2nd inner loop So that it only does swap operation for smallest element.

for(int i = 0; i < arrayToSort.length; i++){
    smallest = i;
    for(int j = i+1; j < arrayToSort.length; j++){
        if(arrayToSort[j] < arrayToSort[smallest]){
            smallest = j;
        }
    }
    if(smallest != i){
        int temp = arrayToSort[i];
        arrayToSort[i] = arrayToSort[smallest];
        arrayToSort[smallest] = temp;
    }
}
1
Efrain Sanjay Adhikary On

I did this when i need it in college project !

references : selection algoritm with figure

selection sort

public static void selectionSort(int[] arr){  
        for (int i = 0; i < arr.length - 1; i++)  
        {  
            int index = i;  
            for (int j = i + 1; j < arr.length; j++){  
                if (arr[j] < arr[index]){  
                    index = j;//searching for lowest index  
                }  
            }  
            int smallerNumber = arr[index];   
            arr[index] = arr[i];  
            arr[i] = smallerNumber;  
        }  
    }