选择排序练习题c++
时间: 2023-12-02 18:41:44 浏览: 59
以下是使用选择排序对int型数组元素由小到大排序的C++代码实现:
```c++
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
int i, j, minIndex, tmp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
if (minIndex != i) {
tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
}
}
int main() {
int arr[] = {4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
```
相关问题
c++结构体排序练习题
在C++中,结构体排序练习通常涉及到如何使用标准库中的算法对包含自定义数据类型的结构体数组或容器进行排序。这里我们可以举一个简单的例子,假设有一个名为`Student`的结构体,包含`name`和`age`两个成员:
```cpp
struct Student {
std::string name;
int age;
};
```
你可以用以下几种方法对`Student`结构体数组进行排序:
1. **直接排序:**如果年龄是排序的主要依据,你可以定义一个比较函数(`compare`),然后使用`std::sort`函数:
```cpp
bool compareStudents(const Student& s1, const Student& s2) {
return s1.age < s2.age;
}
int main() {
Student students[] = {{"Alice", 20}, {"Bob", 18}, {"Charlie", 22}};
std::sort(students, students + sizeof(students) / sizeof(students), compareStudents);
// 现在students数组按年龄升序排列
}
```
2. **使用STL算法:**如果你的结构体已经实现了`<`运算符,那么可以直接使用`std::stable_sort`:
```cpp
bool studentLess(const Student& s1, const Student& s2) {
return s1.age < s2.age;
}
int main() {
std::vector<Student> students = {{"Alice", 20}, {"Bob", 18}, {"Charlie", 22}};
std::stable_sort(students.begin(), students.end(), studentLess);
}
```
3. **自定义比较器(C++11及以上):**也可以使用lambda表达式来创建一个可传递的比较器:
```cpp
int main() {
std::vector<Student> students = {{"Alice", 20}, {"Bob", 18}, {"Charlie", 22}};
std::sort(students.begin(), students.end(), [](const Student& s1, const Student& s2) {
return s1.age < s2.age;
});
}
```
c++一维数组练习题
C语言中的一维数组是指只有一个维度的数组,可以存储多个相同类型的数据。
一维数组的使用分为声明、初始化和访问三个步骤。
声明一维数组需要指定数组的类型和元素个数,例如int numbers[5]表示声明了一个包含5个整数的数组。初始化数组可以直接给数组的元素赋初值,也可以通过循环等方式逐个赋值。访问数组的元素可以通过下标访问,数组的第一个元素下标为0,依次递增。
在练习题中,我们可以通过使用一维数组来解决问题。例如,统计一段字符串中某个字符的出现次数,可以使用一个计数器数组来记录每个字符出现的次数。又如,将一个数组中的元素进行反转和排序操作时,我们可以利用一个辅助数组来实现交换位置。
一维数组的应用非常广泛,它可以用来存储一组数据,方便处理和管理。在程序中使用一维数组可以提高代码的效率和可读性,使代码更加简洁和优雅。
总结来说,一维数组是一种常见的数据结构,它能够存储多个相同类型的数据,并通过下标访问和操作数组元素。在解决问题时,我们可以灵活运用一维数组的特性,提高程序的效率和可读性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)