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

推荐订阅源

量子位
博客园_首页
Google DeepMind News
Google DeepMind News
博客园 - Franky
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
罗磊的独立博客
IT之家
IT之家
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
腾讯CDC
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

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");
        }
    }
}