forked from krahets/hello-algo
-
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.
- Loading branch information
Showing
13 changed files
with
128 additions
and
35 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 |
---|---|---|
|
@@ -5,8 +5,10 @@ | |
.vscode/ | ||
|
||
# mkdocs files | ||
overrides/ | ||
site/ | ||
.cache/ | ||
overrides/ | ||
codes/python | ||
codes/cpp | ||
|
||
docs/chapter_* |
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,50 @@ | ||
package chapter_sorting; | ||
|
||
import java.util.*; | ||
|
||
public class bubble_sort { | ||
/* 冒泡排序 */ | ||
static void bubbleSort(int[] nums) { | ||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1 | ||
for (int i = nums.length - 1; i > 0; i--) { | ||
// 内循环:冒泡操作 | ||
for (int j = 0; j < i; j++) { | ||
if (nums[j] > nums[j + 1]) { | ||
// 交换 nums[j] 与 nums[j + 1] | ||
int tmp = nums[j]; | ||
nums[j] = nums[j + 1]; | ||
nums[j + 1] = tmp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* 冒泡排序(标志优化)*/ | ||
static void bubbleSortWithFlag(int[] nums) { | ||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1 | ||
for (int i = nums.length - 1; i > 0; i--) { | ||
boolean flag = false; // 初始化标志位 | ||
// 内循环:冒泡操作 | ||
for (int j = 0; j < i; j++) { | ||
if (nums[j] > nums[j + 1]) { | ||
// 交换 nums[j] 与 nums[j + 1] | ||
int tmp = nums[j]; | ||
nums[j] = nums[j + 1]; | ||
nums[j + 1] = tmp; | ||
flag = true; // 记录交换元素 | ||
} | ||
} | ||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出 | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] nums = { 4, 1, 3, 1, 5, 2 }; | ||
bubbleSort(nums); | ||
System.out.println("排序后数组 nums = " + Arrays.toString(nums)); | ||
|
||
int[] nums1 = { 4, 1, 3, 1, 5, 2 }; | ||
bubbleSortWithFlag(nums1); | ||
System.out.println("排序后数组 nums1 = " + Arrays.toString(nums)); | ||
} | ||
} |
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,25 @@ | ||
package chapter_sorting; | ||
|
||
import java.util.*; | ||
|
||
public class insertion_sort { | ||
/* 插入排序 */ | ||
static void insertionSort(int[] nums) { | ||
// 外循环:base = nums[1], nums[2], ..., nums[n-1] | ||
for (int i = 1; i < nums.length; i++) { | ||
int base = nums[i], j = i - 1; | ||
// 内循环:将 base 插入到左边的正确位置 | ||
while (j >= 0 && nums[j] > base) { | ||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 | ||
j--; | ||
} | ||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置 | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] nums = { 4, 1, 3, 1, 5, 2 }; | ||
insertionSort(nums); | ||
System.out.println("排序后数组 nums = " + Arrays.toString(nums)); | ||
} | ||
} |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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