[go: up one dir, main page]

0% found this document useful (0 votes)
37 views7 pages

Pradyumn - Kabra Computer Project

Uploaded by

s9605519
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)
37 views7 pages

Pradyumn - Kabra Computer Project

Uploaded by

s9605519
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
You are on page 1/ 7

COMPUTER PROJECT

NAME: PRADYUMN KABRA

CLASS: XI

SECTION: BS1

ROLL NO: 21

SUBJECT: COMPUTER SCIENCE


QUESTION-
Write a menu driven program to store N integers one by one in a list
and implement the following as per user’s choice:
1. Sorting

1. Bubble Sort

2. Selection Sort

3.Insertion Sort

2. Searching

1. Linear search in the unsorted list

2. Binary search in the list after sorting

PROGRAM-
arr = []

n = int(input("Enter the number of integers: "))

for i in range(n):

arr.append(int(input(f"Enter integer {i+1}: ")))

print(f"Entered list: {arr}")

while True:

print("\nMenu:")

print("1. Sorting")

print("2. Searching")

print("3. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:

print("\nSorting options:")

print("1. Bubble Sort")


print("2. Selection Sort")

print("3. Insertion Sort")

sort_choice = int(input("Enter your choice: "))

order = input("Enter sorting order (a for ascending, d for descending): ")

if sort_choice == 1:

n = len(arr)

for i in range(n):

for j in range(0, n-i-1):

if order == 'a':

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

elif order == 'd':

if arr[j] < arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

print(f"\nSorted list: {arr}")

elif sort_choice == 2:

n = len(arr)

for i in range(n):

min_idx = i

for j in range(i+1, n):

if order == 'a':

if arr[min_idx] > arr[j]:

min_idx = j

elif order == 'd':

if arr[min_idx] < arr[j]:

min_idx = j

arr[i], arr[min_idx] = arr[min_idx], arr[i]


print(f"\nSorted list: {arr}")

elif sort_choice == 3:

n = len(arr)

for i in range(1, n):

key = arr[i]

j = i-1

if order == 'a':

while j >= 0 and key < arr[j]:

arr[j+1] = arr[j]

j -= 1

elif order == 'd':

while j >= 0 and key > arr[j]:

arr[j+1] = arr[j]

j -= 1

arr[j+1] = key

print(f"\nSorted list: {arr}")

else:

print("\nInvalid choice!")

elif choice == 2:

print("\nSearching options:")

print("1. Linear search in the unsorted list")

print("2. Binary search in the list after sorting")

search_choice = int(input("Enter your choice: "))

if search_choice == 1:

x = int(input("Enter the integer to search: "))

for i in range(n):

if arr[i] == x:
print(f"\n{x} found at index {i}")

break

else:

print(f"\n{x} not found in the list")

elif search_choice == 2:

order = input("Enter sorting order (a for ascending, d for descending): ")

if order == 'a':

arr.sort()

elif order == 'd':

arr.sort(reverse=True)

else:

print("\nInvalid choice!")

continue

x = int(input("Enter the integer to search: "))

l, r = 0, n-1

while l <= r:

mid = (l+r) // 2

if arr[mid] == x:

print(f"\n{x} found at index {mid}")

break

elif arr[mid] < x:

l = mid + 1

else:

r = mid - 1

else:

print(f"\n{x} not found in the list")

else:
print("\nInvalid choice!")

elif choice == 3:

print("\nExiting...")

break

else:

print("\nInvalid choice!")

You might also like