Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ビルド成果物
*.o
*.bin
obj/

# バックアップ・一時ファイル(オプション)
*~
Expand Down
62 changes: 38 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
# コンパイラとオプション
CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c99 -Iinclude
# OS 判定
ifeq ($(OS),Windows_NT)
WIN_EXEC_CHARSET = -fexec-charset=CP932
else
WIN_EXEC_CHARSET =
endif

# ソースファイルとオブジェクトファイル
SRC_DIR = src
SORT_DIR = $(SRC_DIR)/sort
# CFLAGS 例
CFLAGS = -Wall -Wextra -Werror -std=c99 -Iinclude $(WIN_EXEC_CHARSET) CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c99 -Iinclude -fexec-charset=CP932
OBJ_DIR = obj

SRCS = $(SRC_DIR)/main.c \
$(SRC_DIR)/sort_util.c \
$(SORT_DIR)/bogo.c \
$(SORT_DIR)/bubble.c \
$(SORT_DIR)/stalin.c \
# $(SORT_DIR)/bubble.c \
# $(SORT_DIR)/quick.c
TARGET = sort.bin

OBJS = $(SRCS:.c=.o)
SRCS = src/main.c src/sort_util.c src/sort/bogo.c src/sort/bubble.c src/sort/stalin.c src/sort/quick.c
OBJS = $(SRCS:src/%.c=obj/src/%.o)

# 実行ファイル名
TARGET = sort.bin

# ルール定義
.PHONY: all clean


all: $(TARGET)

$(TARGET): $(OBJS)
$(CC) -o $@ $^

# 依存するヘッダファイルの記述(オプション)
$(SRC_DIR)/main.o: include/ACN_types.h include/sort_util.h
$(SRC_DIR)/sort_util.o: include/ACN_types.h include/sort_util.h
$(SORT_DIR)/bogo.o: include/ACN_types.h include/sort_util.h
obj/src/main.o: src/main.c
@mkdir -p obj/src
$(CC) $(CFLAGS) -c $< -o $@

obj/src/sort_util.o: src/sort_util.c
@mkdir -p obj/src
$(CC) $(CFLAGS) -c $< -o $@

obj/src/sort/bogo.o: src/sort/bogo.c
@mkdir -p obj/src/sort
$(CC) $(CFLAGS) -c $< -o $@

obj/src/sort/bubble.o: src/sort/bubble.c
@mkdir -p obj/src/sort
$(CC) $(CFLAGS) -c $< -o $@

obj/src/sort/stalin.o: src/sort/stalin.c
@mkdir -p obj/src/sort
$(CC) $(CFLAGS) -c $< -o $@

obj/src/sort/quick.o: src/sort/quick.c
@mkdir -p obj/src/sort
$(CC) $(CFLAGS) -c $< -o $@

# クリーン
clean:
rm -f $(OBJS) $(TARGET)
rm -rf $(OBJ_DIR) $(TARGET)
46 changes: 27 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@
## ファイル構成

```
$ tree .
.
├── Makefile
├── README.md
├── include
│   ├── ACN_types.h
│   └── sort_util.h
├── sort.bin
└── src
├── main.c
├── main.o
├── sort
│   ├── bogo.c
│   ├── bogo.o
│   ├── bubble.c
│   ├── bubble.o
│   └── quick.c
├── sort_util.c
└── sort_util.o

4 directories, 14 files
|-- Makefile
|-- README.md
|-- include
| |-- ACN_types.h
| `-- sort_util.h
|-- obj
| `-- src
| |-- main.o
| |-- sort
| | |-- bogo.o
| | |-- bubble.o
| | |-- quick.o
| | `-- stalin.o
| `-- sort_util.o
|-- sort.bin
`-- src
|-- main.c
|-- sort
| |-- bogo.c
| |-- bubble.c
| |-- quick.c
| `-- stalin.c
`-- sort_util.c

6 directories, 17 files

```

# ビルド方法
Expand Down
16 changes: 16 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ VD bubblesort(S4 *array, U4 size);
/* stalinsort関数のプロトタイプ宣言 */
VD stalinsort(S4 *array, U4 size);

/* stalinsort関数のプロトタイプ宣言 */
VD qsort_main(S4 *array, U4 size);

/*
* 概要 : メイン関数
* 引数 : なし
Expand All @@ -28,11 +31,16 @@ int main(VD)
S4 array_bubble[ARRAY_SIZE] = {0};
S4 array_bogo[ARRAY_SIZE] = {0};
S4 array_stalin[ARRAY_SIZE] = {0};
S4 array_quick[ARRAY_SIZE] = {0};
U4 size = ARRAY_SIZE;

/* 乱数の初期化 */
srand((U4)time(NULL));

if (size < 5U) {
printf("ARRAY_SIZEは5以上を指定してください");
return 1;
}
/* 配列をランダムに初期化(例:1〜99) */
for (U4 i = 0; i < size; i++) {
array[i] = (S4)(rand() % 99 + 1); // 1〜99のランダム整数
Expand All @@ -41,6 +49,8 @@ int main(VD)
memcpy(array_bogo, array, sizeof(S4) * ARRAY_SIZE);
memcpy(array_bubble, array, sizeof(S4) * ARRAY_SIZE);
memcpy(array_stalin, array, sizeof(S4) * ARRAY_SIZE);
memcpy(array_quick, array, sizeof(S4) * ARRAY_SIZE);

printf("bogo\n");
printf("整列前: ");
print_array(array, size);
Expand All @@ -62,5 +72,11 @@ int main(VD)
printf("整列後: ");
print_array(array_stalin, size);

printf("quicksort\n");
printf("整列前: ");
print_array(array, size);
qsort_main(array_quick, size);
printf("整列後: ");
print_array(array_quick, size);
return 0;
}
42 changes: 42 additions & 0 deletions src/sort/quick.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include "ACN_types.h"
#include "sort_util.h"


S2 partition(S4 *array, S2 left, S2 right, U4 *count) {
S2 j;
S2 i = left - 1;
for(j = left; j < right; j++) {
if(array[j] < array[right]) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i+1], &array[right]);
(*count)++;
return i + 1;
}

VD quicksort(S4 *array, S2 left, S2 right, U4 *count) {
S2 pi = 0;
if(left < right) {
pi = partition(array, left, right, count);

quicksort(array, left, pi - 1, count);
quicksort(array, pi + 1, right, count);
}
}

VD qsort_main(S4 *array, U4 size)
{
// printf("平均回数: 1/4 * n(n - 1)\n");
// printf("理論値: %ld\n", (size) / 2);
// printf("最悪回数: 1/2 * n(n - 1)\n");
U4 count = 0U;

quicksort(array, 0, size-1, &count);

printf("整列までに %lu 回シャッフルしました\n", count);
}