>

>
1x
go to beginning previous frame pause play next frame go to end

Pengurutan adalah masalah klasik tentang mengubah urutan elemen-elemen (yang bisa dibandingkan, seperti bilangan bulat, bilangan pecahan, strings, dsb) dari sebuah larik (senarai) ke urutan tertentu (menaik, tidak-menurun (menaik atau datar), menurun, tidak-menaik (menurun atau datar), terurut secara abjad, dsb).


Ada banyak algoritma-algoritma pengurutan, dan masing-masing memiliki kekuatan-kekuatan maupun kekurangan-kekurangan.


Pengurutan biasanya digunakan sebagai masalah pembuka dalam berbagai kelas-kelas Ilmu Komputer untuk menjelaskan berbagai ide-ide algoritma.


Tanpa kehilangan makna umum, kami menggunakan asumsi bahwa kita akan mengurutkan hanya bilangan-bilangan bulat, tidak harus unik, ke dalam urutan tidak-menurun di visualisasi ini. Cobalah klik Bubble Sort untuk animasi contoh pengurutan daftar 5 bilangan-bilangan bulat yang tidak beratur (dengan duplikat) diatas.


Remarks: By default, we show e-Lecture Mode for first time (or non logged-in) visitor.
If you are an NUS student and a repeat visitor, please login.

🕑

Sorting problem has a variety of interesting algorithmic solutions that embody many Computer Science ideas:

  1. Comparison versus non-comparison based strategies,
  2. Iterative versus Recursive implementation,
  3. Divide-and-Conquer paradigm (e.g., Merge Sort or Quick Sort),
  4. Best/Worst/Average-case Time Complexity analysis,
  5. Randomized Algorithms, etc.

Pro-tip 1: Since you are not logged-in, you may be a first time visitor (or not an NUS student) who are not aware of the following keyboard shortcuts to navigate this e-Lecture mode: [PageDown]/[PageUp] to go to the next/previous slide, respectively, (and if the drop-down box is highlighted, you can also use [→ or ↓/← or ↑] to do the same),and [Esc] to toggle between this e-Lecture mode and exploration mode.

🕑

Ketika sebuah larik (bilangan bulat) A terurut, banyak masalah-masalah yang berhubungan dengan A menjadi mudah (atau lebih mudah):

  1. Mencari nilai spesifik v di dalam larik A,
  2. Mencari nilai terkecil/terbesar atau nilai terkecil/terbesar ke-k di larik (statis) A,
  3. Mengetes keunikan dan menghapus duplikat dari larik A,
  4. Menghitung seberapa banyak nilai spesifik v muncul dalam larik A,
  5. Himpunan Irisan/Gabungan dari larik A dan larik terurut lainnya yaitu B,
  6. Mencari pasangan target xA dan yA sehingga x+y sama dengan nilai target z,
  7. Menghitung banyak nilai di larik A yang berada pada jangkauan [lo..hi], dsb.

Diskusi: Dalam kelas-kelas nyata, instruktor bisa membahas aplikasi-aplikasi ini dengan lebih detail.


Pro-tip 2: We designed this visualization and this e-Lecture mode to look good on 1366x768 resolution or larger (typical modern laptop resolution in 2021). We recommend using Google Chrome to access VisuAlgo. Go to full screen mode (F11) to enjoy this setup. However, you can use zoom-in (Ctrl +) or zoom-out (Ctrl -) to calibrate this.

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.


Pro-tip 3: Other than using the typical media UI at the bottom of the page, you can also control the animation playback using keyboard shortcuts (in Exploration Mode): Spacebar to play/pause/replay the animation, / to step the animation backwards/forwards, respectively, and -/+ to decrease/increase the animation speed, respectively.

🕑

Ada dua aksi yang anda dapat lakukan di visualisasi ini.

🕑

Aksi pertama adalah tentang mendefinisikan masukan anda sendiri, sebuah larik/daftar A yang:

  1. Betul-betul acak,
  2. Acak tapi terurut (dalam urutan tidak-menurun atau tidak-menaik),
  3. Acak tetapi hampir terurut (dalam urutan tidak-menurun atau tidak-menaik),
  4. Acak dan memiliki banyak duplikat-duplikat (sehingga jangkauan bilangan-bilangan bulatnya kecil), atau
  5. Didefinisikan oleh anda sendiri.

Dalam mode Eksplorasi, anda dapat bereksperimen dengan berbagai algoritma-algoritma pengurutan yang tersedia di visualisasi ini untuk mengetahui masukan-masukan terbaik dan terjelek untuk algoritma-algoritma tersebut.

🕑

Aksi kedua adalah yang terpenting: Jalankan algoritma pengurutan yang sedang aktif dengan meng-klik tombol "Urutkan".


Ingat bahwa anda dapat mengubah algoritma yang aktif dengan meng-klik singkatan di sisi atas dari halaman visualisasi ini.

🕑

Lihat visualisasi/animasi dari algoritma pengurutan terpilih disini.


Tanpa kehilangan makna umum, kami hanya menunjukkan bilangan-bilangan bulat didalam visualisasi ini dan tujuan kami adalah untuk mengurutkan mereka dari status awal ke status terurut tidak-menurun. Ingat, tidak-menurun berarti pada umumnya urutan menaik (atau membesar), tetapi karena bisa ada duplikat-duplikat, bisa ada garis datar/sama diantara dua bilangan-bilangan bulat bersisian yang bernilai sama.

🕑

Di bagian atas halaman ini, anda akan melihat daftar berbagai algoritma-algoritma pengurutan yang biasanya diajarkan dalam kelas-kelas Ilmu Komputer. Untuk mengaktifkan algoritma tertentu, pilihlah singkatan dari nama algoritma yang bersangkutan sebelum meng-klik "Urutkan".


Untuk memfasilitasi keberagaman, kami akan mengacak algoritma yang aktif setiap kali halaman ini ditampilkan.


Enam algoritma pertama adalah berbasis-pembandingan sedangkan dua terakhir tidak. Kita akan membahas ide ini di pertengahan Kuliah Maya ini.


Tiga algoritma ditengah adalah algoritma pengurutan yang bersifat rekursif sedangkan sisanya biasa diimplementasikan secara iteratif.

🕑

