• 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;
    }



  • 相关阅读:
    解析空白符(空白,制表)分隔的字串
    关于select—页面中的ListBox的Javascript
    【函数】strcat源代码
    【SQL语法】系列08:利用Update更新表中数据
    【函数】fill和fill_n填充之区别
    【Xcode】编辑与调试
    【网站】在WAMP下安装wordpress
    【SQL语法】系列06:利用ORDER BY排序
    【SQL语法】系列07:利用insert into插入新行
    【Boost】系列03:内存管理之shared_ptr智能指针
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258161.html
Copyright © 2020-2023  润新知