编写方法按身高进行排序
时间: 2023-08-22 08:42:02 浏览: 103
好的,按身高进行排序可以有多种方法,我这里提供一种基于Java语言的实现方式:
```java
public void sortByHeight(int[] heights) {
int n = heights.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (heights[i] > heights[j]) {
int temp = heights[i];
heights[i] = heights[j];
heights[j] = temp;
}
}
}
}
```
这是一个简单的冒泡排序算法,在每一轮循环中,比较相邻的两个元素的大小关系,如果前一个元素较大,则交换它们的位置,这样每一轮循环都会将当前未排序的最小值置于最前面,最终实现了按身高升序排序的效果。
相关问题
声明一个类People,成员变量有姓名,出生日期,性别,身高,体重等;生成10个类对象,并放在一个一维数组中,编写方法按身高进行排序
好的,我会尽力回答您的问题。下面是代码示例:
```python
class People:
def __init__(self, name, birthday, gender, height, weight):
self.name = name
self.birthday = birthday
self.gender = gender
self.height = height
self.weight = weight
people_list = [People("张三", "1990-01-01", "男", 175, 65),
People("李四", "1992-02-02", "女", 170, 60),
People("王五", "1995-03-03", "男", 180, 70),
People("赵六", "1998-04-04", "女", 165, 50),
People("钱七", "2000-05-05", "男", 185, 75),
People("孙八", "2002-06-06", "女", 160, 45),
People("周九", "2005-07-07", "男", 190, 80),
People("吴十", "2008-08-08", "女", 155, 40),
People("郑十一", "2010-09-09", "男", 195, 85),
People("冯十二", "2012-10-10", "女", 150, 35)]
def sort_by_height(people_list):
return sorted(people_list, key=lambda x: x.height)
sorted_people_list = sort_by_height(people_list)
for p in sorted_people_list:
print(p.name, p.height)
```
以上代码中,我们定义了一个`People`类,包含了姓名、出生日期、性别、身高和体重等属性。我们创建了一个包含10个`People`对象的列表`people_list`,并使用`sorted()`函数按照身高进行排序,返回一个新的排序后的列表`sorted_people_list`。最后我们遍历打印排序后的列表中每个人的姓名和身高。
希望能对您有所帮助!
java中声明一个类People,成员变量有姓名,出生日期,性别,身高,体重等;生成10个类对象,并放在一个一维数组中,编写方法按身高进行排序
好的,这里是Java语言的代码示例:
```java
import java.util.Arrays;
import java.util.Comparator;
public class People {
private String name;
private String birthday;
private String gender;
private int height;
private int weight;
public People(String name, String birthday, String gender, int height, int weight) {
this.name = name;
this.birthday = birthday;
this.gender = gender;
this.height = height;
this.weight = weight;
}
public static void main(String[] args) {
People[] peopleList = {
new People("张三", "1990-01-01", "男", 175, 65),
new People("李四", "1992-02-02", "女", 170, 60),
new People("王五", "1995-03-03", "男", 180, 70),
new People("赵六", "1998-04-04", "女", 165, 50),
new People("钱七", "2000-05-05", "男", 185, 75),
new People("孙八", "2002-06-06", "女", 160, 45),
new People("周九", "2005-07-07", "男", 190, 80),
new People("吴十", "2008-08-08", "女", 155, 40),
new People("郑十一", "2010-09-09", "男", 195, 85),
new People("冯十二", "2012-10-10", "女", 150, 35)
};
Arrays.sort(peopleList, Comparator.comparing(People::getHeight));
for (People p : peopleList) {
System.out.println(p.getName() + " " + p.getHeight());
}
}
public String getName() {
return name;
}
public String getBirthday() {
return birthday;
}
public String getGender() {
return gender;
}
public int getHeight() {
return height;
}
public int getWeight() {
return weight;
}
}
```
以上代码中,我们定义了一个`People`类,包含了姓名、出生日期、性别、身高和体重等属性。我们创建了一个包含10个`People`对象的数组`peopleList`,并使用`Arrays.sort()`方法按照身高进行排序,返回一个新的排序后的数组。最后我们遍历打印排序后的数组中每个人的姓名和身高。
希望能对您有所帮助!