Ad Code

Responsive Advertisement

Python Program / Project - 16

Question 16: Write a Python program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using.
a) Selection Sort
b) Bubble sort and display top five scores.
Download hole Program / Project code, by clicking following link:
In if-else construct which part will be executed if condition is ture in Java ?
In an if-else construct in Java, the code within the if block will be executed if the condition specified in the if statement is true. If the if condition is false, then the code within the else block (if an else block is provided) will be executed.
Here's the general syntax of an if-else construct:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

So, if the condition within the parentheses after if is true, the code in the if block will be executed. If the condition is false, the code in the else block (if present) will be executed.

Here's an example:
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are not an adult.");
}


In this example, if the age is greater than or equal to 18, the message "You are an adult" will be printed because the condition is true. If the age is less than 18, the message "You are not an adult" will be printed because the condition is false, and the code in the else block will be executed.
Programming Code:
Following code write in: DS_P16.py
# Python Program / Project

def selectionsort(arr, n):
    for i in range(n):
        min = i
        for j in range(i+1, n):
            if arr[j] < arr[min]:
                min = j
        temp = arr[i]
        arr[i] = arr[min]
        arr[min] = temp
    print(arr)
    print("Top five scores is: ")
    for i in range(len(arr)-1, 1, -1):
        print(arr[i])

def bubblesort(arr, n):
    i = 0
    for i in range(n-1):
        for j in range(0, n-i-1):
            temp = arr[j]
            arr[j] = arr[j+1]
            arr[j+1] = temp

    print(arr)
    print("Top five scores is: ")
    for i in range(len(arr)-1, 1, -1):
        print(arr[i])

n = int(input("Number of Students in First Year: "))
arr = []

i = 0
for i in range(n):
    item = float(input("Enter percentage marks: "))
    arr.append(item)

print("You have entered percentage of students is: ")
print(arr)

while(True):
    print("********** Menu **********")
    print("1. Selection Sort.")
    print("2. Bubble Sort.")
    print("3. Exit.")

    choice = int(input("Enter your choice: "))
    if choice == 1:
        print("The sorted list using Selection Sort is: ")
        selectionsort(arr, n)
    
    elif choice == 2:
        print("The sorted list using Bubble Sort is:")
        bubblesort(arr, n)
    
    else:
        print("You have Exit.")
        exit()

# Thanks For Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement