What is the difference between the two selection sort algorithms?

  • Thread starter magnifik
  • Start date
  • Tags
    Array
In summary: The first one will not work because you check:first iteration:x[0] > x[1]x[0] > x[2]x[0] > x[3]and so on. This will work for first iteration.some iterations later:x[3] > x[1]x[3] > x[2]x[3] > x[3]and so on. This will not work because now you will switch places with numbers already sorted. For example x[3] will for sure be larger than x[1] because x[1] is sorted to be the second smallest number. And when they switch a larger number
  • #1
magnifik
360
0
I'm trying to understand a sorting algorithm (selection sort, to be exact). I started out with this:

public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=1; j<x.length; j++) {
if (x > x[j]) {
//... Exchange elements
int temp = x;
x = x[j];
x[j] = temp;
}
}
}
}

but that didn't work so i made one slight modification by changing j to i + 1

public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=i+1; j<x.length; j++) {
if (x > x[j]) {
//... Exchange elements
int temp = x;
x = x[j];
x[j] = temp;
}
}
}
}

i'm having trouble figuring out what's the difference between the codes?
 
Physics news on Phys.org
  • #2
The first one will not work because you check:
first iteration:
x[0] > x[1]
x[0] > x[2]
x[0] > x[3]
and so on. This will work for first iteration.
some iterations later:
x[3] > x[1]
x[3] > x[2]
x[3] > x[3]
and so on. This will not work because now you will switch places with numbers already sorted.
For example x[3] will for sure be larger than x[1] because x[1] is sorted to be the second smallest number. And when they switch a larger number is placed in x[1].

Second code works because
first iteration:
x[0] > x[1]
x[0] > x[2]
x[0] > x[3]
and so on
some iterations later:
x[3] > x[4]
x[3] > x[5]
x[3] > x[6]
this will work because you discard the already checked values which is not the case in the first code
 
Last edited:
  • #3
When you include code in your post, please put (code) and (/code) tags around it (with the tags in brackets [] though). Doing this preserves your formatting and makes your code easier to read.

I have edited your post to do this.
magnifik said:
I'm trying to understand a sorting algorithm (selection sort, to be exact). I started out with this:
Code:
public static void selectionSort1(int[] x) {
    for (int i=0; i<x.length-1; i++) {
        for (int j=1; j<x.length; j++) {
            if (x[i] > x[j]) {
                //... Exchange elements
                int temp = x[i];
                x[i] = x[j];
                x[j] = temp;
            }
        }
    }
}
but that didn't work so i made one slight modification by changing j to i + 1
Code:
public static void selectionSort1(int[] x) {
    for (int i=0; i<x.length-1; i++) {
        for (int j=i+1; j<x.length; j++) {
            if (x[i] > x[j]) {
                //... Exchange elements
                int temp = x[i];
                x[i] = x[j];
                x[j] = temp;
            }
        }
    }
}
i'm having trouble figuring out what's the difference between the codes?
 

FAQ: What is the difference between the two selection sort algorithms?

What is the purpose of iterating through an array?

The purpose of iterating through an array is to access and manipulate each element in the array. This allows for easier data processing and manipulation.

How do you iterate through an array in JavaScript?

In JavaScript, you can use a for loop or a forEach function to iterate through an array. Both methods allow you to access each element in the array and perform desired actions.

What are the benefits of using a for loop to iterate through an array?

A for loop allows you to have more control over the iteration process, such as specifying the starting and ending index, and the increment value. It also allows for more complex logic within the loop.

What is the difference between iterating through an array using a for loop and using a forEach function?

A forEach function is a higher-order function that takes in a callback function as its argument. It simplifies the syntax for iterating through an array and provides a cleaner and more readable code. However, it may not be suitable for more complex logic.

How do you avoid infinite loops when iterating through an array?

To avoid infinite loops, it is important to make sure the loop has a stopping condition. This can be achieved by using a counter variable or by using the array's length property as the stopping condition. Additionally, it is important to make sure the loop's increment value is appropriate to prevent skipping over elements or getting stuck in an endless loop.

Similar threads

Replies
7
Views
2K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
4
Views
2K
Replies
3
Views
981
Replies
4
Views
2K
Replies
5
Views
2K
Back
Top