A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_URI::$config is deprecated

Filename: core/URI.php

Line Number: 101

Backtrace:

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Router::$uri is deprecated

Filename: core/Router.php

Line Number: 127

Backtrace:

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$benchmark is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$hooks is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$config is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$log is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$utf8 is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$uri is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$exceptions is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$router is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$output is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$security is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$input is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$lang is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$load is deprecated

Filename: core/Controller.php

Line Number: 78

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$db is deprecated

Filename: core/Loader.php

Line Number: 396

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_DB_mysqli_driver::$failover is deprecated

Filename: database/DB_driver.php

Line Number: 371

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 292

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 166

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 235

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 315

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 356

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 289

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$session is deprecated

Filename: core/Loader.php

Line Number: 1283

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$form_validation is deprecated

Filename: core/Loader.php

Line Number: 1283

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 8
Function: __construct

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Welcome::$m_data is deprecated

Filename: core/Loader.php

Line Number: 358

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 11
Function: model

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$benchmark is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$hooks is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$config is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$log is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$utf8 is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$uri is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$exceptions is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$router is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$output is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$security is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$input is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$lang is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$load is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$db is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$session is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$form_validation is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$m_data is deprecated

Filename: core/Loader.php

Line Number: 931

Backtrace:

File: /home/alfe6346/public_html/application/controllers/Welcome.php
Line: 187
Function: view

File: /home/alfe6346/public_html/index.php
Line: 315
Function: require_once

alf-printing Digital-Printing dan Media Pembelajaran
Cepat, Berkualitas, dan Terjangkau Indonesia

Artikel Blog

Penjelasan Lengkap Kode Python: Demo Algoritma Sorting & Searching (Tkinter)

Dokumentasi tiap deklarasi fungsi, kelas, dan metode—siap diunggah ke website sebagai artikel referensi praktikum.

Python 3.x Tkinter Algoritma & Struktur Data

Ringkasan Program

Aplikasi GUI sederhana untuk mendemonstrasikan sorting (Bubble, Selection, Insertion, Merge) dan searching (Linear, Binary). Pengguna memasukkan daftar angka, memilih algoritma, lalu melihat step-by-step prosesnya di Listbox.

import random
import tkinter as tk
from tkinter import messagebox, ttk

Import modul standar: random (membuat data acak) dan tkinter untuk antarmuka grafis (messagebox & ttk untuk widget bergaya modern).

Deklarasi Fungsi Sorting

bubble_sort(data)

  • Parameter: data (list angka)
  • Nilai balik: (arr, steps) — arr hasil terurut, steps daftar snapshot setiap pertukaran
  • Inti: Menukar pasangan elemen berurutan jika terbalik; berhenti dini jika tidak ada pertukaran.
  • Kompleksitas: Waktu O(n²) terburuk/rata‑rata; terbaik O(n) bila sudah hampir terurut. Ruang O(1) selain steps.
def bubble_sort(data):
    arr = data[:]            # salinan agar input asli tidak berubah
    steps = [arr[:]]         # simpan snapshot awal
    n = len(arr)
    for i in range(n):
        swapped = False
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                steps.append(arr[:])
                swapped = True
        if not swapped:      # optimasi: berhenti jika sudah terurut
            break
    return arr, steps

selection_sort(data)

  • Ide: Di setiap iterasi, pilih elemen terkecil dari sisa array lalu tukar ke posisi i.
  • Kompleksitas: Waktu O(n²) selalu; ruang O(1) selain steps.
def selection_sort(data):
    arr = data[:]
    steps = [arr[:]]
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        if min_idx != i:
            arr[i], arr[min_idx] = arr[min_idx], arr[i]
        steps.append(arr[:])
    return arr, steps

insertion_sort(data)

  • Ide: Sisipkan elemen ke posisi yang benar di bagian kiri yang sudah terurut.
  • Kompleksitas: O(n²) terburuk/rata‑rata; O(n) terbaik (data hampir terurut).
