8.8 冒泡排序 选择排序 二分查找 递归使用

冒泡排序:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 100000
  5. #define M 100000
  6. void show_arr(int * a,int n)
  7. {
  8. int i;
  9. for(i = 0; i < n; i++)
  10. {
  11. printf("%d ",a[i]);
  12. }
  13. printf("\n");
  14. }
  15. void init_arr(int * a, int n)
  16. {
  17. int i;
  18. srand(time(NULL));
  19. for(i = 0; i < n ; ++i)
  20. a[i] = rand() % M;
  21. }
  22. void sort_maopao(int * a , int n)
  23. {
  24. int i,j,temp;
  25. for(i = 0; i < n - 1 ; i++)
  26. {
  27. for( j = 0 ; j < n - i - 1 ; j++)
  28. {
  29. if(a[j] > a[j+1])
  30. {
  31. temp = a[j];
  32. a[j] = a[j+1];
  33. a[j+1] = temp;
  34. }
  35. }
  36. }
  37. return;
  38. }
  39. int main()
  40. {
  41. int a[N];
  42. init_arr(a , N);
  43. // show_arr(a, N);
  44. sort_maopao(a, N);
  45. // show_arr(a, N);
  46. return 0;
  47. }

选择排序:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 100000
  5. #define M 100000
  6. void show_arr(int * a,int n)
  7. {
  8. int i;
  9. for(i = 0; i < n; i++)
  10. {
  11. printf("%d ",a[i]);
  12. }
  13. printf("\n");
  14. }
  15. void init_arr(int * a, int n)
  16. {
  17. int i;
  18. srand(time(NULL));
  19. for(i = 0; i < n ; ++i)
  20. a[i] = rand() % M;
  21. }
  22. void sort_xuanze(int * a , int n)
  23. {
  24. int i,j,min,temp;
  25. for(i = 0; i < n - 1 ; i++)
  26. {
  27. min = i;
  28. for( j = i+1 ; j < n ; j++)
  29. if(a[min] > a[j])
  30. min = j;
  31. temp = a[min];
  32. a[min] = a[i];
  33. a[i] = temp;
  34. }
  35. return;
  36. }
  37. int main()
  38. {
  39. int a[N];
  40. init_arr(a , N);
  41. // show_arr(a, N);
  42. sort_xuanze(a, N);
  43. // show_arr(a, N);
  44. return 0;
  45. }

