배열 {10, 53, 35, 100, 87} 버블정렬하여 나타내는 알고리즘
#include <stdio.h>
void bubble(int a[])
{
int temp;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4 - i; j++)
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
main()
{
int a[] = {10, 53, 35, 100, 87}
bubble(a);
for(int i = 0; i < 5; i++)
{
printf("d%", a[i]);
}
}
10 35 53 87 100
배열 {10, 53, 35, 100, 87}을 선택 정렬하는 알고리즘
public class Test
{
public static void main(String[] args)
{
int a[] = {10, 53, 35, 100, 87}
int i = 0;
int temp = 0;
do
{
int j = i;
do
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
j++;
}
while(j < 5);
i++;
}
while(i < 4);
for (int b = 0; b < 5; b++)
{
System.out.printf(a[b] + "\t");
}
}
}
10 35 53 87 100