0% found this document useful (0 votes)
4 views3 pages

4prog Python

The document describes a Python program that stores and sorts student ID numbers using Bubble Sort, and allows searching for a specific ID using Linear and Binary Search methods. It provides a step-by-step implementation, including user input for student IDs and the search key. The document also includes a simple explanation of the sorting and searching algorithms used in the program.

Uploaded by

theyashverma18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

4prog Python

The document describes a Python program that stores and sorts student ID numbers using Bubble Sort, and allows searching for a specific ID using Linear and Binary Search methods. It provides a step-by-step implementation, including user input for student IDs and the search key. The document also includes a simple explanation of the sorting and searching algorithms used in the program.

Uploaded by

theyashverma18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4.

Sorting and Searching of Student IDs : Implement a Python program that stores
student ID numbers in a list and organizes them in sorted order using sorting algorithms such
as Bubble Sort or Selection Sort. The program should then allow the administrator to search
for a particular student ID using:
[Link] Search
2. Binary Search

Program

# Sorting and Searching of Student IDs

# Read student IDs


student_ids = []

n = int(input("Enter number of student IDs: "))

for i in range(n):
sid = int(input("Enter student ID: "))
student_ids.append(sid)

print("Original List:", student_ids)

# Bubble Sort
for i in range(len(student_ids)):
for j in range(0, len(student_ids) - i - 1):
if student_ids[j] > student_ids[j + 1]:
student_ids[j], student_ids[j + 1] = student_ids[j + 1], student_ids[j]

print("Sorted List:", student_ids)

# Search ID
key = int(input("Enter student ID to search: "))

# Linear Search
found = False
for i in range(len(student_ids)):
if student_ids[i] == key:
print("Linear Search: ID found at position", i + 1)
found = True
break

if not found:
print("Linear Search: ID not found")

# Binary Search
low = 0
high = len(student_ids) - 1
found = False

while low <= high:


mid = (low + high) // 2

if student_ids[mid] == key:
print("Binary Search: ID found at position", mid + 1)
found = True
break
elif student_ids[mid] < key:
low = mid + 1
else:
high = mid - 1

if not found:
print("Binary Search: ID not found")

Output
Enter number of student IDs: 5
Enter student ID: 104
Enter student ID: 101
Enter student ID: 103
Enter student ID: 105
Enter student ID: 102

Original List: [104, 101, 103, 105, 102]


Sorted List: [101, 102, 103, 104, 105]

Enter student ID to search: 103

Linear Search: ID found at position 3


Binary Search: ID found at position 3
Simple Explanation
1) Bubble Sort
Compares adjacent elements
Swaps them if they are in wrong order
Repeats until list is sorted

Example:

104 101 → swap


101 104
2) Linear Search
Checks each element one by one
Stops when the element is found
101 → 102 → 103 found
3) Binary Search
Works only on sorted list
Finds middle element
If key is smaller, search left side
If key is larger, search right side

This method is faster than linear search.

You might also like