#include <stdio.h>
int a[10001];
int n, i, j, temp,flag=1;
int main() {
scanf("%d", &n);
for (i=1; i<=n; i++)
scanf("%d", &a[i]);
for(i=1; i<n; i++)
{
flag=1;
for(j=1;j<=n-i;j++)
{
if (a[j] > a[j+1])
{
flag=0;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
if(flag==1)
{
break;
}
}
printf("%d", i-1);
return 0;
}
---------------------------------------------------------------
//실행시간 측정
#include <time.h>
#include <stdio.h>
int main(void)
{
int i;
double start, end;
//for 루프 시작 시간
start = (double)clock() / CLOCKS_PER_SEC;
//for루프 100000000번 돌아보기
int sum = 0;
for (i = 0; i < 100000000; i++) {
sum++;
}
//for 루프 끝난 시간
end = (((double)clock()) / CLOCKS_PER_SEC);
printf("프로그램 수행 시간 :%lf\n", (end-start));
}
______________________________________________________________________
#include <stdio.h>
typedef struct
{
int num, gas;
}device;
int main()
{
int i, j, n;
device a[101];
device temp;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d %d",&a[i].num,&a[i].gas);
}
for(i=1;i<n;i++)
{
for(j=1;j<=n-1;j++)
{
if(a[j].num>a[j+1].num)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=1;i<=n;i++)
{
printf("%d %d\n",a[i].num,a[i].gas);
}
return 0;
}
______________________________________________________________________________________
#include <stdio.h>
typedef struct
{
char name[11];
int score;
}student;
int main()
{
int i,j,m,n;
student a[101];
student temp;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%s %d",a[i].name,&a[i].score);
}
for(i=1;i<n;i++)
{
for(j=1;j<m;j++)
{
if(a[j].score>a[j+1].score)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(j=1;j<=m;j++)
{
printf("%s\n",a[j].name);
}
}
숙제 구조체정렬 - 버블정렬 : 1805 3015 3017



