Counting sort, insertion sort

Please refer algorithm description Introduction to Algorithms

. 1  #ifndef COUNTSORT_H
 2  #define COUNTSORT_H
 . 3  
. 4 #include <the iostream>
 . 5  
. 6  the using  namespace STD;
 . 7  
. 8  / * ************************************************************ ***********************
 . 9  Function: // count_sort
 10  the Description: // counting sequencing
 . 11  the input: // array_a: the input array
 12 is                  // size: number of array elements
 13 is                  // max: the maximum element of the array
 14  the Return: None //
 15  ****************************** ****************** * / 
16  void count_sort (int * array_a, int size, int max)
 . 17  {
 18 is      int * array_b = new new  int [size];
 . 19      int * array_c = new new  int [max];
 20 is      int I; 
 21 is      int TEMP;
 22 is  
23 is  
24      for (I = 0 ; I <max; ++ I)
 25      {
 26 is          array_c [I] = 0 ;
 27      }
 28      // number of occurrence counts element 
29      for (I = 0; i < size; ++i)
30     {
31         temp = array_a[i];
32         array_c[temp]++;
33     }
34     
35     for(i = 1; i < max; ++i)
36     {
37         array_c[i] += array_c[i - 1];
38     }
39     for (i = 0; i < max; i++)
40     {
41         cout << array_c[i] << " ";
42     }
43     cout << endl;
44     for(i = 0; i < size; ++i)
45     {
46         temp = array_c[array_a[i]] - 1;
47         array_b[temp] = array_a[i];
48         --array_c[array_a[i]];
49     }
50     memcpy(array_a, array_b, size * sizeof(int));
51     
52     
53     delete[] array_b;
54     delete[] array_c;
55     return;
56 }
57 
58 #endif

 

1  / * *********************************************** *
 2  Function: // insert_sort
 . 3  the Description: // insertion sort
 . 4  the input: // array_a: the input array
 . 5                  // size: number of array elements
 . 6  the Return: // no
 7  *********** ************************************* * / 
. 8  void insert_sort ( int * array_a, int size)
 . 9  {
 10      int I, J, TEMP;    
 . 11      for (I = . 1 ; I <size; ++ I)
 12 is      {        
 13 is         temp = array_a[i];
14         for(j = i - 1; j >= 0; --j)
15         {
16             if(array_a[j] > temp)
17             {                
18                 array_a[j + 1] = array_a[j];
19             }
20             else
21             {
22                 break;
23             }
24         }
25         array_a[j + 1] = temp;
26         
27         for(j = 0 ; j < size; j++)
28         {
29             cout << array_a[j] << " ";
30         }
31         cout << endl;
32     }
33 }

 

Reproduced in: https: //www.cnblogs.com/liuxg/archive/2013/03/27/2984542.html

Guess you like

Origin blog.csdn.net/weixin_33908217/article/details/93433628