Sort Recreation

Sort Recreation

In this task, you will recreate the basic functionality of std::sort. Create a Student struct that holds int age, std::string name, int average_grades. Write a function std::vector<Student> sortStudents(std::vector<Student> student_list, bool(compare*)(Student, Student));. The compare function should take 2 Student paramters, and should be used to decide which students come first in the array by returning true if the first student should come first, and false if the second student should come first.

sortStudents should return a vector array that is sorted based on the rules set by the compare function.

Implement 3 compare functions:

bool compareByAge(Student s1, Student s2);
bool compareByNameLength(Student s1, Student s2);
bool compareByAverageGrade(Student s1, Student s2);

Show examples of them being used.

Give it a shot before looking at the instructions.

Instructions

One of the simplest sorting algorithms is called bubble sort. To sort an array, you can step through it, compare adjacent elements, and swap them if they are in the wrong order. You repeat this until the array is sorted.

Steps:

  1. Start from the beginning of the list.
  2. Compare each pair of adjacent elements.
  3. Swap the elements if they are in the wrong order.
  4. Repeat steps 1-3 for each element in the list until no more swaps are needed.

Here is an example output:

Sorted by age:
21 Bob C
20 John A
19 Alice B
18 Eve D
Sorted by name length:
19 Alice B
20 John A
21 Bob C
18 Eve D
Sorted by average grade:
20 John A
19 Alice B
21 Bob C
18 Eve D

Answer

#include <iostream>
#include <string>
#include <vector>

struct Student {
    int age;
    std::string name;
    char average_grade;
};

bool compareByAge(const Student& s1, const Student& s2) {
	return s1.age < s2.age;
}

bool compareByNameLength(const Student& s1, const Student& s2) {
    return s1.name.length() < s2.name.length();
}

bool compareByAverageGrade(const Student& s1, const Student& s2) {
	return s1.average_grade > s2.average_grade;
}

std::vector<Student> sortStudents(std::vector<Student> students, bool(*compare)(const Student&, const Student&)) {

	// loop for each array element
	for (int step = 0; step < students.size(); ++step) {
		// loop to compare array elements
		for (int i = 0; i < students.size() - step - 1; ++i) {
			// check if they are in the wrong order
			if (compare(students[i],students[i + 1])) {
				// swapping elements if they are in the wrong order
				Student temp = students[i];
				students[i] = students[i + 1];
				students[i + 1] = temp;
			}
		}
	}

    return students;
}

int main() {
	std::vector<Student> exampleStudents = {
		Student{ 20, "John", 'A' },
		Student{ 19, "Alice", 'B' },
		Student{ 21, "Bob", 'C' },
		Student{ 18, "Eve", 'D' }
	};

	std::vector<Student> studentsSortedByAge = sortStudents(exampleStudents, compareByAge);
	std::vector<Student> studentsSortedByNameLength = sortStudents(exampleStudents, compareByNameLength);
	std::vector<Student> studentsSortedByAverageGrade = sortStudents(exampleStudents, compareByAverageGrade);

	std::cout << "Sorted by age:" << std::endl;
	for (Student student : studentsSortedByAge) {
		std::cout << student.age << " " << student.name << " " << student.average_grade << std::endl;
	}

	std::cout << "Sorted by name length:" << std::endl;
	for (Student student : studentsSortedByNameLength) {
		std::cout << student.age << " " << student.name << " " << student.average_grade << std::endl;
	}

	std::cout << "Sorted by average grade:" << std::endl;
	for (Student student : studentsSortedByAverageGrade) {
		std::cout << student.age << " " << student.name << " " << student.average_grade << std::endl;
	}
}
Questions