def insertion_sort(data):
    arr = data[:]
    steps = [arr[:]]
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
        steps.append(arr[:])
    return arr, steps

merge_sort(data)

  • Ide: Divide & Conquer — belah array, urutkan tiap bagian, lalu gabungkan.
  • Kompleksitas: Waktu O(n log n); ruang O(n) untuk array bantu.
  • Catatan: Fungsi _sort dibuat di dalam untuk menyimpan status ke steps dan memperbarui array utama.
def merge_sort(data):
    arr = data[:]
    steps = [arr[:]]

    def _sort(chunk, offset):
        if len(chunk) <= 1:
            return chunk
        mid = len(chunk) // 2
        left = _sort(chunk[:mid], offset)
        right = _sort(chunk[mid:], offset + mid)
        merged, i, j = [], 0, 0
        while i < len(left) and j < len(right):
            if left[i] <= right[j]:
                merged.append(left[i]); i += 1
            else:
                merged.append(right[j]); j += 1
        merged.extend(left[i:]); merged.extend(right[j:])
        arr[offset: offset + len(merged)] = merged
        steps.append(arr[:])
        return merged

    _sort(arr[:], 0)
    return arr, steps

Deklarasi Fungsi Searching

linear_search(data, target)

  • Ide: Telusuri elemen satu per satu sampai ketemu.
  • Keluaran: (index, steps) — indeks atau -1 jika tidak ada; steps berisi narasi proses.
  • Kompleksitas: O(n) waktu, O(1) ruang.
def linear_search(data, target):
    steps = []
    for index, value in enumerate(data):
        steps.append(f"Bandingkan indeks {index}: nilai {value}")
        if value == target:
            steps.append(f"Ditemukan pada indeks {index}.")
            return index, steps
    steps.append("Elemen tidak ditemukan.")
    return -1, steps

binary_search(data, target)

  • Prasyarat: Data harus terurut; kode ini mengurutkan salinan terlebih dahulu.
  • Keluaran: (index, sorted_data, steps) — indeks pada data terurut (atau -1), salinan data terurut, dan langkah.
  • Kompleksitas: O(log n) untuk pencarian; ada biaya O(n log n) untuk pengurutan awal.
