其于插入排序第法针对水果售责信息进行排序代码
时间: 2023-12-21 14:30:57 浏览: 25
好的,插入排序是一种简单直观的排序算法,它的基本思想是将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增加1的有序表。对于水果售责信息进行排序,可以根据需要排序的字段进行比较和交换。
以下是一个使用插入排序对水果售责信息按照价格进行排序的示例代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
struct Fruit {
string name;
double price;
};
void insertionSort(vector<Fruit>& fruits) {
int n = fruits.size();
for (int i = 1; i < n; i++) {
Fruit key = fruits[i];
int j = i - 1;
while (j >= 0 && fruits[j].price > key.price) {
fruits[j + 1] = fruits[j];
j--;
}
fruits[j + 1] = key;
}
}
int main() {
vector<Fruit> fruits = {{"apple", 2.5}, {"banana", 1.8}, {"orange", 3.2}, {"pear", 2.0}};
insertionSort(fruits);
for (auto fruit : fruits) {
cout << fruit.name << " " << fruit.price << endl;
}
return 0;
}
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![sb3](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)