码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法

时间:2015-10-08 23:17:44      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:


  1. // sort.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. void print ( int a[] ,int n )
  7. {
  8. for ( int i=0;i<n;i++) cout<<a[i]<<",";
  9. cout<<endl;
  10. }
  11. // ?°?Y??Dò
  12. void bubble_sort( int a[] , int n )
  13. {
  14. for ( int i=0 ; i < n; i++ ) {
  15. for ( int j=i+1; j< n; j++ ) {
  16. if ( a[i] > a[j] ) {
  17. int tmp = a[i];
  18. a[i] = a[j] ;
  19. a[j] = tmp;
  20. }
  21. }
  22. }
  23. print ( a, 10 );
  24. }
  25. // 2?è???Dò
  26. void insert_sort ( int a[] , int n )
  27. {
  28. int b[10] ={0,0,0,0,0,0,0,0,0};
  29. for ( int i = 0; i < n ; i++ ) {
  30. if ( a[i] < b[0] ) {
  31. b[0] = a[i];
  32. for ( int j=1 ; j<i-1; j++ ) b[j] = b[j+1];
  33. }
  34. if( a[i] > b[i-1] ) b[i] = a[i] ;
  35. for ( int j = 0 ; j < i-1 ; j++ ) {
  36. if ( a[i] > b[j] && a[i] < b[j+1] ) {
  37. b[j] = a[i];
  38. for ( int n=j+1; n<j ; n++) b[n] = b[n+1];
  39. break;
  40. }
  41. }
  42. }
  43. print ( b, 10 );
  44. }
  45. // ??????Dò
  46. void select_sort ( int a[] , int len )
  47. {
  48. for ( int i = 0 ; i < len ; i++ ) {
  49. for ( int j=i+1; j< len; j++ ) {
  50. if ( a[j] > a[i] ) {
  51. int tmp = a[j] ;
  52. a[j] = a[i];
  53. a[i] = tmp;
  54. }
  55. }
  56. }
  57. print ( a, 10 );
  58. }
  59. // μY1é·?×é??Dò
  60. void recursive_group_sort ( int a[] , int left , int right )
  61. {
  62. if ( left == right ) return;
  63. int mid = (right+left)/2;
  64. for( int i=left; i<= mid; i++) {
  65. for( int j=mid+1; j <= right ; j++ ) {
  66. if( a[i] > a[j] ) {
  67. int tmp = a[j] ;
  68. a[j] = a[i];
  69. a[i] = tmp;
  70. }
  71. }
  72. }
  73. recursive_group_sort ( a, left , mid ) ;
  74. recursive_group_sort ( a, mid+1 , right) ;
  75. }
  76. int main(int argc, char* argv[])
  77. {
  78. int a[10] = {5,2,8,9,3,7,6,0,1,4};
  79. bubble_sort(a,10);
  80. insert_sort(a,10);
  81. select_sort(a,10);
  82. cout<<"recursive group sort "<<endl;
  83. recursive_group_sort( a, 0 , 9 );
  84. print(a,10);
  85. return 0;
  86. }





排序算法

标签:

原文地址:http://www.cnblogs.com/fysola/p/4862562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!