search
HomeJavajavaTutorialUnderstanding Selection Sort Algorithm (with Examples in Java)

Selection Sort: A Step-by-Step Guide

Selection Sort is a straightforward sorting algorithm. It repeatedly finds the minimum element from the unsorted part of the list and places it at the beginning. This process continues until the entire list is sorted.

How Selection Sort Works

Let's illustrate with an example, sorting this array in ascending order:

Understanding Selection Sort Algorithm (with Examples in Java)

Iteration 1:

The goal is to place the smallest element at the beginning. We start by assuming the first element is the minimum.

Understanding Selection Sort Algorithm (with Examples in Java)

We compare the current minimum with each subsequent element, updating the minimum if a smaller element is found.

Understanding Selection Sort Algorithm (with Examples in Java)

This continues until the actual minimum is identified.

Understanding Selection Sort Algorithm (with Examples in Java)

Finally, we swap the minimum element with the first element.

Understanding Selection Sort Algorithm (with Examples in Java)

The first element is now sorted. Subsequent iterations will only consider the unsorted portion.

Subsequent Iterations:

This process repeats for each remaining unsorted element.

Understanding Selection Sort Algorithm (with Examples in Java)

The algorithm iterates n-1 times (where n is the array's length). After the fifth iteration (for a six-element array), the last element is implicitly sorted.

Selection Sort Implementation (Java):

import java.util.Arrays;

public class SelectionSortTest {
    public static void main(String[] args) {
        int[] arr = {8, 2, 6, 4, 9, 1};
        System.out.println("Unsorted array: " + Arrays.toString(arr));
        selectionSort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }

    public static void selectionSort(int[] arr) {
        int size = arr.length;

        // Iterate through the array size-1 times
        for (int i = 0; i < size - 1; i++) {
            int minIndex = i;
            // Find the minimum element in the unsorted part
            for (int j = i + 1; j < size; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            // Swap the minimum element with the first unsorted element
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
}

Output:

Unsorted array: [8, 2, 6, 4, 9, 1] Sorted array: [1, 2, 4, 6, 8, 9]

Complexity Analysis:

  • Time Complexity: O(n²) in all cases (best, average, worst). The nested loops always execute a fixed number of times regardless of the input order.
  • Space Complexity: O(1). It's an in-place algorithm, requiring constant extra space.

Conclusion:

Selection Sort's O(n²) time complexity makes it inefficient for large datasets. It's best suited for small arrays or situations where simplicity is prioritized over performance.

The above is the detailed content of Understanding Selection Sort Algorithm (with Examples in Java). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool