Sorting an array without changing position of negative numbers
Thu Dec 26 2019 15:35:22 GMT+0000 (Coordinated Universal Time)
Saved by
@divisionjava
#python
#interesting
#arrays
#sorting
#interviewquestions
# Python3 implementation of the approach
# Function to sort the array such that
# negative values do not get affected
def sortArray(a, n):
# Store all non-negative values
ans=[]
for i in range(n):
if (a[i] >= 0):
ans.append(a[i])
# Sort non-negative values
ans = sorted(ans)
j = 0
for i in range(n):
# If current element is non-negative then
# update it such that all the
# non-negative values are sorted
if (a[i] >= 0):
a[i] = ans[j]
j += 1
# Print the sorted array
for i in range(n):
print(a[i],end = " ")
# Driver code
arr = [2, -6, -3, 8, 4, 1]
n = len(arr)
sortArray(arr, n)
content_copyCOPY
for example:
Input: arr[] = {2, -6, -3, 8, 4, 1}
Output: 1 -6 -3 2 4 8
https://www.geeksforgeeks.org/sort-an-array-without-changing-position-of-negative-numbers/
Comments
Anonymous - Thu Mar 04 2021 10:01:32 GMT+0000 (Coordinated Universal Time)
a