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

推荐订阅源

WordPress大学
WordPress大学
A
About on SuperTechFans
量子位
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
U
Unit 42
D
DataBreaches.Net
博客园 - Franky
D
Docker
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - 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
Quick Sort
Yusuf Aytas · 2009-03-10 · via Yusuf Aytas

Published · 2 min read

Birçok bilgisayar bilimi dersinde Quick Sort’u defalarca işledik, analiz ettik, hatta sınavlarda bile çözdük. Ama çoğu zaman öğrendiğimiz şeyleri pratiğe dökmeden bırakıyoruz. İşte ben de uzun süre yazmayı ertelediğim bu algoritmayı, sonunda kendi elimle uygulamak istedim. Quick Sort, ortalama O(n log n) karmaşıklığıyla büyük diziler için en verimli sıralama yöntemlerinden biridir. Mantığı basit: Bir pivot seçer, diziyi pivot’tan küçük ve büyük elemanlar olarak ikiye ayırır ve aynı işlemi her alt diziye yeniden uygular. Bu rekürsif yapı, algoritmayı hem hızlı hem de öğretici kılar.

Aşağıdaki kod, Quick Sort’un temel prensiplerini açık bir şekilde gösteren birebir uygulamasıdır.

public class QuickSortExample {

    // Quick Sort algoritmasi, ortalama olarak O(n log n) zamanda calisir.
    // Buyuk diziler icin oldukca etkili bir siralama yontemidir.
    public static void quicksort(int[] array, int left, int right) {
        if (left < right) {
            // Partitioning index'i bulunur ve quicksort bu indekse gore iki alt diziye uygulanir
            int partitionIndex = partition(array, left, right);

            // Sol alt diziyi ayri, sag alt diziyi ayri siralayarak recursive olarak quicksort uygulanir
            quicksort(array, left, partitionIndex - 1);
            quicksort(array, partitionIndex + 1, right);
        }
    }

    private static int partition(int[] array, int left, int right) {
        int pivot = array[right];  // Pivot olarak son eleman secilir
        int i = (left - 1);  // Index of smaller element

        for (int j = left; j < right; j++) {
            // Eger mevcut eleman pivot'tan kucuk veya esitse
            if (array[j] <= pivot) {
                i++;

                // i'nci ve j'nci elemanlari degistirir
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        // Pivot elemanini dogru konumuna yerlestirir
        int temp = array[i + 1];
        array[i + 1] = array[right];
        array[right] = temp;

        return i + 1;
    }

    // Test icin main metodu
    public static void main(String[] args) {
        int[] array = { 10, 7, 8, 9, 1, 5 };
        int n = array.length;
        quicksort(array, 0, n - 1);
        System.out.println("Siralanmis dizi: ");
        for (int i = 0; i < n; ++i)
            System.out.print(array[i] + " ");
        System.out.println();
    }
}