#include <stdio.h>
#include <stdlib.h>
#define N (sizeof scores / sizeof scores[0])
void swap(char** a, char** b)
{
char* t = *a;
*a = *b;
*b = t;
}
void bubble_sort(char* string_list[], int count)
{
int i, j;
for(i = count - 1; i > 0; --i)
for(j = 0; j < i; ++j)
if (string_list[j][0] > string_list[j + 1][0])
swap(&string_list[j], &string_list[j + 1]);
}
int main()
{
char* scores[] =
{
"Charlie 90",
"David 95",
"Bob 85",
"Alice 70",
"Eve 100", // 일부러 콤마 남겨두는 것임. 배열 늘릴 때 복붙하기 좋도록
};
int i, count;
char name[80];
bubble_sort(scores, N);
for(i = 0; i < N; ++i)
{
for(count = 0; scores[i][count] != ' '; ++count)
name[count] = scores[i][count];
name[count] = 0;
printf("%s %s\\n", scores[i] + count + 1, name);
}
system("pause");
return 0;
}
char* [] 형태의 변수라 순서를 바꿀수 있는데 따로 인덱스아닌 인덱스를 만들어 정렬하는게 웃겨서.
전혀 재사용성 없어보이는 bubble_sort 도 웃기고.
string_list[j][0] > string_list[j + 1][0] 로 첫 스펠링만 비교하는 것 보단 strcmp(string_list[j], string_list[j + 1]) > 0 이렇게 해서 문자열 전체를 비교하는게 일반적이지만, 그럴거면 버블소트의 O(n^2)이 너무 비싸지지.