def binary_search(data, target):
    sorted_data = sorted(data)
    steps = [f"Data terurut: {sorted_data}"]
    left, right = 0, len(sorted_data) - 1
    while left <= right:
        mid = (left + right) // 2
        steps.append(
            f"Periksa indeks {mid} (nilai {sorted_data[mid]}), rentang [{left}, {right}]"
        )
        if sorted_data[mid] == target:
            steps.append(f"Ditemukan pada indeks {mid} pada data terurut.")
            return mid, sorted_data, steps
        if sorted_data[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    steps.append("Elemen tidak ditemukan.")
    return -1, sorted_data, steps

Kelas GUI: SortingApp

Mengelola jendela, state, serta interaksi pengguna.

class SortingApp:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Demo Algoritma Sorting")
        self.root.geometry("640x480")

        # State aplikasi (variabel Tkinter)
        self.numbers_var = tk.StringVar()
        self.algorithm_var = tk.StringVar(value="Bubble Sort")
        self.search_target_var = tk.StringVar()
        self.search_algorithm_var = tk.StringVar(value="Linear Search")
        self.sorted_var = tk.StringVar(value="Hasil: -")
        self.status_var = tk.StringVar(value="Masukkan angka lalu pilih algoritma untuk mengurutkan.")

        # Peta nama->fungsi untuk sorting & searching
        self.algorithms = {
            "Bubble Sort": bubble_sort,
            "Selection Sort (Linear)": selection_sort,
            "Insertion Sort": insertion_sort,
            "Merge Sort": merge_sort,
        }
        self.search_algorithms = {
            "Linear Search": linear_search,
            "Binary Search": binary_search,
        }

        self._build_ui()  # konstruksi layout
  • self.root: jendela utama Tkinter.
  • StringVar: pembungkus nilai teks yang otomatis menyinkronkan UI.
  • self.algorithms & self.search_algorithmsdictionary untuk memilih fungsi berdasarkan nama yang dipilih pengguna.

_build_ui(self)

Membuat struktur antarmuka: frame input, kontrol algoritma, panel pencarian, panel hasil, dan list langkah.

def _build_ui(self):
    main_frame = ttk.Frame(self.root, padding=20)
    # ... (membangun Entry, Button, Combobox, Listbox, Scrollbar)
    # tombol: Acak Data, Bersihkan, Urutkan, Cari
    # combobox: pilih algoritma sorting & searching
    # listbox: menampilkan steps proses

Utilitas & Event Handler

  • _generate_random_data: membuat 10 angka acak 0‑99 lalu mengisi Entry.
  • _clear_input: mengosongkan input, target, langkah, dan reset status.
  • _parse_numbers: memvalidasi input (spasi/koma), mengonversi ke list[int], dan melempar ValueError jika salah.
  • _perform_sort: mengambil algoritma dari Combobox, memanggil fungsi, menampilkan langkah dan hasil.
  • _display_steps: merender daftar snapshot/langkah ke Listbox.
  • _perform_search: menjalankan Linear/Binary Search dan menampilkan narasi langkah.
  • run: menjalankan mainloop() Tkinter.
Cuplikan fungsi utilitas
def _parse_numbers(self):
    raw = self.numbers_var.get()
    tokens = [t for t in raw.replace(","," ").split(" ") if t]
    if not tokens:
        raise ValueError("Mohon isi data terlebih dahulu.")
    try:
        return [int(t) for t in tokens]
    except ValueError as exc:
        raise ValueError("Gunakan angka bulat saja.") from exc

def _perform_sort(self):
    try:
        numbers = self._parse_numbers()
    except ValueError as e:
        messagebox.showerror("Input tidak valid", str(e)); return
    sorter = self.algorithms.get(self.algorithm_var.get())
    sorted_numbers, steps = sorter(numbers)
    self._display_steps(steps)
    self.sorted_var.set("Hasil: " + " ".join(map(str, sorted_numbers)))

def _perform_search(self):
    # validasi input & target, pilih algoritma
    if self.search_algorithm_var.get() == "Binary Search":
        index, sorted_numbers, steps = binary_search(numbers, target)
        # tampilkan steps & hasil
    else:
        index, steps = linear_search(numbers, target)

Kompleksitas Waktu & Ruang (Ringkas)

AlgoritmaTerbaikRata‑rataTerburukRuang
Bubble SortO(n)O(n²)O(n²)O(1) + steps
Selection SortO(n²)O(n²)O(n²)O(1) + steps
Insertion SortO(n)O(n²)O(n²)O(1) + steps
Merge SortO(n log n)O(n log n)O(n log n)O(n) + steps
Linear SearchO(n)O(n)O(1)
Binary Search*O(log n)O(log n)O(log n)O(1)

*Pada implementasi ini ada biaya tambahan O(n log n) untuk mengurutkan salinan data sebelum pencarian.

Cara Menjalankan

  1. Pastikan Python 3.x terpasang.
  2. Simpan kode sebagai app.py.
  3. Jalankan: python app.py

Catatan: Aplikasi menggunakan modul standar, tidak memerlukan instalasi pihak ketiga.

FAQ

Mengapa steps menyimpan salinan array (arr[:])?

Agar setiap snapshot tidak berubah ketika array utama berubah di langkah selanjutnya; ini penting untuk visualisasi riwayat.

Mengapa Binary Search mengurutkan data dulu?

Karena syarat Binary Search adalah data terurut. Implementasi ini menjaga input asli, lalu mengembalikan juga salinan data terurut sebagai referensi.

Blok Program Utama

if __name__ == "__main__":
    app = SortingApp()
    app.run()

Menjamin kode hanya menjalankan GUI ketika file dieksekusi langsung, bukan saat diimpor sebagai modul.


© 2025 Alimin — Artikel ini berlisensi untuk penggunaan edukasi.