![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C语言
六月的黑猫
这个作者很懒,什么都没留下…
展开
-
统计二进制中1的个数
我们可以把这个二进制数字减去1,和自已做与运算,能做多少次与运算,就说明有多少1,或者是把这个数字与1做与运算,等于1就count+1,然后再右移一位,直到它等于0.#include "stdio.h"int countOnes(int num){ int count = 0; while (num >0){ if ((num & 1) == 1){ coun...原创 2019-11-25 20:23:57 · 192 阅读 · 0 评论 -
归并排序
##http://c.biancheng.net/cpp/html/2742.html//https://www.cnblogs.com/chengxiao/p/6194356.html#include <stdio.h>#include <stdlib.h>#define N 7void merge(int arr[], int low, int mid, ...原创 2019-11-01 10:38:48 · 122 阅读 · 0 评论 -
选择排序
#include"stdio.h"//找到最大值的位置,进行交换int findMaxPos(int arr[],int n){ int max=arr[0]; int i; int pos=0; for(i=0;i<n;i++){ if(max<arr[i]) max=arr[i]; pos=i;} return pos;}void select...原创 2019-11-01 10:40:33 · 161 阅读 · 0 评论 -
快速排序
//参考博客http://developer.51cto.com/art/201403/430986.htm#include <stdio.h>void swap(int a[], int i, int j){ int temp = a[i]; a[i] = a[j]; a[j] = temp;}int partition(int a[],...原创 2019-11-01 10:39:59 · 134 阅读 · 0 评论 -
堆排序
#include <stdio.h>#include <stdlib.h>//建堆,调整堆,堆排序void swap(int arr[],int i,int j){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;}//heapify函数实现的是,把最大的节点调整到父节点上void heap...原创 2019-11-01 10:37:46 · 133 阅读 · 0 评论 -
C语言直接插入排序
思路:首先,我们拿到一个数组,假设在有序基础上对后面的无序进行直接插入排序,下图是插入思路: 以上是数字4插入的过程,但是我们需要看边界情况,当前面数字都大于插入关键字的时候,插入操作进行到下标为0的时候结束插入。 ...原创 2019-06-19 18:34:40 · 237 阅读 · 0 评论 -
C语言冒泡排序
以下给出冒泡排序的原理图和代码:从左往右两个数字两两比较,大的放右边,逐渐给前n个,前n-1个,。。。前1个进行排序。 #include <stdio.h>#include <stdlib.h>//这是两两比较动作,从左到右,让右边到数字最大,每一趟都能保证...原创 2019-06-19 18:44:35 · 247 阅读 · 0 评论