zoukankan      html  css  js  c++  java
  • c语言冒泡排序,指针,数组

    冒泡排序算法的运作如下:
    1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
    2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
    3. 针对所有的元素重复以上的步骤,除了最后一个。
    4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

     时间复杂度


      若文件的初始状态是正序的,一趟扫描即可完成排序。所需的关键字比较次数
    C
    和记录移动次数
    M
    均达到最小值:
     C_{min}=n-1
    M_{min}=0
    所以,冒泡排序最好的时间复杂度为 O(n)
      若初始文件是反序的,需要进行
    n-1
    趟排序。每趟排序要进行
    n-i
    次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值:
    C_{max}=frac{n(n-1)}{2}=O(n^{2})
    M_{max}=frac{3n(n-1)}{2}=O(n^{2})
    冒泡排序的最坏时间复杂度为
    O(n^{2})
    综上,因此冒泡排序总的平均时间复杂度为
    Oleft(n^2
ight)


    2. Use pointer to complete the assignment. define array for three integers.

    Write three functions, which are input(), deal(), print()
    The input() function needs to complete three number's input.
    The deal() function needs to put the smallest onto the first position, put the biggest one onto the end of the sequence.

    The print() function needs to print the result.


    #include<stdio.h>
    int input(int* a);
    int output(int* a);
    int deal(int *);
    int main()
    {
    	int array[3];
    	input(array);
    	deal(array);
    	output(array);
        return 0;
    }
    
    int input(int* a)
    {
    	int i;
    	for(i=0;i<3;i++)
    	{
    		scanf("%d",&*(a+i));
    	}
    	return 0;
    
    }
    int deal(int *a)
    {
    	int min,i,j;
    	for(i=0;i<3-1;i++)
    		for(j=0;j<2-i;j++)
    			if(*(a+j)>*(a+j+1))
    			{
    				min=*(a+j);
    			    *(a+j)=*(a+j+1);
    				*(a+j+1)=min;
    			}
    	return 0;
    }
    int output(int* a)
    {
    	int i;
    	for(i=0;i<3;i++)
    		printf("%d  ",*(a+i));
    	return 0;
    }



  • 相关阅读:
    使用hibernate在5秒内插入11万条数据,你觉得可能吗?
    标准模板库 STL 使用之 —— vector 使用 tricks
    主定理(Master Theorem)与时间复杂度
    主定理(Master Theorem)与时间复杂度
    位数(digits)的处理
    位数(digits)的处理
    从大整数乘法的实现到 Karatsuba 快速算法
    从大整数乘法的实现到 Karatsuba 快速算法
    进位和借位问题的研究
    进位和借位问题的研究
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258161.html
Copyright © 2011-2022 走看看