Practice Assignment: Search and Sort Data
Practice Assignment: Search and Sort Data
Practice Data Structures and Algorithms by building a small program that searches and sorts student data. This assignment will help you apply arrays/lists, linear search, binary search, sorting algorithms, Big O notation, and real-world problem-solving skills.
Assignment Introduction
In this practice assignment, students will apply the concepts learned in the Data Structures and Algorithms basics chapter. Until now, you have learned about arrays, linked lists, stacks, queues, hash tables, trees, graphs, searching algorithms, sorting algorithms, and Big O notation.
Now it is time to use some of these concepts in a practical mini project. In this assignment, students will create a program that stores student records, searches for specific student information, and sorts student data based on marks or names.
This assignment is designed to help students understand how searching and sorting are used in real-world applications such as student management systems, result processing systems, product catalogs, contact lists, leaderboards, and reports.
Assignment Objective
The main objective of this assignment is to design a simple program that can search and sort student records using basic algorithms.
Students should understand not only how to write the code, but also why a particular algorithm is used. They should be able to explain when linear search is useful, when binary search is useful, and why sorting improves data readability and searching efficiency.
Learning Objectives
- Store multiple student records using an array or list.
- Search for a student by roll number using linear search.
- Search for a student by roll number using binary search after sorting.
- Sort student records by marks in ascending or descending order.
- Sort student records by name alphabetically.
- Understand the difference between unsorted and sorted data.
- Compare linear search and binary search.
- Apply basic Big O notation to searching and sorting operations.
- Write clean and readable algorithm-based code.
- Connect searching and sorting with real-world applications.
Prerequisites
Before attempting this assignment, students should understand the following topics. These prerequisites will help them complete the assignment confidently.
| Prerequisite Topic | Why It Is Needed |
|---|---|
| Variables and Data Types | To store roll number, name, marks, grade, and other student details. |
| Arrays / Lists | To store multiple student records together. |
| Objects / Records | To represent one student as a structured unit of data. |
| Loops | To traverse student records during searching and sorting. |
| Conditional Statements | To compare roll numbers, names, and marks. |
| Functions / Methods | To separate logic such as searchStudent(), sortByMarks(), and displayStudents(). |
| Linear Search | To search records one by one in unsorted data. |
| Binary Search | To search faster in sorted data. |
| Sorting Algorithms | To arrange records by marks, name, or roll number. |
| Big O Notation | To understand and compare algorithm efficiency. |
Problem Statement
Design a simple program named Student Search and Sort System. The program should store multiple student records and allow the user to search and sort the data.
Each student record should contain:
- Roll number
- Student name
- Course name
- Marks
- Grade
Functional Requirements
Functional requirements describe what the program should do.
Required Features
- Create a collection of student records.
- Display all student records.
- Search student by roll number using linear search.
- Sort students by roll number.
- Search student by roll number using binary search after sorting by roll number.
- Sort students by marks in ascending order.
- Sort students by marks in descending order.
- Sort students by name alphabetically.
- Display highest-scoring student.
- Display lowest-scoring student.
- Show meaningful messages when a student is found or not found.
- Use functions or methods to organize the program.
Non-Functional Requirements
Non-functional requirements describe how the program should be designed.
Design Requirements
- The program should be easy to understand.
- The code should be clean and properly organized.
- Variable and function names should be meaningful.
- Searching and sorting logic should be written separately.
- The program should avoid unnecessary duplicate code.
- The output should be clear and readable.
- The solution should explain which algorithm is used.
- The solution should mention basic time complexity.
Sample Student Data
Students can use the following sample dataset for this assignment.
| Roll Number | Name | Course | Marks | Grade |
|---|---|---|---|---|
| 104 | Priya | Programming Fundamentals | 88 | B |
| 101 | Rahul | Programming Fundamentals | 85 | B |
| 103 | John | Programming Fundamentals | 76 | C |
| 102 | Ayesha | Programming Fundamentals | 92 | A |
| 105 | Karan | Programming Fundamentals | 69 | C |
Suggested Data Structure
Since this is a beginner-level assignment, students can use an array or list of student records. Each student record can be represented as an object, structure, dictionary, or class depending on the programming language.
// Conceptual student record
Student
{
rollNumber
name
course
marks
grade
}
// Conceptual list
students = [
Student(104, "Priya", "Programming Fundamentals", 88, "B"),
Student(101, "Rahul", "Programming Fundamentals", 85, "B"),
Student(103, "John", "Programming Fundamentals", 76, "C"),
Student(102, "Ayesha", "Programming Fundamentals", 92, "A"),
Student(105, "Karan", "Programming Fundamentals", 69, "C")
]
Assignment Task 1: Display All Student Records
First, create a function or method to display all student records. This will help verify that the data is stored correctly.
Requirements
- Loop through all student records.
- Display roll number, name, course, marks, and grade.
- Format the output clearly.
// Conceptual display logic
function displayStudents(students):
for each student in students:
print student.rollNumber
print student.name
print student.course
print student.marks
print student.grade
Assignment Task 2: Linear Search by Roll Number
Create a function to search for a student by roll number using linear search. Linear search checks each student record one by one until the required roll number is found.
// Conceptual linear search
function linearSearchByRoll(students, targetRoll):
for each student in students:
if student.rollNumber == targetRoll:
return student
return null
If the student is found, display the student record. If not found, display a message such as "Student not found."
Assignment Task 3: Sort Students by Roll Number
Binary search requires sorted data. Therefore, before using binary search, students should sort the student records by roll number.
Requirements
- Sort students in ascending order of roll number.
- Use any simple sorting algorithm such as Bubble Sort, Selection Sort, or Insertion Sort.
- Display the sorted student list after sorting.
// Conceptual sort by roll number
function sortByRollNumber(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].rollNumber > students[j + 1].rollNumber:
swap students[j] and students[j + 1]
Assignment Task 4: Binary Search by Roll Number
After sorting students by roll number, create a function to search by roll number using binary search.
// Conceptual binary search
function binarySearchByRoll(students, targetRoll):
left = 0
right = students.length - 1
while left <= right:
middle = (left + right) / 2
if students[middle].rollNumber == targetRoll:
return students[middle]
else if targetRoll < students[middle].rollNumber:
right = middle - 1
else:
left = middle + 1
return null
Students should compare this approach with linear search and explain why binary search can be faster for large sorted data.
Assignment Task 5: Sort Students by Marks Ascending
Create a function to sort students by marks from lowest to highest.
// Conceptual sort by marks ascending
function sortByMarksAscending(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].marks > students[j + 1].marks:
swap students[j] and students[j + 1]
Expected sorted order by marks ascending:
Karan - 69
John - 76
Rahul - 85
Priya - 88
Ayesha - 92
Assignment Task 6: Sort Students by Marks Descending
Create a function to sort students by marks from highest to lowest. This is useful for ranking students.
// Conceptual sort by marks descending
function sortByMarksDescending(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].marks < students[j + 1].marks:
swap students[j] and students[j + 1]
Expected sorted order by marks descending:
Ayesha - 92
Priya - 88
Rahul - 85
John - 76
Karan - 69
Assignment Task 7: Sort Students by Name
Create a function to sort students alphabetically by name. This is similar to sorting a contact list.
// Conceptual sort by name
function sortByName(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].name > students[j + 1].name:
swap students[j] and students[j + 1]
Expected alphabetical order:
Ayesha
John
Karan
Priya
Rahul
Assignment Task 8: Find Highest and Lowest Marks
Create functions to find the highest-scoring and lowest-scoring students. This can be done by traversing the list once.
// Conceptual highest marks logic
function findHighestScorer(students):
highest = students[0]
for each student in students:
if student.marks > highest.marks:
highest = student
return highest
// Conceptual lowest marks logic
function findLowestScorer(students):
lowest = students[0]
for each student in students:
if student.marks < lowest.marks:
lowest = student
return lowest
Assignment Task 9: Create a Menu-Based Program
As an optional improvement, students can create a menu-based program. This makes the assignment more interactive and closer to a real application.
===== Student Search and Sort System =====
1. Display all students
2. Linear search by roll number
3. Sort by roll number
4. Binary search by roll number
5. Sort by marks ascending
6. Sort by marks descending
7. Sort by name
8. Show highest scorer
9. Show lowest scorer
10. Exit
Enter your choice:
The menu should continue running until the user chooses Exit.
Suggested Program Flow
The following flow shows how the program should work.
Start Program
Create student list
Show menu
If user chooses display:
display all students
If user chooses linear search:
input roll number
search one by one
display result
If user chooses sort by roll number:
sort records by roll number
display sorted records
If user chooses binary search:
ensure records are sorted by roll number
input roll number
perform binary search
display result
If user chooses sort by marks:
sort records by marks
display sorted records
If user chooses sort by name:
sort records by name
display sorted records
If user chooses highest or lowest:
traverse records
display result
If user chooses exit:
stop program
Complete Language-Neutral Pseudocode Solution
The following pseudocode gives a complete idea of the assignment logic. Students can convert it into any programming language such as Java, Python, JavaScript, C#, PHP, or C++.
students = [
{ rollNumber: 104, name: "Priya", course: "Programming Fundamentals", marks: 88, grade: "B" },
{ rollNumber: 101, name: "Rahul", course: "Programming Fundamentals", marks: 85, grade: "B" },
{ rollNumber: 103, name: "John", course: "Programming Fundamentals", marks: 76, grade: "C" },
{ rollNumber: 102, name: "Ayesha", course: "Programming Fundamentals", marks: 92, grade: "A" },
{ rollNumber: 105, name: "Karan", course: "Programming Fundamentals", marks: 69, grade: "C" }
]
function displayStudents(students):
for each student in students:
print student.rollNumber, student.name, student.course, student.marks, student.grade
function linearSearchByRoll(students, targetRoll):
for each student in students:
if student.rollNumber == targetRoll:
return student
return null
function sortByRollNumber(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].rollNumber > students[j + 1].rollNumber:
swap students[j] and students[j + 1]
function binarySearchByRoll(students, targetRoll):
left = 0
right = students.length - 1
while left <= right:
middle = floor((left + right) / 2)
if students[middle].rollNumber == targetRoll:
return students[middle]
else if targetRoll < students[middle].rollNumber:
right = middle - 1
else:
left = middle + 1
return null
function sortByMarksAscending(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].marks > students[j + 1].marks:
swap students[j] and students[j + 1]
function sortByMarksDescending(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].marks < students[j + 1].marks:
swap students[j] and students[j + 1]
function sortByName(students):
for i from 0 to students.length - 1:
for j from 0 to students.length - i - 2:
if students[j].name > students[j + 1].name:
swap students[j] and students[j + 1]
function findHighestScorer(students):
highest = students[0]
for each student in students:
if student.marks > highest.marks:
highest = student
return highest
function findLowestScorer(students):
lowest = students[0]
for each student in students:
if student.marks < lowest.marks:
lowest = student
return lowest
Optional Java-Style Sample Solution
The following Java-style solution shows how this assignment can be implemented using a Student class and an array of students. Students can use this as a reference after trying the assignment themselves.
class Student {
int rollNumber;
String name;
String course;
int marks;
String grade;
Student(int studentRollNumber, String studentName, String studentCourse, int studentMarks, String studentGrade) {
rollNumber = studentRollNumber;
name = studentName;
course = studentCourse;
marks = studentMarks;
grade = studentGrade;
}
void display() {
System.out.println(rollNumber + " | " + name + " | " + course + " | " + marks + " | " + grade);
}
}
public class Main {
static void displayStudents(Student[] students) {
for (int i = 0; i < students.length; i++) {
students[i].display();
}
}
static Student linearSearchByRoll(Student[] students, int targetRoll) {
for (int i = 0; i < students.length; i++) {
if (students[i].rollNumber == targetRoll) {
return students[i];
}
}
return null;
}
static void sortByRollNumber(Student[] students) {
for (int i = 0; i < students.length - 1; i++) {
for (int j = 0; j < students.length - i - 1; j++) {
if (students[j].rollNumber > students[j + 1].rollNumber) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
static Student binarySearchByRoll(Student[] students, int targetRoll) {
int left = 0;
int right = students.length - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (students[middle].rollNumber == targetRoll) {
return students[middle];
} else if (targetRoll < students[middle].rollNumber) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return null;
}
static void sortByMarksDescending(Student[] students) {
for (int i = 0; i < students.length - 1; i++) {
for (int j = 0; j < students.length - i - 1; j++) {
if (students[j].marks < students[j + 1].marks) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
static void sortByName(Student[] students) {
for (int i = 0; i < students.length - 1; i++) {
for (int j = 0; j < students.length - i - 1; j++) {
if (students[j].name.compareTo(students[j + 1].name) > 0) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
static Student findHighestScorer(Student[] students) {
Student highest = students[0];
for (int i = 1; i < students.length; i++) {
if (students[i].marks > highest.marks) {
highest = students[i];
}
}
return highest;
}
public static void main(String[] args) {
Student[] students = {
new Student(104, "Priya", "Programming Fundamentals", 88, "B"),
new Student(101, "Rahul", "Programming Fundamentals", 85, "B"),
new Student(103, "John", "Programming Fundamentals", 76, "C"),
new Student(102, "Ayesha", "Programming Fundamentals", 92, "A"),
new Student(105, "Karan", "Programming Fundamentals", 69, "C")
};
System.out.println("All Students:");
displayStudents(students);
System.out.println("Linear Search Result:");
Student result1 = linearSearchByRoll(students, 102);
if (result1 != null) {
result1.display();
} else {
System.out.println("Student not found");
}
sortByRollNumber(students);
System.out.println("Students Sorted by Roll Number:");
displayStudents(students);
System.out.println("Binary Search Result:");
Student result2 = binarySearchByRoll(students, 103);
if (result2 != null) {
result2.display();
} else {
System.out.println("Student not found");
}
sortByMarksDescending(students);
System.out.println("Students Sorted by Marks Descending:");
displayStudents(students);
sortByName(students);
System.out.println("Students Sorted by Name:");
displayStudents(students);
Student highest = findHighestScorer(students);
System.out.println("Highest Scorer:");
highest.display();
}
}
Sample Output
The exact output may vary depending on formatting, but the program output should look similar to this:
All Students:
104 | Priya | Programming Fundamentals | 88 | B
101 | Rahul | Programming Fundamentals | 85 | B
103 | John | Programming Fundamentals | 76 | C
102 | Ayesha | Programming Fundamentals | 92 | A
105 | Karan | Programming Fundamentals | 69 | C
Linear Search Result:
102 | Ayesha | Programming Fundamentals | 92 | A
Students Sorted by Roll Number:
101 | Rahul | Programming Fundamentals | 85 | B
102 | Ayesha | Programming Fundamentals | 92 | A
103 | John | Programming Fundamentals | 76 | C
104 | Priya | Programming Fundamentals | 88 | B
105 | Karan | Programming Fundamentals | 69 | C
Binary Search Result:
103 | John | Programming Fundamentals | 76 | C
Students Sorted by Marks Descending:
102 | Ayesha | Programming Fundamentals | 92 | A
104 | Priya | Programming Fundamentals | 88 | B
101 | Rahul | Programming Fundamentals | 85 | B
103 | John | Programming Fundamentals | 76 | C
105 | Karan | Programming Fundamentals | 69 | C
Highest Scorer:
102 | Ayesha | Programming Fundamentals | 92 | A
Big O Analysis for This Assignment
Students should include a short complexity explanation in their submission. This helps connect the assignment with Big O notation.
| Operation | Algorithm Used | Time Complexity | Explanation |
|---|---|---|---|
| Display all students | Traversal | O(n) | Every student record is visited once. |
| Search by roll number | Linear Search | O(n) | May check all records in worst case. |
| Search by roll number after sorting | Binary Search | O(log n) | Search area is divided in half each step. |
| Sort by roll number | Bubble Sort | O(n²) | Uses nested loops for comparison and swapping. |
| Sort by marks | Bubble Sort | O(n²) | Compares records repeatedly. |
| Find highest scorer | Linear Traversal | O(n) | Checks each student's marks once. |
Extension Tasks
After completing the basic assignment, students can improve the project by adding more features.
Optional Enhancements
- Search student by name using linear search.
- Search all students with marks greater than a given value.
- Sort students by grade.
- Sort students by course name.
- Use insertion sort instead of bubble sort.
- Use selection sort and compare it with bubble sort.
- Use merge sort for faster sorting.
- Store students in a hash table using roll number as key.
- Read student records from a CSV file.
- Save sorted records into a new file.
- Create a menu-based console application.
- Add duplicate roll number validation.
Evaluation Criteria
The assignment can be evaluated using the following criteria.
| Criteria | Marks | What to Check |
|---|---|---|
| Student Data Structure | 10 | Student records are stored properly. |
| Display Function | 10 | All student records are displayed clearly. |
| Linear Search | 15 | Student can be searched by roll number using linear search. |
| Sorting by Roll Number | 15 | Records are sorted correctly by roll number. |
| Binary Search | 15 | Binary search works correctly after sorting. |
| Sorting by Marks | 15 | Records are sorted correctly by marks. |
| Sorting by Name | 10 | Records are sorted alphabetically by name. |
| Big O Explanation | 5 | Basic time complexity is explained correctly. |
| Code Readability | 5 | Code is clean, structured, and easy to understand. |
| Total | 100 | Complete search and sort assignment. |
Common Mistakes to Avoid
Students should avoid the following mistakes while completing this assignment.
Mistakes
- Using binary search without sorting the data first.
- Sorting by marks but then using binary search by roll number without sorting by roll number again.
- Confusing index with roll number.
- Not returning a clear result when the student is not found.
- Using wrong comparison for descending order.
- Forgetting to swap complete student records.
- Swapping only marks but not the full student record.
- Using unclear variable names.
- Writing all logic inside one large block without functions.
- Not explaining algorithm complexity.
Correct Approach
- Use linear search for unsorted data.
- Sort by roll number before binary search.
- Compare roll number with roll number, not index.
- Return null or a clear message if not found.
- Use correct comparison for ascending and descending order.
- Swap the full student object or record.
- Use separate functions for search and sort.
- Use meaningful names such as targetRoll, minIndex, highestStudent.
- Test with found and not-found cases.
- Write Big O explanation for each main operation.
Best Practices
Follow these best practices to create a clean and reliable solution.
Recommended Practices
- Use a structured student record instead of separate unrelated arrays.
- Keep search logic and sorting logic separate.
- Display output clearly using headings.
- Use linear search before sorting to understand basic search.
- Use binary search only after sorting by the searched field.
- Swap complete records while sorting.
- Test with different roll numbers.
- Test with duplicate marks.
- Test with students already sorted.
- Test with students in reverse order.
- Add comments only where they improve understanding.
- Write a short explanation of algorithm choice.
Frequently Asked Questions
1. What is the main goal of this assignment?
The main goal is to practice searching and sorting algorithms using a real-world student record example.
2. Which search algorithm should be used first?
Linear search should be used first because it works on unsorted data and is easier for beginners.
3. When should binary search be used?
Binary search should be used only after the data is sorted by the same field being searched. For example, to search by roll number, records must be sorted by roll number.
4. Why should we sort students by marks?
Sorting by marks helps create rankings, identify top performers, and generate result reports.
5. Why should we sort students by name?
Sorting by name helps display records alphabetically, similar to a contact list or attendance sheet.
6. What happens if only marks are swapped during sorting?
The records become incorrect because marks may no longer belong to the correct student. The complete student record should be swapped.
7. What is the time complexity of linear search?
The worst-case time complexity of linear search is O(n).
8. What is the time complexity of binary search?
The worst-case time complexity of binary search is O(log n), but it requires sorted data.
9. What is the time complexity of Bubble Sort?
The average and worst-case time complexity of Bubble Sort is O(n²).
10. Can this assignment be done in any programming language?
Yes. This assignment can be implemented in Java, Python, JavaScript, C#, PHP, C++, or any language that supports arrays/lists, loops, and functions.
Assignment Summary
This practice assignment helps students apply searching and sorting algorithms in a practical context. Students create a small student record system that stores records and performs operations such as linear search, binary search, sorting by roll number, sorting by marks, sorting by name, and finding the highest scorer.
The assignment reinforces important DSA concepts such as arrays/lists, traversal, comparison, swapping, algorithm design, and Big O notation. It also teaches students when to use linear search, when to use binary search, and why sorted data is important.
By completing this assignment, students will understand how search and sort operations are used in real-world systems such as student management systems, result processing systems, e-commerce platforms, contact lists, dashboards, and reports.
Key Takeaway
The Search and Sort Data assignment teaches students how to find and organize records using algorithms. Linear search is useful for unsorted data, binary search is useful for sorted data, and sorting algorithms help arrange data for ranking, reporting, searching, and better readability.