Kliknite Step i otvorite Memorija ispod promenljivih: liste i nizovi su ćelije (brojevi i kao stubići), ćelije koje je promenio poslednji korak su zelene, a promenljive koje su indeksi (i, j, min) stoje ispod svoje ćelije. U Python-u strelica pokazuje na svaki objekat: numbers i same pokazuju na istu listu, a čvorovi povezane liste jedan na drugi.
Python (u pregledaču):
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def bubble(a):
n = len(a)
for i in range(n - 1):
for j in range(n - 1 - i):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
numbers = [5, 2, 8, 1, 9, 3]
same = numbers
bubble(numbers)
print(same)
head = Node(1, Node(2, Node(3)))
ages = {"Ana": 16, "Marko": 17}
print(head.next.value, ages["Ana"])
C (na serveru), sortiranje izborom:
#include <stdio.h>
struct point {
int x, y;
};
int main(void)
{
int a[6] = {5, 2, 8, 1, 9, 3};
int n = 6;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[min]) {
min = j;
}
}
int t = a[i];
a[i] = a[min];
a[min] = t;
}
struct point p = {3, 4};
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d %d %d\n", a[0], p.x + p.y, m[1][2]);
return 0;
}
Pokazivači u C-u: p prolazi kroz niz (njegovo ime se pomera ispod ćelija), s pokazuje na sum, a čvorovi povezane liste na hipu pokazuju jedan na drugi:
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
int main(void)
{
int a[5] = {4, 8, 15, 16, 23};
int sum = 0;
for (int *p = a; p < a + 5; p++) {
sum += *p;
}
int *s = ∑
struct node *head = NULL;
for (int i = 3; i >= 1; i--) {
struct node *n = malloc(sizeof *n);
n->value = i;
n->next = head;
head = n;
}
printf("%d %d\n", *s, head->next->value);
while (head != NULL) {
struct node *next = head->next;
free(head);
head = next;
}
return 0;
}