0

I have implemented a code to sort this array using Selection Sort. The code seems to be fine, but it doesn't sort the array perfectly. This is the array to be sorted, {20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2}

Here is the code I implemented;

public class Main {

    public static void main(String[] args) {
        int[] arr = {20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2};
        System.out.println("Unsorted Array: \t");
        StringBuffer sb = new StringBuffer();
        for (Object s : arr) {
            sb.append(s);
            sb.append(" ");
        }
        String str = sb.toString();
        System.out.println(str);


        selectionSort(arr);
        System.out.println("Sorted Array: \t");
        printArray(arr);
    }

    public static void selectionSort(int[] arr) {
        int n = arr.length;
        // one by one move boundary of unsorted subarray
        for (int i = 0; i < n - 1; i++) {
            // find the minimum element in unsorted array
            int min = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
                //swapping the found min value with the first element
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
            }
        }
    }

    public static void printArray(int[] arr) {
        StringBuffer sb = new StringBuffer();
        for (Object s : arr) {
            sb.append(s);
            sb.append(" ");
        }
        String str = sb.toString();
        System.out.println(str);
    }
}

And here is the output I received;

enter image description here

I have no idea why it is not sorting out properly, Would love to have some help.

2 Answers 2

1

The swap comes after the inner loop. Like,

public static void selectionSort(int[] arr) {
    int n = arr.length;
    // one by one move boundary of unsorted subarray
    for (int i = 0; i < n - 1; i++) {
        // find the minimum element in unsorted array
        int min = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[min]) {
                min = j;
            }
        }
        // swapping the found min value with the first element
        int temp = arr[min];
        arr[min] = arr[i];
        arr[i] = temp;
    }
}

Just changing that (and adding a printArray) I get

Unsorted Array:     
[20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2]
Sorted Array:   
[2, 5, 6, 7, 14, 17, 19, 20, 22, 39, 42, 46, 47, 48, 51]
Sign up to request clarification or add additional context in comments.

Comments

0
class selecion_sort{
    static void selection(int[] arr){
        for (int i=0;i<arr.length-1;++i){
//initialise the element to a variable named 'key' and traverse the element
//using another for loop and compare the the smallest number among the elements 
//of the array.... 
            int key=i;
            for (int j=i+1;j<arr.length;++j){
                if (arr[key]>arr[j]){
                    key=j;                                                         
                }
            }
//swap the smallest element with the first element and as the iteration grows 
//the swapping continues as 1st,2nd,3rd elements.......
            int temp=arr[key];
            arr[key]=arr[i];
            arr[i]=temp;
        }
    }                     
//this method prints the elements of an array.....                                                     
    static void print(int[] arr){
        for (int i=0;i<arr.length;++i){
            System.out.print(" "+arr[i]);
        }
    }
    public static void main(String[] args){
        int[] arr={23,54,65,12,87,45,10,45};
     selection(arr);
    print(arr);
    }
}

2 Comments

This does not answer why [is the code presented] not sorting out properly?
please elaborate........

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.