二分查找:查找一个元素在某个数组里(必须是有序的数组)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 10
  5. #define M 100
  6. void show_arr(int * a,int n)
  7. {
  8. int i;
  9. for(i = 0; i < n; i++)
  10. {
  11. printf("%d ",a[i]);
  12. }
  13. printf("\n");
  14. }
  15. void init_arr(int * a, int n)
  16. {
  17. int i;
  18. srand(time(NULL));
  19. for(i = 0; i < n ; ++i)
  20. a[i] = rand() % M;
  21. }
  22. void sort_xuanze(int * a , int n)
  23. {
  24. int i,j,min,temp;
  25. for(i = 0; i < n - 1 ; i++)
  26. {
  27. min = i;
  28. for( j = i+1 ; j < n ; j++)
  29. if(a[min] > a[j])
  30. min = j;
  31. temp = a[min];
  32. a[min] = a[i];
  33. a[i] = temp;
  34. }
  35. return;
  36. }
  37. int erfen_find(int * a, int n , int key)
  38. {
  39. int mid , l = 0 , r = n - 1; //mid是中间的坐标,l是左坐标,r是右坐标
  40. while( l <= r)
  41. {
  42. mid = ( l + r )/2;
  43. if( a[mid] < key)
  44. l = mid + 1;
  45. else if ( a[mid] > key)
  46. r = mid - 1;
  47. else
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. int main()
  53. {
  54. int a[N];
  55. init_arr(a , N);
  56. //show_arr(a, N);
  57. sort_xuanze(a, N);
  58. show_arr(a, N);
  59. int key;
  60. scanf("%d",&key);
  61. if(erfen_find(a , N ,key)) //10 需要查找的数
  62. printf("find!\n");
  63. else
  64. printf("not find!\n");
  65. return 0;
  66. }

递归:(阶乘的实现)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int factorial(int n)
  4. {
  5. if( n == 0)
  6. return 1;
  7. n *= factorial(n - 1);
  8. }
  9. int main()
  10. {
  11. int num = factorial(6);
  12. printf("%d\n",num);
  13. return 0;
  14. }

递归打印三角形:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void fun(int n)
  4. {
  5. if (n == 0) return;
  6. fun(n - 1);
  7. int i;
  8. for (i = 0; i < n; ++i)
  9. printf("* ");
  10. putchar(‘\n‘);
  11. }
  12. int main()
  13. {
  14. fun(5);
  15. return 0;
  16. }

来自为知笔记(Wiz)

时间: 2024-08-02 11:03:57

8.8 冒泡排序 选择排序 二分查找 递归使用的相关文章

【算法拾遗】二分查找递归非递归实现

转载请注明出处:http://blog.csdn.net/ns_code/article/details/33747953 本篇博文没太多要说的,二分查找很简单,也是常见常考的查找算法,以下是递归非递归的实现. 非递归实现: /* 非递归实现,返回对应的序号 */ int BinarySearch(int *arr,int len,int key) { if(arr==NULL || len<1) return -1; int low = 0; int high = len-1; while(l

C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序

以下列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序,然后是測试的样例.代码位置:http://download.csdn.net/detail/luozuolincool/8040027 排序类: public class Sortings { //插入排序 public void insertSort(int[] array) { int temp = 0; int index = 0; for (int i = 0; i <

C# 插入排序 冒泡排序 选择排序 快速排序 堆排序 归并排序 基数排序 希尔排序

下面列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 快速排序 堆排序 归并排序 基数排序 希尔排序,然后是测试的例子.代码位置:http://download.csdn.net/detail/luozuolincool/8040027 排序类: public class Sortings { //插入排序 public void insertSort(int[] array) { int temp = 0; int index = 0; for (int i = 0; i <

经典排序:冒泡排序+选择排序 小结

经典排序:冒泡排序+选择排序 例 FJUTOJ 1842 冒泡排序 原理是取相邻两个数进行大小比较,判断是否交换. 以从小到大排序为例,冒泡排序就像气泡一样,最小的数慢慢浮上来,最大的数慢慢沉下去.那么完整从头到尾做一次之后最后一位就是原序列中最大的数字了.然后只需要对1~(n-1)个数字进行排序,完成后倒数第二个数字也为原序列的1~n-1元素中最大的值.如此重复,进行n-1次一定能完成排序.参考代码: 1 #include <stdio.h> 2 void BubbleSort(int *,

java 折半查找 冒泡排序 选择排序

更多查找可以参考 http://www.cnblogs.com/liuling/p/2013-7-24-01.html 这是别人的资源,感觉写的很全.可以仔细研究一下. /* 使用折半查找的前提是数据是有序(升序)的. */ class HalfSearchDemo { public static void main(String[] args) { int[] arr={1,2,3,6,2,3,5,1,8,9,3,5,2}; sop("关键字的位置:"+halfSearch_2(ar

基本排序算法(冒泡排序 选择排序 插入排序 快速排序 归并排序 基数排序 希尔排序)

冒泡排序 public static void bubbleSort(int[] arr){ int lgn = arr.length; for (int i = 0; i < lgn - 1; i++) { for (int j = 0; j < lgn - 1 - i; j++) { if(arr[j] > arr[j + 1]){ int temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } } 选择排序 publ

冒泡排序法与二分查找法

冒泡排序(Bubble Sort) 是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名. 算法原理 冒泡排序算法的运作如下:(从后往前) 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该

冒泡排序-选择排序

/// <summary> /// 选择排序--递归写法 /// </summary> /// <param name="data">数组</param> /// <param name="start">开始下标</param> /// <param name="end">结束下标</param> public void selectSort(int[

二分查找递归版以及书写递归的注意事项

采用递归的方法实现二分查找. 在一个有序数组list中,从下标1开始,查看target是否在数组中.在的话就返回下标,否则返回0. 思路 1.对线性表排序,通常是由小到大排序. 2.取中间值与目标值比较. 2.1取中间值与目标值比较,若相等,则返回下标,退出函数. 2.2中间值比目标值要大,那么在最小值和中间值之间继续查找,执行2. 2.3中间值比目标值要小,那么在最大值和中间值之间继续查,执行2. 第一版(错误) int recursive_bin_ser(int* list, int len