Home > other >  Problem in sorting array using Selection Sort algorithm
Problem in sorting array using Selection Sort algorithm

Time:10-01

When passing an array in the selection function, I'm getting the required sorted array only for positive real numbers but for the negative numbers it is failing to do the same.

    static void selection(int[] array) {
            for (int i = 0; i < array.length; i  ) {
                int last = array.length - i - 1;
                int max= getMaxIndex(array, 0, last);
                swap(array, max, last);
            }
       }
        static void swap(int[] arr, int first, int second) {
            int temp = arr[first];
            arr[first] = arr[second];
            arr[second] = temp;
        }
        static int getMaxIndex(int[] arr, int start, int last) {
            int max = start;
            for (int i = 0; i < last; i  ) {
                if (arr[max] < arr[i]) {
                    max = i;
                }
            }
            return max;
        }

CodePudding user response:

You don't state whether or not you are trying to sort in an ascending or descending manner but I went ahead and assumed ascending. The issue is that you are sorting the array in reverse, thus your method:

static int getMaxIndex(int[] arr, int start, int last) {
        int max = start;
        for (int i = 0; i < last; i  ) {
            if (arr[max] < arr[i]) {
                max = i;
            }
        }
        return max;
    }

returns the wrong value. It should be:

static int getMaxIndex(int[] arr, int last) {
    int max = last;
    for (int i = 0; i < last; i  ) {
        if (arr[max] < arr[i]) {
            max = i;
        }
    }
    return max;
}

The start parameter was unneeded in your implementation. Max should start on the current index that we are sorting.

Your solution is rather complicated however. When doing selection sort normally, we want to go search for the next lowest value from the i 1 position to the end of the array and swap it with the value at the ith position of the array if it is smaller. This implementation makes more sense to me:

static void selectionSort(int[] array) {
    for (int i = 0; i < array.length; i  ) {
        int maxIndex = i;
        for(int j = i 1; j < array.length;j  ){
            if(array[j]< array[maxIndex])maxIndex = j;
        }
        if(maxIndex != i){
            int temp = array[i];
            array[i] = array[maxIndex];
            array[maxIndex]= temp;
        }
    }
}
 

CodePudding user response:

Your algorithm also fails anytime the last few indexes of the initial array are close to where they should be in the final sorted array.

The problem here is that you are calculating the last index for the sub array correctly but then inside of getMaxIndex you loop up to last - 1 (i < last) meaning the final index of the sub array is not actually being checked to see if it is the max index. Changing getMaxIndex loop condition to i <= last fixes the problem.

  • Related