C++选择排序

冒泡排序在处理大型数组时的效率不够理想,因为经常需要重复的数据交换来将单个项目放置到正确的位置。选择排序和冒泡排序一样,每趟只放置一个项目到正确的位置。但是,通常情况下它执行的交换会比较少,因为它会立即将项目移动到数组中正确的位置。本文实现了选择排序的升序排列降序排列

 1 #include <iostream>
 2 int maxOfArray(int arr[], int start, int end);
 3 int minOfArray(int arr[], int start, int end);
 4 void SelectionSort(int arr[], int n);
 5 void InverseSelectionSort(int arr[], int n);
 6 void SelectSort(int arr[], int n, int (*pf)(int [], int, int));
 7 void printArray(int arr[], int n);
 8 const int n = 6;
 9 int main(int argc, char **argv) {
10     int a1[n] = {5,7,2,8,9,1};
11     int a2[n] = {5,7,2,8,9,1};
12     int a3[n] = {5,7,2,8,9,1};
13     int a4[n] = {5,7,2,8,9,1};
14     //SelectionSort
15     std::cout << "SelectionSort: ";
16     SelectionSort(a1,n);
17     printArray(a1,n);
18     //InverseSelectionSort
19     std::cout << "InverseSelectionSort: ";
20     InverseSelectionSort(a2,n);
21     printArray(a2,n);
22     //SelectSort(ascending sort)
23     std::cout << "SelectSort(ascending sort): ";
24     SelectSort(a3,n,minOfArray);
25     printArray(a3,n);
26     //SelectSort(descending sort)
27     std::cout << "SelectSort(descending sort): ";
28     SelectSort(a4,n,maxOfArray);
29     printArray(a4,n);
30     return 0;
31 }
32 //函数功能:给定数组起始点start和终止点end,返回数组中最大元素的索引
33 int maxOfArray(int arr[], int start, int end)
34 {
35     int temp = arr[start],index;
36     for(int i = start; i < end; i++)
37     {
38         if(arr[i] > temp)
39         {
40             temp = arr[i];
41             index = i;
42         }
43     }
44     return index;
45 }
46 //函数功能:给定数组起始点start和终止点end,返回数组中最小元素的索引
47 int minOfArray(int arr[], int start, int end)
48 {
49     int temp = arr[start],index;
50     for(int i = start; i < end; i++)
51     {
52         if(arr[i] < temp)
53         {
54             temp = arr[i];
55             index = i;
56         }
57     }
58     return index;
59 }
60 //函数功能:按照从小到大的顺序对数组进行排列
61 void SelectionSort(int arr[], int n)
62 {
63     int index;
64     for(int i = 0; i < n - 1; i++)
65     {
66         index = minOfArray(arr,i,n);
67         std::swap(arr[i],arr[index]);
68     }
69 }
70 //函数功能:按照从大到小的顺序对数组进行排列
71 void InverseSelectionSort(int arr[], int n)
72 {
73     int index;
74     for(int i = 0; i < n - 1; i++)
75     {
76         index = maxOfArray(arr,i,n);
77         std::swap(arr[i],arr[index]);
78     }
79 }
80 //函数功能:通过对第三个参数传入函数指针的方式,实现数组的升序排列(minOfArray)和降序排列(maxOfArray)
81 void SelectSort(int arr[], int n, int (*pf)(int [], int, int))
82 {
83     int index;
84     for(int i = 0; i < n - 1; i++)
85     {
86         index = (*pf)(arr,i,n);
87         std::swap(arr[i],arr[index]);
88     }
89 }
90 //函数功能:打印数组
91 void printArray(int arr[], int n)
92 {
93     for(int i = 0; i < n; i ++)
94     {
95         std::cout << arr[i] << " ";
96     }
97     std::cout << std::endl;
98 }

输出:

SelectionSort: 1 2 5 7 8 9 
InverseSelectionSort: 9 8 7 5 2 1 
SelectSort(ascending sort): 1 2 5 7 8 9 
SelectSort(descending sort): 9 8 7 5 2 1

猜你喜欢

转载自www.cnblogs.com/bindeer2019/p/10614188.html