Skip to content

Commit

Permalink
Sort : add bubble sort and gcov utils usage
Browse files Browse the repository at this point in the history
  • Loading branch information
54shady committed Oct 16, 2018
1 parent c82e82e commit 4946f82
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
tags
project.vim
project.info
*.gcov
*.gcda
*.gcno
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

[GDB 调试](./gdb/README.md)

[排序算法](./sort/README.md)

[形参和实参](./parameters.md)

[Linux Signal](./misc/sig/README.md)
Expand Down
1 change: 1 addition & 0 deletions sort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bubbleSort
12 changes: 12 additions & 0 deletions sort/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CC=gcc
CFLAGS= -Wall -ftest-coverage -fprofile-arcs

PROGS = bubbleSort

all: $(PROGS)

bubbleSort: bubble_sort.o
$(CC) $(CFLAGS) -o $@ $^

clean:
rm -f $(PROGS) *.o *.gcda *.gcno *.gcov
24 changes: 24 additions & 0 deletions sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 排序算法

参考:GNU/Linux Application Programming

## 使用GCOV

编译时配置如下选项

test-coverage
profile-arcs

编译后将生成.gcno文件

bubble_sort.gcno

正常运行应用程序后会生成.gcda文件

./bubbleSort
bubble_sort.gcda

执行gcov(可以带-b选项)生成.gcov文件

gcov [-b] bubble_sort.c
bubble_sort.c.gcov
41 changes: 41 additions & 0 deletions sort/bubble_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>

#define ELEMENT_NR 10
void bubbleSort(int list[], int size)
{
int i, j, temp, swap = 1;

while (swap)
{
swap = 0;
for (i = (size-1) ;i >= 0 ;i--)
{
for (j = 1 ; j <= i ;j++ )
{
if (list[j-1] > list[j])
{
temp = list[j-1];
list[j-1] = list[j];
list[j] = temp;
swap = 1;
}
}
}
}
}

int main(int argc, char *argv[])
{
int theList[ELEMENT_NR]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int i;

/* Invoke the bubble sort algorithm */
bubbleSort(theList, ELEMENT_NR);

/* Print out the final list */
for (i = 0 ; i < ELEMENT_NR ; i++)
printf("%d\t", theList[i]);
printf("\n");

return 0;
}

0 comments on commit 4946f82

Please sign in to comment.