C程序实现冒泡排序

Bubble Sort is a simple, stable, and in-place sorting algorithm.

气泡排序是一种简单,稳定且就地的排序算法。

  1. A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is present in the input unsorted array.

    一种稳定的排序算法是一种具有相等值的键在排序后的输出数组中以与输入未排序数组中存在的键相同的顺序出现的算法

  2. An in-place sorting algorithm has various definitions but a more used one is – An in-place sorting algorithm does not need extra space and uses the constant memory for manipulation of the input in-place. Although, it may require some extra constant space allowed for variables.

    就地排序算法具有各种定义,但使用更广泛的定义是:–就地排序算法不需要额外的空间,并且使用常量内存来对就地输入进行操作。 虽然,它可能需要一些允许变量使用的额外常量空间。

Due to its simplicity, it is widely used as a sorting algorithm by computer programmers.

由于其简单性,它被计算机程序员广泛用作排序算法。

The basic working principle of bubble sort is that it repeatedly swaps the adjacent elements if they are in the wrong order. Hence, after every full iteration, the largest element reaches its position as in the sorted array.

冒泡排序的基本工作原理是,如果相邻元素的顺序错误,它将反复交换。 因此,在每次完整的迭代之后,最大元素将到达其在排序数组中的位置。

Pseudo-code:

伪代码:

1.	for i: 0 to n-1 not inclusive do:
2.	     for j: 0 to n-i-1 not inclusive do:
3.	          If a[j] > a[j+1] then
4.	                   swap a[j] and a[j+1]
5.	          end if
6.	     end for
7.	end for

Example:

例:

Input Array:

输入数组:

5 8 1 2 9

5 8 1 2 9

Here, I will run from 0 to 3

在这里,我将从0运行到3

Since, i < n-1 => i < 5-1 => i < 4

因为,我<n-1 =>我<5-1 =>我<4

Iteration 1 (i = 0):

迭代1(i = 0):

For j = 0, (5 8 1 2 9) -> (5 8 1 2 9) No swap because 5 < 8

对于j = 0,( 5 8 1 2 9)->( 5 8 1 2 9)无交换,因为5 <8

For j = 1, (5 8 1 2 9) -> (5 1 8 2 9), swap because 1 < 8

对于j = 1,(5 8 1 2 9)->(5 1 8 2 9),交换是因为1 <8

For j = 2, (5 1 8 2 9) -> (5 1 2 8 9), swap because 2 < 8

对于j = 2,(5 1 8 2 9)->(5 1 2 8 9),交换因为2 <8

For j = 3, (5 1 2 8 9) -> (5 1 2 8 9), no swap

对于j = 3,(5 1 2 8 9 )->(5 1 2 8 9 ),没有交换

1st Pass gives – 5 1 2 8 9

1 通给出- 5 1 2 8 9

Iteration 2 (i = 1):

迭代2(i = 1):

For j = 0, (5 1 2 8 9) -> (1 5 2 8 9) No swap because 1 < 5

对于j = 0,( 5 1 2 8 9 )->( 1 5 2 8 9 )由于1 <5而没有交换

For j = 1, (1 5 2 8 9) -> (1 2 5 8 9), swap because 2 < 5

对于j = 1,(1 5 2 8 9 )->(1 2 5 8 9 ),因为2 <5

For j = 2, (1 2 5 8 9) -> (1 2 5 8 9), no swap

对于j = 2,(1 2 5 8 9 )->(1 2 5 8 9 ),没有交换

2nd Pass gives – 1 2 5 8 9

第二遍给– 1 2 5 8 9

Iteration 3 (i = 2):

迭代3(i = 2):

For j = 0, (1 2 5 8 9) -> (1 2 5 8 9), No swap because 1 < 2

对于j = 0,( 1 2 5 8 9 )->( 1 2 5 8 9 ),由于1 <2而没有交换

For j = 1, (1 2 5 8 9) -> (1 2 5 8 9), No swap 2 < 5

对于j = 1,(1 2 5 8 9 )->(1 2 5 8 9 ),无交换2 <5

3rd Pass gives – 1 2 5 8 9

第三张通过– 1 2 5 8 9

Iteration 4 (i = 3):

迭代4(i = 3):

For j = 0, (1 2 5 8 9) -> (1 2 5 8 9), No swap because 1 < 2

对于j = 0,( 1 2 5 8 9 )->( 1 2 5 8 9 ),由于1 <2而没有交换

4th Pass gives – 1 2 5 8 9 because, last element is automatically sorted.

4 通过给出– 1 2 5 8 9,因为最后一个元素会自动排序。

Time Complexity: The time complexity of Binary Search can be described as: T(n) = T(n/2) + C

时间复杂度:二进制搜索的时间复杂度可描述为:T(n)= T(n / 2)+ C

  1. Worst case: O(n^2)

    最坏的情况:O(n ^ 2)

  2. Average Case: O(n^2)

    平均情况:O(n ^ 2)

  3. Best case: O(n^2), since the loops run even if the array is sorted

    最佳情况:O(n ^ 2),因为即使数组已排序循环也会运行

  4. Space Complexity: O(1)

    空间复杂度:O(1)

This simple implementation of Bubble Sort is just to explain the concept of the algorithm. In real life, whenever bubble sort is used, the optimized version is preferred over this one. That is because the optimized version gives O(n) time complexity in the best case.

Bubble Sort的这种简单实现只是为了说明算法的概念。 在现实生活中,无论何时使用气泡排序,都比该版本更优选优化版本。 这是因为在最佳情况下,优化版本会给O(n)时间带来复杂性。

In the optimized bubble sort, the inner loop doesn't run if the array is already sorted. Whereas, in the simple implementation, no such provision is there.

在优化的冒泡排序中,如果已对数组进行排序,则内部循环不会运行。 然而,在简单的实现中,没有这样的规定。

Bubble Sort Implementation:

气泡排序实施:

#include <stdio.h>

void swap(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}

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

int main()
{
    int arr[] = { 12, 46, 34, 82, 10, 9, 28 };
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("\nInput Array: \n");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    bubble_sort(arr, n);
    printf("\nSorted Array: \n");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Output:

输出:


Input Array:
12 46 34 82 10 9 28
Sorted Array:
9 10 12 28 34 46 82


翻译自: https://www.includehelp.com/c-programs/implement-bubble-sort.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值