Find all unsorted pairs in partially sorted array
NickName:Aditya Pratap Singh Ask DateTime:2021-07-21T23:29:53

Find all unsorted pairs in partially sorted array

I have to find (or atleast count) all pairs of (not necessarily adjacent) unsorted elements in a partially sorted array.

If we assume the sorting to be ascending, the array [1 4 3 2 5] has the following unsorted pairs: (4, 3), (3, 2) and (4, 2).

I'm thinking of an algorithm that works along the lines of insertion sort, as insertion sort tends to compare every new element with all elements which are misplaced with respect to the new element.

Edit: While posting the question, I didn't realise that finding the pairs would have a higher time complexity than counting them. Is there a better possible algorithm that just counts how many such pairs exist?

Copyright Notice:Content Author:「Aditya Pratap Singh」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68472355/find-all-unsorted-pairs-in-partially-sorted-array

Answers
Thomas 2021-07-21T15:53:46

It depends a little bit on what you mean exactly by "partially sorted" - One could argue that every array is partially sorted to some degree.\nSince this algorithm has worst-case complexity O(n^2) anyway (consider the input sorted in descending order), you might as well go down the straight-forward route:\nret = []\n\nfor i in range(len(array)):\n for j in range(i, len(array)):\n if array[i] > array[j]:\n ret.append((array[i], array[j]))\n\nreturn ret\n\nThis works very well for random arrays.\nHowever, I suppose what you have in mind is more something that there are larger stretches inside the array where the numbers are sorted but that that's not the case for the array as a whole.\nIn that case, you can save a bit of time over the naive approach above by first identifying those stretches - this can be done in a linear pass. Once you have them, you only have to compare these stretches with each other, and you can use binary search for that (since the stretches are in sort order).\nHere's a Python implementation of what I have in mind:\n# find all sorted stretches\nstretches = []\nbegin = 0\nfor i in range(1, len(array)):\n if array[i-1] > array[i]:\n stretches.append(array[begin:i])\n begin = i\nif i+1 > begin:\n stretches.append(array[begin:])\n\n# compare stretches\nret = []\nfor i in range(len(stretches)):\n stretchi = stretches[i]\n stretchi_rev = None\n \n for j in range(i+1, len(stretches)):\n stretchj = stretches[j]\n\n if stretchi[-1] > stretchj[0]:\n if stretchi_rev is None:\n stretchi_rev = list(reversed(stretchi))\n\n hi = len(stretchj)\n for x in stretchi_rev:\n i = bisect.bisect_left(stretchj, x, 0, hi)\n if i == 0:\n break\n else:\n for y in stretchj[:i]:\n ret.append((x, y))\n hi = i\n \nreturn ret\n\nFor random arrays, this will be slower than the first approach. But if the array is big, and the amount of partially sorted portions is high enough, this algorithm will at some point starting to beat the brute-force search.",


More about “Find all unsorted pairs in partially sorted array” related questions

Find all unsorted pairs in partially sorted array

I have to find (or atleast count) all pairs of (not necessarily adjacent) unsorted elements in a partially sorted array. If we assume the sorting to be ascending, the array [1 4 3 2 5] has the foll...

Show Detail

Sorting a partially sorted array in O(n)

Hey so I'm just really stuck on this question. I need to devise an algorithm (no need for code) that sorts a certain partially sorted array into a fully sorted array. The array has N real numbers ...

Show Detail

Find all pairs in an unsorted array whose sum is divisible by 4

Find all pairs in an unsorted array whose sum is divisble by 4. Kindly suggest an algorithm better than O(n*n) e.g. [1,2,4,0,20,22] k=4 [0,4] [0,20] [20,4] [2,22]

Show Detail

Counting inversions to see if an array is partially sorted

The definition of a sorted array below is from Algorithm book by Robert Sedgewick and Kevin Wayne. An array is partially sorted if the number of inversions is <= cN Shouldn't there be a lim...

Show Detail

How do I check if a list is partially sorted? How to define a threshold for partially sorted lists?

I have hundreds of lists that look like that: list_1 = [10,20,30,40,70,90,230,450] # sorted list example list_2 = [10,20,40,30,70,230,90,450] # partially sorted list example list_a = [20,450,90,30,...

Show Detail

Find all pairs in an unsorted array

I have come across a programming question in find I have to find all the pair in a given unsorted array such that |i - j| <= K and |A[i] - A[j]| <= x For example: A = {5,4,8,3} and x =...

Show Detail

Finding pairs in an sorted array with big values

I'm currently stuck on a problem with finding pairs in an sorted array with big values. Example: if i get the array sortedarr=[1, 2, 2, 2, 2, 2, 2, 3] as input. It should return pairs = 15. I have

Show Detail

DIfference between sorted and unsorted array time complexity

So, I'm very new to programming and computer science, and have been trying to understand the concept of time complexity while solving my code. However, when it comes to sorting arrays, I always get

Show Detail

Print / Count all distinct pairs with equal sum in an unsorted array

Given an unsorted array, for example : [6,4,12,10,22,54,32,42,21,11] We need to find pairs with same sum. Output: (12,4) and (10,6) , SUM = 16 ; (42,22) and (54,10) , SUM = 64 etc ... How do we

Show Detail

Finding Duplicate numbers in a sorted Array

I want to know if there is a any way to find out how many pairs is in a sorted array The easiest way is using two for loop for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j &...

Show Detail