数据结构-排序(选择排序)
1.直接选择算法
1 // 直接选择排序
2 //Code by Larman Yuan 2009-06-29
3
4 #include "stdafx.h"
5
6 void selectSort(int * sortArray, int size)
7 {
8 int i,j,k, temp;
9 for( i = 0; i < size - 1; i++)
10 {
11 k = i;
12 for(j = i + 1; j < size; j++)
13 {
14 if(sortArray[j] < sortArray[k])
15 {
16 k = j;
17 }
18 }
19 if(k != i)
20 {
21 temp = sortArray[k];
22 sortArray[k] = sortArray[i];
23 sortArray[i] = temp;
24 }
25 }
26 }
27
28 void printArray(int arrayToPrint[],int size)
29 {
30 for(int i = 0; i < size; i++)
31 {
32 printf("%d ", arrayToPrint[i]);
33 }
34 printf("\n");
35 }
36
37 int _tmain(int argc, _TCHAR* argv[])
38 {
39 int sortArray[8] = {49, 38, 65, 97,76, 13, 27, 49};
40 printf("Array before sort: ");
41 printArray(sortArray, 8);
42 selectSort(sortArray, 8);
43 printf("Array after sort : ");
44 printArray(sortArray, 8);
45 return 0;
46 }
2 //Code by Larman Yuan 2009-06-29
3
4 #include "stdafx.h"
5
6 void selectSort(int * sortArray, int size)
7 {
8 int i,j,k, temp;
9 for( i = 0; i < size - 1; i++)
10 {
11 k = i;
12 for(j = i + 1; j < size; j++)
13 {
14 if(sortArray[j] < sortArray[k])
15 {
16 k = j;
17 }
18 }
19 if(k != i)
20 {
21 temp = sortArray[k];
22 sortArray[k] = sortArray[i];
23 sortArray[i] = temp;
24 }
25 }
26 }
27
28 void printArray(int arrayToPrint[],int size)
29 {
30 for(int i = 0; i < size; i++)
31 {
32 printf("%d ", arrayToPrint[i]);
33 }
34 printf("\n");
35 }
36
37 int _tmain(int argc, _TCHAR* argv[])
38 {
39 int sortArray[8] = {49, 38, 65, 97,76, 13, 27, 49};
40 printf("Array before sort: ");
41 printArray(sortArray, 8);
42 selectSort(sortArray, 8);
43 printf("Array after sort : ");
44 printArray(sortArray, 8);
45 return 0;
46 }
运行结果 :
Array before sort: 49 38 65 97 76 13 27 49
Array after sort : 13 27 38 49 49 65 76 97
2.堆排序
1 // 堆排序
2 //Code by Larman Yuan 2009-06-29
3
4 #include "stdafx.h"
5 #define leftChild(i) 2*(i)+1
6
7 void sift(int* sortArray, int i, int size)
8 {
9 int child;
10 int temp;
11 temp = sortArray[i];
12 child = leftChild(i);
13 while(child < size)
14 {
15 if((child < size - 1) && (sortArray[child] < sortArray[child + 1]))
16 child++;
17 if(temp < sortArray[child])
18 {
19 sortArray[i] = sortArray[child];
20 i = child;
21 child = leftChild(i);
22 }
23 else
24 break;
25 }
26 sortArray[i] = temp;
27 }
28
29 void heapSort(int * sortArray, int size)
30 {
31 int i, n, temp;
32 n = size;
33 for(i = n / 2 - 1; i >= 0; i--)
34 {
35 sift(sortArray, i, n);
36 }
37 for(i = n - 1; i > 0; i--)
38 {
39 temp = sortArray[0];
40 sortArray[0] = sortArray[i];
41 sortArray[i] = temp;
42 sift(sortArray, 0, i);
43 }
44 }
45
46 void printArray(int arrayToPrint[],int size)
47 {
48 for(int i = 0; i < size; i++)
49 {
50 printf("%d ", arrayToPrint[i]);
51 }
52 printf("\n");
53 }
54
55 int _tmain(int argc, _TCHAR* argv[])
56 {
57 int sortArray[8] = {49, 38, 65, 97,76, 13, 27, 49};
58 printf("Array before sort: ");
59 printArray(sortArray, 8);
60 heapSort(sortArray, 8);
61 printf("Array after sort : ");
62 printArray(sortArray, 8);
63 return 0;
64 }
2 //Code by Larman Yuan 2009-06-29
3
4 #include "stdafx.h"
5 #define leftChild(i) 2*(i)+1
6
7 void sift(int* sortArray, int i, int size)
8 {
9 int child;
10 int temp;
11 temp = sortArray[i];
12 child = leftChild(i);
13 while(child < size)
14 {
15 if((child < size - 1) && (sortArray[child] < sortArray[child + 1]))
16 child++;
17 if(temp < sortArray[child])
18 {
19 sortArray[i] = sortArray[child];
20 i = child;
21 child = leftChild(i);
22 }
23 else
24 break;
25 }
26 sortArray[i] = temp;
27 }
28
29 void heapSort(int * sortArray, int size)
30 {
31 int i, n, temp;
32 n = size;
33 for(i = n / 2 - 1; i >= 0; i--)
34 {
35 sift(sortArray, i, n);
36 }
37 for(i = n - 1; i > 0; i--)
38 {
39 temp = sortArray[0];
40 sortArray[0] = sortArray[i];
41 sortArray[i] = temp;
42 sift(sortArray, 0, i);
43 }
44 }
45
46 void printArray(int arrayToPrint[],int size)
47 {
48 for(int i = 0; i < size; i++)
49 {
50 printf("%d ", arrayToPrint[i]);
51 }
52 printf("\n");
53 }
54
55 int _tmain(int argc, _TCHAR* argv[])
56 {
57 int sortArray[8] = {49, 38, 65, 97,76, 13, 27, 49};
58 printf("Array before sort: ");
59 printArray(sortArray, 8);
60 heapSort(sortArray, 8);
61 printf("Array after sort : ");
62 printArray(sortArray, 8);
63 return 0;
64 }
运行结果:
Array before sort: 49 38 65 97 76 13 27 49
Array after sort : 13 27 38 49 49 65 76 97