Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,689 questions

55,440 answers

573 users

How to print inversions pairs in array in Ruby

1 Answer

0 votes
def printInversionPair(arr) 
    size = arr.length
    
	if (size <= 1) 
		return
	end
    i = 0
	
	sorted = true	
	while (i < size) 
		j = i + 1
		while (j < size) 
			if (arr[i] > arr[j]) 
			    sorted = false
				print(arr[i] ,", ", arr[j] ,"\n")
				end
				j += 1
			end
			i += 1
		end
	if (sorted) 
	    print("sorted")
	end
end

	
arr1 = [1, 7, 2, 5, 4, 3, 9, 8]
printInversionPair(arr1)

arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
printInversionPair(arr2);




#
# run:
# 
# 7, 2
# 7, 5
# 7, 4
# 7, 3
# 5, 4
# 5, 3
# 4, 3
# 9, 8
# sorted
#

 



answered Nov 10, 2021 by avibootz

Related questions

1 answer 188 views
1 answer 200 views
200 views asked Nov 10, 2021 by avibootz
1 answer 213 views
1 answer 133 views
133 views asked Nov 10, 2021 by avibootz
1 answer 145 views
1 answer 139 views
1 answer 249 views
...