Supaya muat di layar, kami menyingkat nama-nama algoritma menjadi tiga karakter saja:

  1. Algoritma-Algorithma Pengurutan berbasis-pembandingan:
    1. BUB - Bubble Sort,
    2. SEL - Selection Sort,
    3. INS - Insertion Sort,
    4. MER - Merge Sort (implementasi rekursif),
    5. QUI - Quick Sort (implementasi rekursif),
    6. R-Q - Quick Sort Acak (implementasi rekursif).
  2. Algoritma-Algoritma Pengurutan tidak berbasis-pembandingan:
    1. COU - Counting Sort,
    2. RAD - Radix Sort.
🕑

Kita akan membahas tiga algoritma-algoritma pengurutan berbasis-pembandingan di beberapa slides berikutnya:

  1. Bubble Sort,
  2. Selection Sort,
  3. Insertion Sort.

Mereka disebut berbasis-pembandingan karena mereka membandingkan pasangan elemen-elemen dari sebuah larik dan menentukan apakah akan menukar posisi mereka atau tidak.


Ketiga algoritma-algoritma pengurutan ini adalah yang termudah untuk diimplementasikan tetapi juga bukan yang paling efisien, karena mereka berjalan dalam kompleksitas waktu O(N2).

🕑

Sebelum kita memulai diskusi berbagai algoritma-algoritma pengurutan, adalah ide bagus untuk membahas dasar-dasar dari analisa algoritma secara asimptotik, sehingga anda dapat mengikuti diskusi-diskusi dari berbagai algoritma-algoritma pengurutan O(N^2), O(N log N), dan spesial O(N) nantinya.


Section ini bisa dilompati jika anda sudah mengetahui topik ini.

🕑

Anda butuh untuk sudah mengerti/mengingat hal-hal ini:
-. Logaritma dan Eksponen, misalkan log2(1024) = 10, 210 = 1024
-. Deret Aritmetika, misalkan 1+2+3+4+…+10 = 10*11/2 = 55
-. Deret Geometri, misalkan 1+2+4+8+..+1024 = 1*(1-211)/(1-2) = 2047
-. Fungsi Linear/Kuadratik/Kubik, misalkan f1(x) = x+2, f2(x) = x2+x-1, f3(x) = x3+2x2-x+7
-. Fungsi Langit-Langit, Lantai, dan Absolut, misalkan ceil(3.1) = 4, floor(3.1) = 3, abs(-7) = 7

🕑

Analisis algoritma adalah sebuah proses untuk mengevaluasi secara keras sumber daya (waktu dan ruang) yang dibutuhkan oleh sebuah algoritma dan merepresentasikan hasil dari evaluasi tersebut dengan sebuah formula (sederhana).


Kebutuhan waktu/ruang dari sebuah algoritma juga disebut sebagai kompleksitas waktu/ruang dari algoritma tersebut, masing-masing.


Untuk modul ini, kita lebih memfokuskan kepada persyaratan waktu dari berbagai algoritma-algoritma pengurutan.

🕑

We can measure the actual running time of a program by using wall clock time or by inserting timing-measurement code into our program, e.g., see the code shown in SpeedTest.cpp | py | java.


However, actual running time is not meaningful when comparing two algorithms as they are possibly coded in different languages, using different data sets, or running on different computers.

🕑

Instead of measuring the actual timing, we count the # of operations (arithmetic, assignment, comparison, etc). This is a way to assess its efficiency as an algorithm's execution time is correlated to the # of operations that it requires.


See the code shown in SpeedTest.cpp | py | java and the comments (especially on how to get the final value of variable counter).


Knowing the (precise) number of operations required by the algorithm, we can state something like this: Algorithm X takes 2n2 + 100n operations to solve problem of size n.

🕑

If the time t needed for one operation is known, then we can state that algorithm X takes (2n2 + 100n)t time units to solve problem of size n.


However, time t is dependent on the factors mentioned earlier, e.g., different languages, compilers and computers, the complexity of the operation itself (addition/subtraction is easier/faster to compute than multiplication/division), etc.


Therefore, instead of tying the analysis to actual time t, we can state that algorithm X takes time that is proportional to 2n2 + 100n to solving problem of size n.

🕑

Asymptotic analysis is an analysis of algorithms that focuses on analyzing problems of large input size n, considers only the leading term of the formula, and ignores the coefficient of the leading term.


We choose the leading term because the lower order terms contribute lesser to the overall cost as the input grows larger, e.g., for f(n) = 2n2 + 100n, we have:
f(1000) = 2*10002 + 100*1000 = 2.1M, vs
f(100000) = 2*1000002 + 100*100000 = 20010M.
(notice that the lower order term 100n has lesser contribution).

🕑

Misalkan dua algoritma-algoritma memiliki 2n2 dan 30n2 sebagai term-term tertinggi.


Meskipun waktu sebenarnya akan berbeda karena konstanta-konstanta yang berbeda, laju-laju pertumbuhan dari waktu eksekusi adalah sama.


Dibandingkan dengan algoritma lain dengan term tertinggi n3, perbedaan dari laju pertumbuhan adalah faktor yang jauh lebih dominan.


Maka, kita bisa melepaskan koefisien dari term tertinggi ketika mempelajari kompleksitas algoritma.

🕑

Jika algoritma A membutuhkan waktu proporsional terhadap f(n), kita bilang bahwa algoritma A berada dalam order f(n).


Kita menulis bahwa algoritma A mempunyai kompleksitas waktu O(f(n)), dimana f(n) adalah fungsi laju pertumbuhan untuk algoritma A.

🕑

Secara matematis, sebuah algoritma adalah O(f(n)) jika ada sebuah konstanta k dan sebuah bilangan bulat positif n0 sehingga algoritma A membutuhkan tidak lebih dari k*f(n) unit-unit waktu untuk menyelesaikan sebuah masalah dengan ukuran n ≥ n0, yaitu, jika ukuran masalah lebih besari dari n0, algoritma A (selalu) dibatasi dari atas oleh formula sederhana k*f(n) ini.


Big-O Notation

Catat bahwa: n0 dan k tidak unik dan bisa ada beberapa banyak f(n) valid yang memungkinkan.

🕑

Dalam analisa asimtotik, sebuah formula bisa disederhanakan menjadi sebuah term tunggal dengan koefisien 1.


Term tersebut disebut sebagai term pertumbuhan (tingkat pertumbuhan, urutan pertumbuhan, urutan besar).


Term-term pertumbuhan yang paling umum dapat diurutkan dari yang paling cepat ke paling lambat sebagai berikut:
O(1)/waktu konstan < O(log n)/waktu logaritmik < O(n)/waktu linear <
O(n log n)/waktu kuasilinear < O(n2)/waktu kuadratik < O(n3)/waktu kubik <
O(2n)/waktu eksponensial < O(n!)/waktu juga-eksponensial < ∞ (misalkan, perulangan tak berakhir).


