Position of an element after stable sort | Python program to get index of array element in sorted array Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver code to test above methods
def getIndexInSortedArray ( arr , n , idx ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ idx ] ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT if ( arr [ i ] == arr [ idx ] and i < idx ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT arr = [ 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE idxOfEle = 5 NEW_LINE print getIndexInSortedArray ( arr , n , idxOfEle ) NEW_LINE