惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
The GitHub Blog
The GitHub Blog
L
LangChain Blog
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
V
V2EX
MyScale Blog
MyScale Blog

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Selection Sort
Yusuf Aytas · 2008-10-17 · via Yusuf Aytas

Published · 2 min read

Selection Sort, en basit sıralama algoritmalarından biridir ve özellikle algoritma mantığını öğrenirken sıkça kullanılır. Bu yöntemde amaç, dizinin her adımında geri kalan bölümdeki en küçük elemanı bulup, doğru yerine yerleştirmektir. Yani algoritma, her turda dizi üzerinde bir tarama yapar, minimum değeri belirler ve bulunduğu konumu, o turdaki sıralanacak ilk elemanla değiştirir. Mantığı oldukça basittir, fakat işlem adımı çok olduğu için büyük listelerde verimli değildir. Yine de sıralama algoritmalarını anlamak için ideal bir başlangıçtır.

import java.util.Random;

public class SelectionSort {

    // Method to perform selection sort
    public static void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            // Swap the found minimum element with the first element
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    public static void main(String[] args) {
        Random generator = new Random();
        int[] list = new int[10];
        
        for (int i = 0; i < list.length; i++) {
            list[i] = generator.nextInt(1000);
        }

        // Call the selectionSort method
        selectionSort(list);

        // Print the sorted array
        for (int i : list) {
            System.out.println(i + "\t");
        }
    }
}