Catat bahwa ada beberapa kompleksitas-kompleksitas waktu yang umum lainnya yang tidak ditunjukkan (lihat juga visualisasi di slide berikutnya)).

🕑
Common Growth Terms

Kita akan melihat tiga laju-laju pertumbuhan yang berbeda O(n2), O(n log n), dan O(n) sepanjang sisa dari modul pengurutan ini.

🕑
Diberikan sebuah larik berisi N elemen-elemen, Bubble Sort akan:
  1. Membandingkan pasangan yang bersebelahan (a, b),
  2. Menukar pasangan tersebut bila tidak pada urutan yang seharusnya (dalam kasus ini, ketika a > b),
  3. Ulangi Langkah 1 dan 2 hingga kita sampai di akhir larik
    (pasangan terakhir adalah elemen ke (N-2) dan (N-1) karena kita menggunakan indeks basis-0),
  4. Pada saat ini, elemen terbesar akan terletak pada posisi terakhir.
    Kita lalu mengurangi N dengan 1 dan kembali ke Langkah 1 sampai kita mendapat N = 1.

Tanpa basa-basi lagi, mari coba Bubble Sort pada larik contoh kecil [29, 10, 14, 37, 14].


Anda akan melihat animasi 'seperti-gelembung' jika anda membayangkan elemen-elemen yang besar 'menggelembung keatas' (atau sebenarnya 'bergerak ke sisi kanan dari larik').

🕑
method bubbleSort(array A, integer N) // the standard version
for each R from N-1 down to 1 // repeat for N-1 iterations
for each index i in [0..R-1] // the 'unsorted region', O(N)
if A[i] > A[i+1] // these two are not in non-decreasing order
swap(a[i], a[i+1]) // swap them in O(1)

Comparison and swap require time that is bounded by a constant, let's call it c. Then, there are two nested loops in (the standard) Bubble Sort. The outer loop runs for exactly N-1 iterations. But the inner loop runs get shorter and shorter:

  1. When R=N-1, (N−1) iterations (of comparisons and possibly swaps),
  2. When R=N-2, (N−2) iterations,
    ...,
  3. When R=1, 1 iteration (then done).

Thus, the total number of iterations = (N−1)+(N−2)+...+1 = N*(N−1)/2 (derivation).

Total time = c*N*(N−1)/2 = O(N^2).


See the code shown in SortingDemo.cpp | py | java.

🕑

Bubble Sort is actually inefficient with its O(N^2) time complexity. Imagine that we have N = 105 numbers. Even if our computer is super fast and can compute 108 operations in 1 second, Bubble Sort will need about 100 seconds to complete.


However, it can be terminated early, e.g., on the small sorted ascending example shown above [3, 6, 11, 25, 39], Bubble Sort can terminates in O(N) time.


The improvement idea is simple: If we go through the inner loop with no swapping at all, it means that the array is already sorted and we can stop Bubble Sort at that point.


Discussion: Although it makes Bubble Sort runs faster in general cases, this improvement idea does not change O(N^2) time complexity of Bubble Sort... Why?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Diberikan sebuah larik berisi N elemen dan L = 0, Selection Sort akan:

  1. Menemukan posisi X dari elemen terkecil dalam interval [L...N-1],
  2. Tukar item ke-X dengan item ke-L,
  3. Naikkan batas bawah L sebesar 1 dan kembail ke Langkah 1 sampai L = N-2.

Mari coba Selection Sort pada larik contoh kecil yang sama [29, 10, 14, 37, 13].


Tanpa kehilangan makna umum, kita juga dapat mengimplementasikan Selection Sort secara terbalik:
Temukan posisi dari elemen terbesar Y dan tukar dengan elemen terakhir.

🕑
method selectionSort(array A[], integer N)
for each L in [0..N-2] // O(N)
let X be the index of the minimum element in A[L..N-1] // O(N)
swap(A[X], A[L]) // O(1), X may be equal to L (no actual swap)

Total: O(N2) — To be precise, it is similar to Bubble Sort analysis.


See the code shown in SortingDemo.cpp | py | java.

🕑

Quiz: How many (real) swaps are required to sort [29, 10, 14, 37, 13] by Selection Sort?

4
1
3
2
🕑

Insertion sort is similar to how most people arrange a hand of poker cards. Insertion Sort Illustration

  1. Start with one card in your hand,
  2. Pick the next card and insert it into its proper sorted order,
  3. Repeat previous step for all cards.

Let's try Insertion Sort on the small example array [6, 2, 10, 7].

🕑
method insertionSort(array A[], integer N)
for i in [1..N-1] // O(N)
let X be A[i] // X is the next item to be inserted into A[0..i-1]
for j from i-1 down to 0 // this loop can be fast or slow
if A[j] > X
A[j+1] = A[j] // make a place for X
else
break
A[j+1] = X // insert X at index j+1

See the code shown in SortingDemo.cpp | py | java.

🕑

Perulangan luar (outer loop) dijalankan N−1 kali, sepertinya ini cukup jelas.

Tetapi berapa kali perulangan dalam (inner loop) dilakukan tergantung pada masukan:

  1. Dalam kasus terbaik, larik sudah terurut menaik dan (a[j] > X) selalu salah
    Sehingga tidak ada pergeseran data yang terjadi dan perulangan dalam (inner loop) berjalan dalam O(1),
  2. Dalam kasus terjelek, larik terurut terbalik (menurun) dan (a[j] > X) selalu benar
    Pemasukkan (insertion) selalu terjadi di depan larik dan perulangan dalam (inner loop) berjalan dalam O(N).

Sehingga, waktu terbaik adalah O(N × 1) = O(N) dan waktu terburuk adalah O(N × N) = O(N2).

🕑

Quiz: What is the complexity of Insertion Sort on any input array?

O(1)
O(N)
O(N log N)
O(N^2)


Tanya instruktor anda bila anda tidak mengerti tentang hal ini atau baca catatan yang sejenis di slide ini.

🕑

Kita akan membahas dua (dan setengah) algoritma-algoritma berbasis-pembandingan segera:

  1. Merge Sort,
  2. Quick Sort dan versi acaknya (yang hanya memiliki satu perubahan).

