mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Update bubble sort (C) and insertion sort (C).
This commit is contained in:
		@ -7,8 +7,7 @@
 | 
				
			|||||||
#include "../include/include.h"
 | 
					#include "../include/include.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 冒泡排序 */
 | 
					/* 冒泡排序 */
 | 
				
			||||||
void bubble_sort(int nums[], int size)
 | 
					void bubble_sort(int nums[], int size) {
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
					    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
				
			||||||
    for (int i = 0; i < size - 1; i++)
 | 
					    for (int i = 0; i < size - 1; i++)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -26,8 +25,7 @@ void bubble_sort(int nums[], int size)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 冒泡排序(标志优化)*/
 | 
					/* 冒泡排序(标志优化)*/
 | 
				
			||||||
void bubble_sort_with_flag(int nums[], int size)
 | 
					void bubble_sort_with_flag(int nums[], int size) {
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
					    // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
				
			||||||
    for (int i = 0; i < size - 1; i++)
 | 
					    for (int i = 0; i < size - 1; i++)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -50,8 +48,7 @@ void bubble_sort_with_flag(int nums[], int size)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Driver Code */
 | 
					/* Driver Code */
 | 
				
			||||||
int main()
 | 
					int main() {
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    int nums[6] = {4, 1, 3, 1, 5, 2};
 | 
					    int nums[6] = {4, 1, 3, 1, 5, 2};
 | 
				
			||||||
    printf("冒泡排序后:\n");
 | 
					    printf("冒泡排序后:\n");
 | 
				
			||||||
    bubble_sort(nums, 6);
 | 
					    bubble_sort(nums, 6);
 | 
				
			||||||
 | 
				
			|||||||
@ -7,8 +7,7 @@
 | 
				
			|||||||
#include "../include/include.h"
 | 
					#include "../include/include.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 插入排序 */
 | 
					/* 插入排序 */
 | 
				
			||||||
void insertionSort(int nums[], int size)
 | 
					void insertionSort(int nums[], int size) {
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    // 外循环:base = nums[1], nums[2], ..., nums[n-1]
 | 
					    // 外循环:base = nums[1], nums[2], ..., nums[n-1]
 | 
				
			||||||
    for (int i = 1; i < size; i++)
 | 
					    for (int i = 1; i < size; i++)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -26,8 +25,7 @@ void insertionSort(int nums[], int size)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Driver Code */
 | 
					/* Driver Code */
 | 
				
			||||||
int main()
 | 
					int main() {
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    int nums[] = {4, 1, 3, 1, 5, 2};
 | 
					    int nums[] = {4, 1, 3, 1, 5, 2};
 | 
				
			||||||
    insertionSort(nums, 6);
 | 
					    insertionSort(nums, 6);
 | 
				
			||||||
    printf("插入排序完成后 nums = \n");
 | 
					    printf("插入排序完成后 nums = \n");
 | 
				
			||||||
 | 
				
			|||||||
@ -170,8 +170,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ```c title="bubble_sort.c"
 | 
					    ```c title="bubble_sort.c"
 | 
				
			||||||
    /* 冒泡排序 */
 | 
					    /* 冒泡排序 */
 | 
				
			||||||
    void bubble_sort(int nums[], int size)
 | 
					    void bubble_sort(int nums[], int size) {
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
					        // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
				
			||||||
        for (int i = 0; i < size - 1; i++)
 | 
					        for (int i = 0; i < size - 1; i++)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@ -368,8 +367,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ```c title="bubble_sort.c"
 | 
					    ```c title="bubble_sort.c"
 | 
				
			||||||
    /* 冒泡排序 */
 | 
					    /* 冒泡排序 */
 | 
				
			||||||
    void bubble_sort(int nums[], int size)
 | 
					    void bubble_sort(int nums[], int size) {
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
					        // 外循环:待排序元素数量为 n-1, n-2, ..., 1
 | 
				
			||||||
        for (int i = 0; i < size - 1; i++)
 | 
					        for (int i = 0; i < size - 1; i++)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
				
			|||||||
@ -136,8 +136,7 @@ comments: true
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ```c title="insertion_sort.c"
 | 
					    ```c title="insertion_sort.c"
 | 
				
			||||||
    /* 插入排序 */
 | 
					    /* 插入排序 */
 | 
				
			||||||
    void insertionSort(int nums[], int size)
 | 
					    void insertionSort(int nums[], int size) {
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        // 外循环:base = nums[1], nums[2], ..., nums[n-1]
 | 
					        // 外循环:base = nums[1], nums[2], ..., nums[n-1]
 | 
				
			||||||
        for (int i = 1; i < size; i++)
 | 
					        for (int i = 1; i < size; i++)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user