forked from 54shady/linuxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort : add bubble sort and gcov utils usage
- Loading branch information
Showing
6 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
tags | ||
project.vim | ||
project.info | ||
*.gcov | ||
*.gcda | ||
*.gcno |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bubbleSort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |