알고리즘2 (버블정렬(C언어), 선택정렬(Python), 삽입정렬(Java))
Bubble Sort 1 단계


Bubble Sort 2단계

Bubble Sort 3단계


Bubble Sort 실제 코드 구현

Bubble Sort
#include <stdio.h>
void swap(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Data[5] = { 3, 2, 5, 4, 1 };
int main()
{
int howmany = sizeof(Data) / sizeof(Data[0]);
for (int until = howmany - 1; until >= 0; until--)
for (int now = 0; now < until; now++)
if (Data[now] > Data[now + 1])
swap(&Data[now], &Data[now + 1]);
for (int i = 0; i < 5; i++)
printf("%d", Data[i]);
return 0;
}
Selection Sort
now = 0

0 ~ 5
now ~ howmany -1
howmany = len(Data)
now = 1

1 ~ 5
now ~ howmany - 1
min(Data[now:howmany])
where = Data.index(min(Data[now:howmany]))
Data[now], Data[where] = Data[where], Data[now]
now = 2

언제까지?
now == howmany - 1

Selection Sort
Data = [3, 2, 5, 4, 1, 6]
howmany = len(Data)
def solve(now):
while now < howmany - 1:
where = Data.index(min(Data[now:howmany]))
Data[now], Data[where] = Data[where], Data[now]
now += 1
solve(0)
print(Data)
Insertion Sort 1 단계

Insertion Sort 2 단계

int here = MyTurn - 1;
Insertion Sort 3 단계

int here = myTurn - 1;
if (Data[here] > key) Data[here + 1] = Data[here];
here--;
Data[here + 1] = key;
Insertion 4 단계

while((here >= 0) && (Data[here] > key))
public class InsertionSort {
public static void main(String[] args) {
int[] Data = {3, 2, 5, 4, 1};
for (int myTurn = 1; myTurn < Data.length; myTurn++) {
int key = Data[myTurn];
int here = myTurn - 1;
while ((here >= 0) && (Data[here] > key)) {
Data[here + 1] = Data[here];
here--;
}
Data[here + 1] = key;
}
for(int i = 0; i < Data.length; i++)
System.out.println(Data[i]);
}
}