Given an array of size N having unique elements, implement Selection Sort.Note: Implement Selection Sort by selecting smallest element at every step.Input FormatThe first line of input contains an integer N - the size of an array. The second line contains the elements of the array.Output FormatFor each iteration of Selection Sort, print the array elements.Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput65 8 10 15 3 6Output3 8 10 15 5 63 5 10 15 8 63 5 6 15 8 103 5 6 8 15 10
Question
Given an array of size N having unique elements, implement Selection Sort.Note: Implement Selection Sort by selecting smallest element at every step.Input FormatThe first line of input contains an integer N - the size of an array. The second line contains the elements of the array.Output FormatFor each iteration of Selection Sort, print the array elements.Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput65 8 10 15 3 6Output3 8 10 15 5 63 5 10 15 8 63 5 6 15 8 103 5 6 8 15 10
Solution
Sure, here is a step-by-step guide on how to implement Selection Sort in Python:
- First, we need to read the input. The first line of input is the size of the array, and the second line contains the elements of the array. We can use the
input()function in Python to read the input.
N = int(input())
arr = list(map(int, input().split()))
- Now, we will implement the Selection Sort algorithm. The idea behind this algorithm is to divide the array into two parts: sorted and unsorted. Initially, the sorted part is empty and the unsorted part contains all elements. In each step, we select the smallest element from the unsorted part and swap it with the leftmost unsorted element, moving the boundary between the sorted and unsorted parts one element to the right.
for i in range(N):
min_index = i
for j in range(i+1, N):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
print(' '.join(map(str, arr)))
In the above code, the outer loop runs from the start of the array to the end. The inner loop finds the minimum element in the unsorted part of the array. Once we find the minimum element, we swap it with the first unsorted element. After each iteration, we print the array.
This is how you can implement Selection Sort in Python. The time complexity of this algorithm is O(N^2), where N is the size of the array.
Similar Questions
Given an array of size N, implement Bubble Sort.Input FormatThe first line of input contains an integer N - the size of an array. The second line contains the elements of the array.Output FormatFor each iteration of Bubble Sort, print the array elements.Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput65 8 10 15 3 6Output5 8 10 3 6 155 8 3 6 10 155 3 6 8 10 153 5 6 8 10 153 5 6 8 10 15
Given an array of size N, implement Merge sort.Input FormatThe first line of input contains an integer N - the size of an array. The second line contains the elements of the array.Output FormatFor each merge call of Merge Sort, print the array elements.Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput65 1 3 15 10 4Output1 5 3 15 10 4 1 3 5 15 10 4 1 3 5 10 15 4 1 3 5 4 10 15 1 3 4 5 10 15
Write a program to implement the Selection Sort algorithm to sort an array of integers in ascending order.Input:91 3 5 7 9 2 4 6 8Output:1 2 3 4 5 6 7 8 9Explanation:Initially, the minimum element in the array is 1. It is swapped with the first element, resulting in the array [1, 3, 8, 2, 5, 4]. Next, the minimum element in the remaining unsorted part is 2. It is swapped with the second element, resulting in [1, 2, 8, 3, 5, 4]. This process continues, and the final sorted array is [1, 2, 3, 4, 5, 8].
You are given an array of 0's and 1's. Sort the array in ascending order and print it.Note: Solve using two-pointer technique.Input FormatFirst line of input contains T - the number of test cases. Its followed by 2T lines, the first line contains N - the size of the array.The second line contains the elements of the array.Constraints1 <= T <= 10001 <= N <= 10000 <= A[i] <= 1Output FormatFor each test case, sort the array in ascending order and print it on a new line.Sample Input 0250 1 1 0 161 1 1 1 1 0Sample Output 00 0 1 1 10 1 1 1 1 1Explanation 0
You work in the examination department of a school, and you've been given a task to sort the test scores of students in ascending order. The scores are stored in a list where each element represents the score of a student.Input FormatThe first line contains the integer N, the size of the array. The next line contains N space-separated integers.Constraints• 1<=N<=1000 • -1000<=a[i]<=1000Output FormatPrint the array as a row of space-separated integers in each iteration.Sample Input 01010 9 8 7 6 5 4 3 2 1Sample Output 05 9 8 7 6 10 4 3 2 15 4 8 7 6 10 9 3 2 15 4 3 7 6 10 9 8 2 15 4 3 2 6 10 9 8 7 15 4 3 2 1 10 9 8 7 63 4 5 2 1 10 9 8 7 63 2 5 4 1 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 8 9 10 7 61 2 3 4 5 8 7 10 9 61 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.