[ create a new paste ] login | about

Link: http://codepad.org/wx5klZaG    [ raw code | output | fork ]

C, pasted on Aug 4:
/*
 * C言語 ソートの練習
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort(char** plist, int size) {
	int i, j;
	char* ptmp;

	for (i = 0; i < size - 1; i++) {
		for (j = i + 1; j < size; j++) {
			// 文字列比較
			if (strcmp(plist[i], plist[j]) > 0) {
				//plist[i] > plist[j]の場合ポインタ入れ替え
				ptmp = plist[i];
				plist[i] = plist[j];
				plist[j] = ptmp;
			}
		}
	}

}

int main(void) {

	char* plist[] = { "aaa", "BBB", "ccc", "DDD", "eee", "Sunday", "Monday",
			"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
	int size = sizeof(plist) / sizeof(plist[0]);
	int i;

	puts("ソート前");
	for (i = 0; i < size; i++) {
		puts(plist[i]);
	}
	sort(plist, size);

	puts("ソート後");
	for (i = 0; i < size; i++) {
		puts(plist[i]);
	}

	return EXIT_SUCCESS;
}


Output:
ソート前
aaa
BBB
ccc
DDD
eee
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
ソート後
BBB
DDD
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
aaa
ccc
eee


Create a new paste based on this one


Comments: