Skip to content

Commit

Permalink
feat: delete duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
fiteen committed Dec 20, 2019
1 parent 0d91f07 commit c11e57d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
12 changes: 4 additions & 8 deletions BubbleSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
#### C语言
```c
void bubble_sort(int arr[], int n) {
int i, j, tmp;
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
swap(arr, j, j+1);
}
}
}
Expand All @@ -40,14 +38,12 @@ void bubble_sort(int arr[], int n) {
```c
void bubble_sort_quicker(int arr[], int n) {
int i, j, tmp, flag;
int i, j, flag;
for (i = 0; i < n - 1; i++) {
flag = 0;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
swap(arr, j, j+1);
flag = 1;
}
}
Expand Down
18 changes: 10 additions & 8 deletions BubbleSort/bubble_sort.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#include <stdio.h>

void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

void bubble_sort(int arr[], int n) {
int i, j, tmp;
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
swap(arr, j, j+1);
}
}
}
}

void bubble_sort_quicker(int arr[], int n) {
int i, j, tmp, flag;
int i, j, flag;
for (i = 0; i < n - 1; i++) {
flag = 0;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
swap(arr, j, j+1);
flag = 1;
}
}
Expand Down
7 changes: 0 additions & 7 deletions QuickSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
这里取序列的第一个元素为基准。

```c
/* 交换位置函数,交换 i 和 j 的位置 */
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

/* 选取序列的第一个元素作为基准 */
int select_pivot(int arr[], int low) {
return arr[low];
Expand Down
1 change: 0 additions & 1 deletion QuickSort/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>
#include <time.h>

/* 交换位置函数,交换 i 和 j 的位置 */
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
Expand Down

0 comments on commit c11e57d

Please sign in to comment.