Algoritma-algoritma pengurutan ini biasanya diimplementasikan secara rekursif, menggunakan paradigma pemecahan masalah Divide and Conquer, dan berjalan dalam waktu O(N log N) untuk Merge Sort dan O(N log N) secara ekspektasi untuk Quick Sort Acak.


PS: Versi tidak-acak dari Quick Sort sayangnya berjalan dalam waktu O(N2).

🕑

Diberikan sebuah larik berisi N elemen, Merge Sort akan:

  1. Menggabungkan tiap pasang elemen (yang secara default terurut) menjadi larik-larik berukuran 2 elemen,
  2. Menggabungkan tiap pasang larik-larik tersebut menjadi larik-larik berukuran 4 elemen, Ulangi proses ini...,
  3. Langkah terakhir: Menggabungkan 2 larik-larik yang sudah terurut dengan jumlah N/2 elemen (untuk memudahkan diskusi, kita berasumsi bahwa N genap) untuk mendapatkan larik yang terurut penuh dengan jumlah N elemen.

Ini hanyalah ide general dan kita membutuhkan beberapa detail tambahan sebelum kita bisa membahas bentuk sebenarnya dari Merge Sort.

🕑

Kita akan membahas dengan detil algoritma Merge Sort ini dengan pertama-tama membahas sub-rutin terpentingnya: Proses penggabungan (merge) dalam O(N).


Diberikan dua larik yang sudah terurut, A dan B, dengan ukuran N1 dan N2, kita dapat dengan efisien menggabungkan mereka mejadi satu larik terurut yang lebih besar dengan ukuran N = N1+N2, dalam waktu O(N).


Ini dapat diraih dengan cara membandingkan elemen terdepan dari kedua larik dan mengambil yang terkecil dari dua itu setiap waktu. Tetapi, sub-rutin merge yang sederhana tapi cepat O(N) ini akan membutuhkan larik tambahan untuk melakukan proses penggabungan ini dengan benar.

🕑
method merge(array A, integer low, integer mid, integer high)
// subarray1 = a[low..mid], subarray2 = a[mid+1..high], both sorted
int N = high-low+1
create array B of size N // discuss: why do we need a temp array b?
int left = low, right = mid+1, bIdx = 0
while (left <= mid && right <= high) // the merging
if (A[left] <= A[right])
B[bIdx++] = A[left++]
else
B[bIdx++] = A[right++]
while (left <= mid)
B[bIdx++] = A[left++] // leftover, if any
while (right <= high)
B[bIdx++] = A[right++] // leftover, if any
for (int k = 0; k < N; ++k)
A[low+k] = B[k]; // copy back

Try Merge Sort on the example array [1, 5, 19, 20, 2, 11, 15, 17] that have its first half already sorted [1, 5, 19, 20] and its second half also already sorted [2, 11, 15, 17]. Concentrate on the last merge of the Merge Sort algorithm.

🕑

Sebelum kita lanjutkan, mari berbicara tentang Divide and Conquer (disingkat sebagai D&C), sebuah paradigma pemecahan masalah yang kuat.


Algoritma Divide and Conquer menyelesaikan (sejenis) masalah — seperti masalah pengurutan kita — dalam beberapa langkah-langkah berikut:

  1. Langkah Divide: Membagi masalah yang asli dan besar menjadi masalah-masalah yang lebih kecil dan secara rekursif menyelesaikan masalah-masalah yang lebih kecil tersebut,
  2. Langkah Conquer: Gabungkan hasil-hasil dari masalah-masalah yang lebih kecil tersebut untuk menghasilkan hasil dari masalah yang asli dan besar.
🕑

Merge Sort adalah algoritma pengurutan bersifat Divide and Conquer.


Langkah pembagian (divide) mudah saja: Bagi larik yang sekarang menjadi dua bagian (sama besar jika N genap atau satu sisi satu elemen sedikit lebih besar jika N ganjil) dan lalu secara rekursif mengurutkan kedua bagian tersebut.


Langkah penaklukkan (conquer) adalah langkah yang melakukan usaha terbesar: Gabungkan dua bagian (yang sudah terurut) untuk membentuk larik yang terurut, menggunakan sub-rutin penggabungan (merge) yang dibahas sebelumnya.

🕑
method mergeSort(array A, integer low, integer high)
// the array to be sorted is A[low..high]
if (low < high) // base case: low >= high (0 or 1 item)
int mid = (low+high) / 2
mergeSort(a, low , mid ) // divide into two halves
mergeSort(a, mid+1, high) // then recursively sort them
merge(a, low, mid, high) // conquer: the merge subroutine

See the code shown in SortingDemo.cpp | py | java.

🕑

Dibandingkan dengan apa yang biasanya ditampilkan di banyak buku-buku teks Ilmu Komputer yang dicetak (karena buku-buku sifatnya statis), eksekusi sebenarnya dari Merge Sort tidak membagi kedua sub-larik level demi level, tetapi Merge Sort akan secara rekursif mengurutkan sub-larik kiri terlebih dahulu sebelum mengurutkan sub-larik kanan.


Lebih detailnya, menjalankan Merge Sort pada larik contoh [7, 2, 6, 3, 8, 4, 5], Merge Sort akan merekursi ke [7, 2, 6, 3], lalu [7, 2], lalu [7] (sebuah elemen tunggal, yang sudah terurut secara default), kembali, rekursi ke [2] (terurut), kembali, lalu menggabungkan [7, 2] menjadi [2, 7], sebelum Merge Sort memproses [6, 3] dan selanjutnya.

🕑

Dalam Merge Sort, usaha terbanyak dilakukan dalam langkah conquer/merge karena langkah divide sebenarnya tidak melakukan apa-apa (dianggap O(1)).


Ketika kita memanggil merge(a, low, mid, high), kita memproses k = (high-low+1) elemen.
Akan ada paling banyak k-1 pembandingan.
Ada k perpindahan dari larik asli a ke larik temporer b dan k perpindahan lainnya untuk menyalin balik.
Secara total, jumlah operasi dalam operasi sub-rutin merge adalah < 3k-1 = O(k).


Pertanyaan penting berikutnya adalah berapa kali sub-rutin merge ini dipanggil?

🕑
The Recursion Tree of Merge Sort
🕑

