Problem:
In this challenge, don’t print every time you move an element. Instead, print the array every time an element is “inserted” into the array in (what is currently) its correct place. Since the array composed of just the first element is already “sorted”, begin printing from the second element and on.
Input Format
There will be two lines of input:
- s - the size of the array
- ar - the list of numbers that makes up the array
Output Format
On each line, output the entire array at every iteration
Constraints
1<=s<=1000
-10000<=x<= 10000 , x ∈ ar
Sample Input
6 1 4 3 5 6 2
Sample Output
1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6
n = input()
s = [int(x) for x in raw_input().split()]
for i in range(1,n):
key =s[i]
j=i-1
while key<s[j] and j>=0:
s[j+1]=s[j]
j-=1
s[j+1]=key
print " ".join(map(str,s))
本文探讨了排序算法中元素插入排序的过程,并优化了元素打印输出的策略,以提高效率和减少输出频率。
308

被折叠的 条评论
为什么被折叠?



