0 points

Sorting an array without changing position of negative numbers


dashboard

Thu Dec 26 2019 15:35:22 GMT+0000 (UTC)

Posted 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_copy

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/

>> Browse more code snippets

more_vert