Level 1: 2^0=1 panggilan kepada merge() dengan N/2^1 elemen masing-masing, O(2^0 x 2 x N/2^1) = O(N)
Level 2: 2^1=2 panggilan kepada merge() dengan N/2^2 elemen masing-masing, O(2^1 x 2 x N/2^2) = O(N)
Level 3: 2^2=4 panggilan kepada merge() dengan N/2^3 elemen masing-masing, O(2^2 x 2 x N/2^3) = O(N)
...
Level (log N): 2^(log N-1) (atau N/2) panggilan kepada merge() dengan N/2^log N (or 1) elemen masing-masing, O(N)


Ada log N level dan di setiap level, kita melakukan usaha sebesar O(N), sehingga kompleksitas waktu seluruhnya adalah O(N log N). Nanti, kita akan melihat bahwa ini adalah algoritma pengurutan (berbasis-pembandingan) yang sudah optimal, atau dengan kata lain, kita tidak bisa membuat algoritma lebih baik dari Merge Sort ini.

🕑

Bagian bagus dari Merge Sort yang paling penting adalah garansi performa O(N log N), tidak tergantung pada urutan asli dari masukan. Dengan kata lain, tidak ada kasus tes yang bisa membuat Merge Sort berjalan lebih lambat dari O(N log N) untuk segala larik dengan N elemen.


Merge Sort sangat cocok untuk mengurutkan masukan yang sangat besar karena O(N log N) bertumbuh jauh lebih lambat dari algoritma-algoritma pengurutan yang membutuhkan waktu O(N2) seperti yang dibahas sebelumnya.


Tetapi ada juga beberapa bagian yang tidak bagus dari Merge Sort. Pertama, sebenarnya tidak mudah untuk mengimplementasikan Merge Sort dari awal (tetapi sebenarnya kita tidak perlu melakukannya). Kedua, Merge Sort membutuhkan tempat tambahan sebesar O(N) dalam proses penggabungan, sehingga tidak efisien secara memori dan tidak di-tempat. Ngomong-ngomong, bila anda tertarik untuk melihat apa yang sudah dilakukan untuk mengatasi bagian yang tidak bagus dari Merge Sort (klasik) ini, anda bisa membaca ini


Merge Sort juga adalah algoritma pengurutan yang stabil. Diskusi: Kenapa?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Quick Sort adalah algoritma pengurutan lain yang juga berbasis Divide and Conquer (satu lagi yang telah dibahas di Kuliah Maya ini adalah Merge Sort).


Kita akan melihat bahwa versi deterministik, tidak acak dari Quick Sort bisa memiliki kompleksitas waktu yang jelek, yaitu O(N2) pada masukan jahat (adversary) sebelum kita melanjutkan pembahasan dengan versi acak yang lebih bisa dipakai nantinya.

🕑

Divide step: Choose an item p (known as the pivot)
Then partition the items of A[i..j] into three parts: A[i..m-1], A[m], and A[m+1..j].
A[i..m-1] (possibly empty) contains items that are smaller than (or equal to) p.
A[m] = p, i.e., index m is the correct position for p in the sorted order of array a.
A[m+1..j] (possibly empty) contains items that are greater than (or equal to) p.
Then, recursively sort the two parts.


Conquer step: Don't be surprised... We do nothing :O!


If you compare this with Merge Sort, you will see that Quick Sort D&C steps are totally opposite with Merge Sort.

🕑

We will dissect this Quick Sort algorithm by first discussing its most important sub-routine: The O(N) partition (classic version).


To partition A[i..j], we first choose A[i] as the pivot p.

The remaining items (i.e., A[i+1..j]) are divided into 3 regions:

  1. S1 = A[i+1..m] where items are ≤ p,
  2. S2 = A[m+1..k-1] where items are ≥ p, and
  3. Unknown = A[k..j], where items are yet to be assigned to either S1 or S2.

Discussion: Why do we choose p = A[i]? Are there other choices?


Harder Discussion: If A[k] == p, should we put it in region S1 or S2?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Initially, both S1 and S2 regions are empty, i.e., all items excluding the designated pivot p are in the unknown region.


Then, for each item A[k] in the unknown region, we compare A[k] with p and decide one of the three cases:

  1. If A[k] > p, put A[k] into S2,
  2. If A[k] < p, put A[k] into S1,
  3. If A[k] == p, throw a coin and put A[k] into S1/S2 if it lands head/tail, respectively.

These three cases are elaborated in the next two slides.


Lastly, we swap A[i] and A[m] to put pivot p right in the middle of S1 and S2.

