Java - Selection Sort Algorithm

后端 未结 17 1612
我在风中等你
我在风中等你 2020-12-17 04:26

I have some questions about selection sort.I\'m a little bit confused.

 int [] arr = {5,4,3,2,1}; // This is my array
    int min = 0;

    for(int i = 0;i&l         


        
相关标签:
17条回答
  • 2020-12-17 04:48

    Assume a lowest element, which requires scanning the all elements and then swap it to the first position.

    private static void selectionSortMethod(int[] arr) {
    
            for (int i = 0; i < arr.length - 1; i++) {  //no of iterations for array length
                for (int j = i + 1; j < arr.length; j++) {  //starting from next index to lowest element(assuming 1st index as lowest)
                    if (arr[i] > arr[j]){
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
    
            }
             //print 
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i]+" ");
            }
        }
    
    0 讨论(0)
  • 2020-12-17 04:49

    What is wrong is that in your inner loop you should update your index, using the strategy you follow of doing the swap in the inner loop I made a working selection sort:

    import java.util.Arrays;
    
    public class SelectionSort {
    
      public static void main(String[] args) {
        int[] input = new int[] {5,2,4,6,1,3};
        System.out.println( Arrays.toString(selectionSort(input)) );
      }
    
      public static int[] selectionSort(int[] input) {
        int length = input.length;
        int minimumValue = Integer.MAX_VALUE;
    
        for (int i = 0; i < length; ++i) {
            // find the minimumValue when j > i and swap it  with input[i] location
            for (int j =i; j < length; ++j) {
              if (input[j] <= minimumValue ) {
                minimumValue = input[j];
                input[j] = input[i];
                input[i] = minimumValue;
              }
            }
            minimumValue = Integer.MAX_VALUE;
        }
        return input;
      }
    
    }
    

    I added to github.

    0 讨论(0)
  • 2020-12-17 04:49

    As mentioned earlier, you're not updating your 'min' variable in the inner loop. The objective of the inner loop is to find the index of the smallest element. You should also move the 'swap' to the outer loop. Below is the Selection Sort pseudo code:

    Selection Sort
    Inputs:
      A: an array
      n: the number of elements in A to sort
    
    Procedure SELECTION-SORT (A, n)
    1. For i = 0 to n – 1:
      A. Set minIndex to i.
      B. For j = i + 1 to n:
        i. If A[j] < A[minIndex], then set minIndex to j. // Add this
      C. Swap A[i] with A[minIndex]. // Move this to outside of the inner loop
    

    Take a look at the link to my blog below to see a full explanation of the Selection Sort algorithm. There are implementations in Java, C++, Python, and JavaScript.

    http://brianredd.com/algorithm/selection-sort

    0 讨论(0)
  • 2020-12-17 04:51

    Selection sort is a algorithm that forming array elements in ANSI order or DENSI order. Best case, Average case and Worst case time complexity is (n2). Selection sort is not better for sorting an array... Selection sort implementation is given below:

    import java.util.Scanner;
    
    class selection{
          public static void main(String a[]){
    
                 Scanner sc=new Scanner(System.in);
                 System.out.print("size :");
                 int n=sc.nextInt();
    
                 int i,j,tmp,minVal;
    
                 int[] ar=new int[n];
    
                 for(i=0;i<n;i++){
                      ar[i]=sc.nextInt();
                 }
    
                 for(i=0;i<(n-1);i++){
                      minVal=ar[i];
                      for(j=(i+1);j<n;j++){
                              if(minVal>ar[j]){
                                      minVal=ar[j];
                                      tmp=ar[i];
                                      ar[i]=minVal;
                                      ar[j]=tmp;
                              }
                      }
    
                }
    
                for(i=0;i<n;i++){
                      System.out.print(ar[i]+"  ");
                }
      }
    
    0 讨论(0)
  • 2020-12-17 04:53

    Correct:

    public class Test {
    
    public static void main(String args[]){
        int[] arr = {5,4,3,2,1}; // This is my array
        int min = 0;
    
        for(int i = 0;i<arr.length;i++)
        {
            //Assume first element is min
            min = i;
            for(int j = i + 1;j<arr.length;j++)
            {
                if(arr[j] < arr[min]) { min = j;}
            }
            int temp = arr[i];
            arr[i] = arr[min];
            arr[min] = temp;
            System.out.println(arr[i]);//I print the in ascending order 
        }
    }
    
    }
    

    About the min part: it just refers to the index of whatever is the current min. You move on down the array until you meet the new min, and set min to that index. So 5 is the minimum number [min =0] until you see 4 [so now min =1] but then you compare 3 to whatever is stored at 4 [when min=1] and then realize that you should set min=2.... etc. etc.

    0 讨论(0)
  • 2020-12-17 04:54

    You should first find the minimum instead of assuming the first element is the minimum

    int[] array = {5, 4, 3, 2, 1};
    for ( int i = 0; i < array.length; i++ ) {
    
      //find minimum, starting from index i
      int minIndex = i;
      int min = array[i];
      for ( int j = i + 1; j < array.length; j++ ) {
        if ( array[ j ] < min ) {
          minIndex = j;
          min = array[j];
        }
      }
    
      // now move the smallest element to the front, and the element at index i to the index of the minimal element
      int temp = array[ i ];
      array[ i ] = min;
      array[ minIndex ] = temp;
    }
    
    0 讨论(0)
提交回复
热议问题