Skip to content

Commit 3ba1ec0

Browse files
committed
Algorithms: Sorting Problems
1 parent f6d52e9 commit 3ba1ec0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Selection Sort Problem
2+
#
3+
# Consider an Array a of size N
4+
# Iterate from 1 to N
5+
# In ith iteration select the ith minimum and swap it with a[i]
6+
# You are given an array a, size of the array N and an integer x.
7+
# Follow the above algorithm and print the state of the array after x iterations have been performed.
8+
#
9+
# Input Format
10+
#
11+
# The first line contains two integer N and x denoting the size of the array and the steps of the above algorithm to be
12+
# performed respectively. The next line contains N space separated integers denoting the elements of the array.
13+
#
14+
# Output Format
15+
#
16+
# Print N space separated integers denoting the state of the array after x steps
17+
#
18+
# Constraints
19+
#
20+
# 1≤N≤100
21+
# 1≤a[i]≤100
22+
# 1≤x≤N
23+
#
24+
# SAMPLE INPUT
25+
# 5 2
26+
# 1 2 3 4 5
27+
#
28+
# SAMPLE OUTPUT
29+
# 1 2 3 4 5
30+
31+
n, x = map(int, input().split())
32+
array = [int(i) for i in input().split()]
33+
for i in range(x): # Note x here, this is what yields the required result
34+
minimum = i
35+
for j in range(i + 1, n):
36+
if array[j] < array[minimum]:
37+
minimum = j
38+
if(minimum != i):
39+
array[i], array[minimum] = array[minimum], array[i]
40+
if(i == x):
41+
break
42+
43+
print(' '.join([str(i) for i in array]))

0 commit comments

Comments
 (0)