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

CSC 213 assignment

Hjjlkggv by jfv

Uploaded by

georgeorsaris7
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)
7 views3 pages

CSC 213 assignment

Hjjlkggv by jfv

Uploaded by

georgeorsaris7
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/ 3

USING SELECTION SORT

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]

# Set of 25 numbers (you can modify this according to your requirements)


numbers = [7, 13, 2, 15, 10, 6, 8, 1, 9, 12, 4, 5, 20, 17, 3, 11, 25, 19, 14, 23, 22, 18, 21, 16,
24]

# Print the original array


print("Original array:")
print(numbers)

# Sort the array using selection sort


selection_sort(numbers)

# Print the sorted array


print("Sorted array:")
print(numbers)

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.

USING EXCHANGE SORT

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]

# Print original array


print("Original array:", numbers)

# Sort the array using exchange sort


exchange_sort(numbers)

# Print sorted array


print("Sorted array:", numbers)

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.

USING INSERTION SORT

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]

# Print original array


print("Original array:", numbers)

# Sort the array using insertion sort


insertion_sort(numbers)

# Print sorted array


print("Sorted array:", numbers)

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.

You might also like