CSC 213 assignment
CSC 213 assignment
def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
In this program, the selection_sort function implements the selection sort algorithm. It
iterates over the array and finds the minimum element in the remaining unsorted part of the
array. It then swaps that minimum element with the current element. This process is
repeated until the entire array is sorted in ascending order.
You can modify the numbers list to store your own set of 25 numbers, and the program will
sort them using the selection sort algorithm.
def exchange_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(i + 1, n):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
# Test data
numbers = [9, 4, 7, 2, 1, 8, 5, 3, 6, 10, 15, 12, 11, 14, 13, 18, 17, 20, 19, 16, 21, 25, 23, 24,
22]
This program defines a function called exchange_sort that implements the exchange sort
algorithm. It takes an array arr as an argument and sorts it in ascending order. The main part
of the program defines a test data array of 25 numbers and then calls the exchange_sort
function to sort the array. Finally, it prints the original and sorted arrays.
Note: In this example, the test data array is provided directly in the program. If you want to
input the numbers from the user, you can modify the program to prompt for input and
populate the array dynamically.
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Test data
numbers = [9, 4, 7, 2, 1, 8, 5, 3, 6, 10, 15, 12, 11, 14, 13, 18, 17, 20, 19, 16, 21, 25, 23, 24,
22]
This program defines a function called insertion_sort that implements the insertion sort
algorithm. It takes an array arr as an argument and sorts it in ascending order. The main part
of the program defines a test data array of 25 numbers and then calls the insertion_sort
function to sort the array. Finally, it prints the original and sorted arrays.
Note: In this example, the test data array is provided directly in the program. If you want to
input the numbers from the user, you can modify the program to prompt for input and
populate the array dynamically.