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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Vercel News
Vercel News
F
Fortinet All Blogs
月光博客
月光博客
G
Google Developers Blog
博客园 - Franky
GbyAI
GbyAI
The Cloudflare Blog
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
小众软件
小众软件
腾讯CDC
B
Blog
量子位
V
V2EX
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News

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