🕑
Case when a[k] ≥ p, increment k, extend S2 by 1 item
🕑
Case when a[k] < p, increment m, swap a[k] with a[m], increment k, extend S1 by 1 item
🕑
int partition(array A, integer i, integer j)
int p = a[i] // p is the pivot
int m = i // S1 and S2 are initially empty
for (int k = i+1; k <= j; ++k) // explore the unknown region
if ((A[k] < p) || ((A[k] == p) && (rand()%2 == 0))) { // case 2+3
++m
swap(A[k], A[m]) // exchange these two indices
// notice that we do nothing in case 1: A[k] > p
swap(A[i], A[m]) // final step, swap pivot with a[m]
return m // return the index of pivot
🕑
method quickSort(array A, integer low, integer high)
if (low < high)
int m = partition(a, low, high) // O(N)
// A[low..high] ~> A[low..m–1], pivot, A[m+1..high]
quickSort(A, low, m-1); // recursively sort left subarray
// A[m] = pivot is already sorted after partition
quickSort(A, m+1, high); // then sort right subarray

See the code shown in SortingDemo.cpp | py | java.

🕑

Try Quick Sort on example array [27, 38, 12, 39, 29, 16]. We shall elaborate the first partition step as follows:
We set p = A[0] = 27.
We set A[1] = 38 as part of S2 so S1 = {} and S2 = {38}.
We swap A[1] = 38 with A[2] = 12 so S1 = {12} and S2 = {38}.
We set A[3] = 39 and later A[4] = 29 as part of S2 so S1 = {12} and S2 = {38,39,29}.
We swap A[2] = 38 with A[5] = 16 so S1 = {12,16} and S2 = {39,29,38}.
We swap p = A[0] = 27 with A[2] = 16 so S1 = {16,12}, p = {27}, and S2 = {39,29,38}.


After this, A[2] = 27 is guaranteed to be sorted and now Quick Sort recursively sorts the left side A[0..1] first and later recursively sorts the right side A[3..5].

🕑

First, we analyze the cost of one call of partition.


Inside partition(A, i, j), there is only a single for-loop that iterates through (j-i) times. As j can be as big as N-1 and i can be as low as 0, then the time complexity of partition is O(N).


Similar to Merge Sort analysis, the time complexity of Quick Sort is then dependent on the number of times partition(A, i, j) is called.

🕑

When the array A is already in ascending order, e.g., A = [5, 18, 23, 39, 44, 50], Quick Sort will set p = A[0] = 5, and will return m = 0, thereby making S1 region empty and S2 region: Everything else other than the pivot (N-1 items).

🕑

On such worst case input scenario, this is what happens:


Worst Case analysis of Quick Sort

The first partition takes O(N) time, splits A into 0, 1, N-1 items, then recurse right.
The second one takes O(N-1) time, splits A into 0, 1, N-2 items, then recurse right again.
...
Until the last, N-th partition splits A into 0, 1, 1 item, and Quick Sort recursion stops.


This is the classic N+(N-1)+(N-2)+...+1 pattern, which is O(N2), similar analysis as the one in this Bubble Sort analysis slide...

🕑

Skenario terbaik dari Quick Sort terjadi ketika partition selalu membagi larik menjadi dua bagian yang sama besar, seperti Merge Sort.


Ketika itu terjadi, kedalaman rekursi hanyalah O(log N).


Karena setiap level membutuhkan O(N) pembandingan, kompleksitas waktu adalah O(N log N).


Coba Quick Sort di contoh larik masukan yang telah dipersiapkan secara khusus [4, 1, 3, 2, 6, 5, 7].

Pada prakteknya, hal ini jarang terjadi, sehingga kita harus memikirkan cara yang lebih baik: Quick Sort Acak.

🕑

Same as Quick Sort except just before executing the partition algorithm, it randomly select the pivot between A[i..j] instead of always choosing A[i] (or any other fixed index between [i..j]) deterministically.


Mini exercise: Implement the idea above to the implementation shown in this slide!


Running Random Quick Sort on this large and somewhat random example array a = [3,44,38,5,47,15,36,26,27,2,46,4,19,50,48] feels fast.

🕑

It will take about 1 hour lecture to properly explain why this randomized version of Quick Sort has expected time complexity of O(N log N) on any input array of N elements.


In this e-Lecture, we will assume that it is true.


If you need non formal explanation: Just imagine that on randomized version of Quick Sort that randomizes the pivot selection, we will not always get extremely bad split of 0 (empty), 1 (pivot), and N-1 other items. This combination of lucky (half-pivot-half), somewhat lucky, somewhat unlucky, and extremely unlucky (empty, pivot, the rest) yields an average time complexity of O(N log N).


Discussion: For the implementation of Partition, what happen if A[k] == p, we always put A[k] on either side (S1 or S2) deterministically?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Kita akan membahas dua algoritma-algoritma pengurutan yang tidak berbasis-pembandingan di beberapa slide berikut ini:

  1. Counting Sort,
  2. Radix Sort.

Algoritma-algoritma pengurutan ini bisa lebih cepat dari batas bawah algoritma pengurutan berbasis-pembandingan yaitu Ω(N log N) karena mereka tidak membandingkan elemen-elemen dari larik.

🕑

It is known (also not proven in this visualization as it will take about half-an-hour lecture about decision tree model to do so) that all comparison-based sorting algorithms have a lower bound time complexity of Ω(N log N).


Thus, any comparison-based sorting algorithm with worst-case complexity O(N log N), like Merge Sort is considered an optimal algorithm, i.e., we cannot do better than that.


However, we can achieve faster sorting algorithm — i.e., in O(N) — if certain assumptions of the input array exist and thus we can avoid comparing the items to determine the sorted order.

🕑

Assumption: If the items to be sorted are Integers with small range, we can count the frequency of occurrence of each Integer (in that small range) and then loop through that small range to output the items in sorted order.


Try Counting Sort on the example array above where all Integers are within [1..9], thus we just need to count how many times Integer 1 appears, Integer 2 appears, ..., Integer 9 appears, and then loop through 1 to 9 to print out x copies of Integer y if frequency[y] = x.


The time complexity is O(N) to count the frequencies and O(N+k) to print out the output in sorted order where k is the range of the input Integers, which is 9-1+1 = 9 in this example. The time complexity of Counting Sort is thus O(N+k), which is O(N) if k is small.


We will not be able to do the counting part of Counting Sort when k is relatively big due to memory limitation, as we need to store frequencies of those k integers.


PS: This version of Counting Sort is not stable, as it does not actually remember the (input) ordering of duplicate integers. The version presented in CLRS is stable, but is a bit more complex than this form.

🕑

Assumption: If the items to be sorted are Integers with large range but of few digits, we can combine Counting Sort idea with Radix Sort to achieve the linear time complexity.


In Radix Sort, we treat each item to be sorted as a string of w digits (we pad Integers that have less than w digits with leading zeroes if necessary).


For the least significant (rightmost) digit to the most significant digit (leftmost), we pass through the N items and put them according to the active digit into 10 Queues (one for each digit [0..9]), which is like a modified Counting Sort as this one preserves stability (remember, the Counting Sort version shown in this slide earlier is not a stable sort). Then we re-concatenate the groups again for subsequent iteration.


Try Radix Sort on the random 4-digits array above for clearer explanation.


Notice that we only perform O(w × (N+k)) iterations. In this example, w = 4 and k = 10.

🕑

Now, having discussed about Radix Sort, should we use it for every sorting situation?


For example, it should be theoretically faster to sort many (N is very large) 32-bit signed integers as w ≤ 10 digits and k = 10 if we interpret those 32-bit signed integers in Decimal. O(10 × (N+10)) = O(N).


Discussion: Using base-10 as shown in this visualization is actually not the best way to sort N 32-bit signed integers. What should be the better setup?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Ada beberapa properti-properti lain yang bisa digunakan untuk membedakan algoritma-algoritma pengurutan lebih dari sekedar apakah mereka berbasis-pembandingan atau tidak, rekursif atau iteratif.


Dalam bagian ini, kita akan membahas tentang di-tempat dibandingkan dengan tidak di-tempat, stabil dibandingkan dengan tidak stabil, dan performa caching dari berbagai algoritma-algoritma pengurutan.

🕑

A sorting algorithm is said to be an in-place sorting algorithm if it requires only a constant amount (i.e., O(1)) of extra space during the sorting process. That's it, a few, constant number of extra variables is OK but we are not allowed to have variables that has variable length depending on the input size N.


Merge Sort (the classic version), due to its merge sub-routine that requires additional temporary array of size N, is not in-place.


Discussion: How about Bubble Sort, Selection Sort, Insertion Sort, Quick Sort (randomized or not), Counting Sort, and Radix Sort. Which ones are in-place?

🕑

A sorting algorithm is called stable if the relative order of elements with the same key value is preserved by the algorithm after sorting is performed.


Example application of stable sort: Assume that we have student names that have been sorted in alphabetical order. Now, if this list is sorted again by tutorial group number (recall that one tutorial group usually has many students), a stable sort algorithm would ensure that all students in the same tutorial group still appear in alphabetical order of their names. Radix sort that goes through multiple round of sorts digit-by-digit requires a stable sort sub-routine for it to work correctly.


Discussion: Which of the sorting algorithms discussed in this e-Lecture are stable?
Try sorting array A = {3, 4a, 2, 4b, 1}, i.e. there are two copies of 4 (4a first, then 4b).

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Kita mendekati akhir dari Kuliah Maya ini.


Waktunya untuk beberapa pertanyaan-pertanyaan sederhana.

🕑

Quiz: Which of these algorithms run in O(N log N) on any input array of size N?

Quick Sort (Deterministic)
Bubble Sort
Merge Sort
Insertion Sort
🕑

Quiz: Which of these algorithms has worst case time complexity of Θ(N^2) for sorting N integers?

Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Radix Sort


Θ adalah analisa kompleksitas waktu yang sudah ketat (tight) dimana analisa kasus terbaik Ω dan kasus terjelek big-O cocok.

🕑

Kita telah mencapai akhir dari Kuliah Maya pengurutan.


Tetapi, masih ada dua algoritma-algoritma pengurutan lainnya di VisuAlgo yang berada didalam struktur-struktur data yang lain: Heap Sort dan Balanced BST Sort. Kita akan membahas mereka saat anda menjalani Kuliah Maya dari dua struktur-struktur data tersebut.

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

Sebenarnya, kode sumber C++ untuk banyak dari algoritma-algoritma pengurutan ini sudah tersebar di berbagai slide-slide Kuliah Maya. Untuk bahasa pemrograman yang lain, silahkan terjemahkan kode sumber C++ yang diberikan ke bahasa pemrograman lain tersebut.


Biasanya, pengurutan hanyalah bagian kecil dari proses pemecahan masalah dan akhir-akhir ini, banyak bahasa-bahasa pemrograman mempunyai fungsi-fungsi pengurutan mereka sendiri-sendiri sehingga kita tidak perlu untuk mengimplementasikan ulang algoritma-algoritma tersebut kecuali sangat perlu.


Dalam C++, anda bisa menggunakan std::sort (sangat mungkin adalah algoritma pengurutan hibrid: introsort), std::stable_sort (sangat mungkin adalah Merge Sort), atau std::partial_sort (sangat mungkin adalah Timbunan Biner) dalam STL algorithm.
Dalam Python, anda bisa menggunakan sort (sangat mungkin adalah algoritma pengurutan hibrid: Timsort).
Dalam Java, anda bisa menggunakan Collections.sort.

Dalam OCaml, anda bisa menggunakan List.sort compare list_name.


Jika fungsi pembandingan adalah spesifik ke sebuah masalah, kita mungkin harus memberikan fungsi pembandingan tambahan kepada rutin pengurutan yang sudah built-in tersebut.

🕑

Sekarang saatnya bagi anda untuk melihat apakah anda telah mengerti dasar-dasar dari berbagi algoritma-algoritma pengurutan yang dibahas sejauh ini.


Test your understanding here!

🕑

Sekarang setelah anda mencapai akhir dari Kuliah Maya ini, apakah anda pikir masalah pengurutan sesimpel pemanggilan rutin pengurutan yang sudah built-in di berbagai bahasa pemrograman?


Cobalah masalah-masalah di online judge ini untuk menyelidiki lebih lanjut:
Kattis - mjehuric
Kattis - sortofsorting, atau
Kattis - sidewayssorting


Ini bukan akhir dari topik pengurutan. Ketika anda menjelajahi topik-topik lain di VisuAlgo, anda akan menyadari bahwa pengurutan adalah langkah pre-processing untuk banyak algoritma-algoritma tingkat lanjut lainnya untuk menyelesaikan masalah-masalah yang lebih sulit, contohnya sebagai langkah pre-processing untuk algoritma Kruskal, secara kreatif dipakai didalam struktur data Larik Akhiran (Suffix Array), dsb.

🕑

start of 3230 material


You have reached the last slide. Return to 'Exploration Mode' to start exploring!

Note that if you notice any bug in this visualization or if you want to request for a new visualization feature, do not hesitate to drop an email to the project leader: Dr Steven Halim via his email address: stevenhalim at gmail dot com.

🕑

0 1 2 3 4 5 6 7 8 9 A B C D E F

Buat(A)

Urutkan

>
N =

Acak

Terurut

Hampir terurut

Many Duplicates

A =

Lakukan

Set base

Set base

Tentang Tim Syarat Guna Kebijakan Privasi

Tentang

Initially conceived in 2011 by Associate Professor Steven Halim, VisuAlgo aimed to facilitate a deeper understanding of data structures and algorithms for his students by providing a self-paced, interactive learning platform.

Featuring numerous advanced algorithms discussed in Dr. Steven Halim's book, 'Competitive Programming' — co-authored with Dr. Felix Halim and Dr. Suhendry Effendy — VisuAlgo remains the exclusive platform for visualizing and animating several of these complex algorithms even after a decade.

While primarily designed for National University of Singapore (NUS) students enrolled in various data structure and algorithm courses (e.g., CS1010/equivalent, CS2040/equivalent (including IT5003), CS3230, CS3233, and CS4234), VisuAlgo also serves as a valuable resource for inquisitive minds worldwide, promoting online learning.

Initially, VisuAlgo was not designed for small touch screens like smartphones, as intricate algorithm visualizations required substantial pixel space and click-and-drag interactions. For an optimal user experience, a minimum screen resolution of 1366x768 is recommended. However, since April 2022, a mobile (lite) version of VisuAlgo has been made available, making it possible to use a subset of VisuAlgo features on smartphone screens.

VisuAlgo remains a work in progress, with the ongoing development of more complex visualizations. At present, the platform features 24 visualization modules.

Equipped with a built-in question generator and answer verifier, VisuAlgo's "online quiz system" enables students to test their knowledge of basic data structures and algorithms. Questions are randomly generated based on specific rules, and students' answers are automatically graded upon submission to our grading server. As more CS instructors adopt this online quiz system worldwide, it could effectively eliminate manual basic data structure and algorithm questions from standard Computer Science exams in many universities. By assigning a small (but non-zero) weight to passing the online quiz, CS instructors can significantly enhance their students' mastery of these basic concepts, as they have access to an almost unlimited number of practice questions that can be instantly verified before taking the online quiz. Each VisuAlgo visualization module now includes its own online quiz component.

VisuAlgo has been translated into three primary languages: English, Chinese, and Indonesian. Additionally, we have authored public notes about VisuAlgo in various languages, including Indonesian, Korean, Vietnamese, and Thai:

id, kr, vn, th.

Tim

Pemimpin & Penasihat Proyek (Jul 2011-sekarang)
Associate Professor Steven Halim, School of Computing (SoC), National University of Singapore (NUS)
Dr Felix Halim, Senior Software Engineer, Google (Mountain View)

Murid-Murid S1 Peniliti 1
CDTL TEG 1: Jul 2011-Apr 2012: Koh Zi Chun, Victor Loh Bo Huai

Murid-Murid Proyek Tahun Terakhir/UROP 1
Jul 2012-Dec 2013: Phan Thi Quynh Trang, Peter Phandi, Albert Millardo Tjindradinata, Nguyen Hoang Duy
Jun 2013-Apr 2014 Rose Marie Tan Zhao Yun, Ivan Reinaldo

Murid-Murid S1 Peniliti 2
CDTL TEG 2: May 2014-Jul 2014: Jonathan Irvin Gunawan, Nathan Azaria, Ian Leow Tze Wei, Nguyen Viet Dung, Nguyen Khac Tung, Steven Kester Yuwono, Cao Shengze, Mohan Jishnu

Murid-Murid Proyek Tahun Terakhir/UROP 2
Jun 2014-Apr 2015: Erin Teo Yi Ling, Wang Zi
Jun 2016-Dec 2017: Truong Ngoc Khanh, John Kevin Tjahjadi, Gabriella Michelle, Muhammad Rais Fathin Mudzakir
Aug 2021-Apr 2023: Liu Guangyuan, Manas Vegi, Sha Long, Vuong Hoang Long, Ting Xiao, Lim Dewen Aloysius

Murid-Murid S1 Peniliti 3
Optiver: Aug 2023-Oct 2023: Bui Hong Duc, Oleh Naver, Tay Ngan Lin

Murid-Murid Proyek Tahun Terakhir/UROP 3
Aug 2023-Apr 2024: Xiong Jingya, Radian Krisno, Ng Wee Han

List of translators who have contributed ≥ 100 translations can be found at statistics page.

Ucapan Terima Kasih
NUS CDTL gave Teaching Enhancement Grant to kickstart this project.

For Academic Year 2023/24, a generous donation from Optiver will be used to further develop VisuAlgo.

Syarat Guna

VisuAlgo is generously offered at no cost to the global Computer Science community. If you appreciate VisuAlgo, we kindly request that you spread the word about its existence to fellow Computer Science students and instructors. You can share VisuAlgo through social media platforms (e.g., Facebook, YouTube, Instagram, TikTok, Twitter, etc), course webpages, blog reviews, emails, and more.

Data Structures and Algorithms (DSA) students and instructors are welcome to use this website directly for their classes. If you capture screenshots or videos from this site, feel free to use them elsewhere, provided that you cite the URL of this website (https://visualgo.net) and/or the list of publications below as references. However, please refrain from downloading VisuAlgo's client-side files and hosting them on your website, as this constitutes plagiarism. At this time, we do not permit others to fork this project or create VisuAlgo variants. Personal use of an offline copy of the client-side VisuAlgo is acceptable.

Please note that VisuAlgo's online quiz component has a substantial server-side element, and it is not easy to save server-side scripts and databases locally. Currently, the general public can access the online quiz system only through the 'training mode.' The 'test mode' offers a more controlled environment for using randomly generated questions and automatic verification in real examinations at NUS.

List of Publications

This work has been presented at the CLI Workshop at the ICPC World Finals 2012 (Poland, Warsaw) and at the IOI Conference at IOI 2012 (Sirmione-Montichiari, Italy). You can click this link to read our 2012 paper about this system (it was not yet called VisuAlgo back in 2012) and this link for the short update in 2015 (to link VisuAlgo name with the previous project).

Bug Reports or Request for New Features

VisuAlgo is not a finished project. Associate Professor Steven Halim is still actively improving VisuAlgo. If you are using VisuAlgo and spot a bug in any of our visualization page/online quiz tool or if you want to request for new features, please contact Associate Professor Steven Halim. His contact is the concatenation of his name and add gmail dot com.

Kebijakan Privasi

Version 1.2 (Updated Fri, 18 Aug 2023).

Since Fri, 18 Aug 2023, we no longer use Google Analytics. Thus, all cookies that we use now are solely for the operations of this website. The annoying cookie-consent popup is now turned off even for first-time visitors.

Since Fri, 07 Jun 2023, thanks to a generous donation by Optiver, anyone in the world can self-create a VisuAlgo account to store a few customization settings (e.g., layout mode, default language, playback speed, etc).

Additionally, for NUS students, by using a VisuAlgo account (a tuple of NUS official email address, student name as in the class roster, and a password that is encrypted on the server side — no other personal data is stored), you are giving a consent for your course lecturer to keep track of your e-lecture slides reading and online quiz training progresses that is needed to run the course smoothly. Your VisuAlgo account will also be needed for taking NUS official VisuAlgo Online Quizzes and thus passing your account credentials to another person to do the Online Quiz on your behalf constitutes an academic offense. Your user account will be purged after the conclusion of the course unless you choose to keep your account (OPT-IN). Access to the full VisuAlgo database (with encrypted passwords) is limited to Prof Halim himself.

For other CS lecturers worldwide who have written to Steven, a VisuAlgo account (your (non-NUS) email address, you can use any display name, and encrypted password) is needed to distinguish your online credential versus the rest of the world. Your account will have CS lecturer specific features, namely the ability to see the hidden slides that contain (interesting) answers to the questions presented in the preceding slides before the hidden slides. You can also access Hard setting of the VisuAlgo Online Quizzes. You can freely use the material to enhance your data structures and algorithm classes. Note that there can be other CS lecturer specific features in the future.

For anyone with VisuAlgo account, you can remove your own account by yourself should you wish to no longer be associated with VisuAlgo tool.