0

I apologize if my question angers anyone since there is a few Selection Sort related questions already out here in Stack Overflow, but I have looked at nearly all of them, and still find myself confused.

I'm simply trying to write a Selection Sort in the correct format. However, I found many people were using two IF statements in them, while others did not.

What I need to know is if Selection Sorts require two IF Statements, or is the way I have written it acceptable?

What I ended up writing so far was this:

public static int[] selectionsort(int[] anArray){
    int temp;

    for(int i = 0; i < anArray.length; i++){
        for(int leastIndex = i + 1; leastIndex < anArray.length; leastIndex++){
            if(anArray[i] > anArray[leastIndex]){
                temp = anArray[i];
                anArray[i] = anArray[leastIndex];
                anArray[leastIndex] = temp;
            }
        }
    }       
    return anArray;
}

The program does run, but I need to make sure that I have written the Selection Sort correctly.

5
  • What is the fundamental function of selection sort? Have you achieved this is in your code? Ask yourself these questions and write out each step of the method with some potential data set to make sure you're understanding it correctly Commented Sep 8, 2015 at 15:03
  • Selection Sort is an algorithm. A sorting algorithm. Algorithms are like recipes! Did you follow the recipe correctly? Does your function perform are the necessary steps according to how the algorithm is defined? Commented Sep 8, 2015 at 15:05
  • @andrewdleach The fundamental function of a selection sort is to sort an Array I believe, and my code does seem to sort out any array correctly. However, before asking this question on here, I proceeded to perform out the code/calculations on paper to see how the array changed after each iteration, and it seemed very similar to a bubble sort, so I became worried that maybe I hadn't done it right and had written a bubble sort on accident. Which is where I became concerned that maybe I needed the two IF statements I was seeing everywhere. Commented Sep 8, 2015 at 15:14
  • @ofekRon I appreciate your confirmation. I was so worried about all the source code I had seen written with the two IF statements, that it undermined my confidence and I began to doubt whether or not I had done it right. Commented Sep 8, 2015 at 15:16
  • @GL007 after seeing your comment now i understand your concern and you are right this isnt a proper implementation of selection sort Commented Sep 8, 2015 at 15:20

2 Answers 2

1

You did implement in some aspects a selection sort, the only thing i would change in your implementation is the "bubbling" where you bubble the smallest int rather then just keeping its index, this led to some extra swapping, and to confusion with bubble sort, although your implementation is not of bubble sort either.

int[] selectionSort(int[] arr) {
    int minIndex;
    for (int i=0;i<arr.length;i++) {
       minIndex= i;
       for (int j=i+1;j<arr.length;j++) {
          if (arr[minIndex]>arr[j])
             minIndex = j;
       }
       swap(arr,i,j);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Alright, so I can understand all of this, but one thing confuses me. if j = i + 1, what happens when j increments? What does it equal then?
Oh, alright. Sorry lol. I have but one more question though. If I wasn't going to use swap, would I just put there what I currently have inside of my IF statement from my original code?
@GL007 yes, but having a swap function is sometimes usefull and since you are a beginner, id encourage you to use that so that this function can be reused. its just good practice.
Okay, excellent. Thank you for helping me figure out and understand my problem! I appreciate it very much.
0

you need just 1 if statement , that is for comparing the values of first array with 2nd array. For syntax i would prefer you to use it like this.. it will b easier.. In the syntax below I have done Ascending order Selection sort for integers using just 2 for loops.. U can use more if u want for sake of convenience

import java.util.*;
class selctionsort
{
    public static void calc()
    {
        int i , j;
        int temp;
        int n ;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter how many Numbers to be sorted");
        n=sc.nextInt();
        int a[]=new int [n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter The Numbers");
            a[i]=sc.nextInt();
        }
        for(i=0;i<n;i++)
        {
            for (j=i;j<n;j++)
            {
                if(a[i]>a[j])
                {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            System.out.println("Sorted array is " +a[i]);
        }
    }
}

Comments

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.