Position of an element after stable sort
Last Updated :
19 Sep, 2023
Given an array of integers which may contain duplicate elements, an element of this array is given to us, we need to tell the final position of this element in the array, if a stable sort algorithm is applied.
Examples :
Input : arr[] = [3, 4, 3, 5, 2, 3, 4, 3, 1, 5], index = 5
Output : 4
Element initial index – 5 (third 3)
After sorting array by stable sorting algorithm, we get
array as shown below
[1(8), 2(4), 3(0), 3(2), 3(5), 3(7), 4(1), 4(6), 5(3), 5(9)]
with their initial indices shown in parentheses next to them,
Element's index after sorting = 4
One easy way to solve this problem is to use any stable sorting algorithm like Insertion Sort, Merge Sort etc and then get the new index of given element but we can solve this problem without sorting the array.
As position of an element in a sorted array is decided by only those elements which are smaller than given element. We count all array elements smaller than given element and for those elements which are equal to given element, elements occurring before given elements’ index will be included in count of smaller elements this will insure the stability of the result’s index.
Simple code to implement above approach is implemented below:
C++
#include <bits/stdc++.h>
using namespace std;
int getIndexInSortedArray( int arr[], int n, int idx)
{
int result = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] < arr[idx])
result++;
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
int main()
{
int arr[] = { 3, 4, 3, 5, 2, 3, 4, 3, 1, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int idxOfEle = 5;
cout << getIndexInSortedArray(arr, n, idxOfEle);
return 0;
}
|
Java
class ArrayIndex {
static int getIndexInSortedArray( int arr[],
int n, int idx)
{
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] < arr[idx])
result++;
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 };
int n = arr.length;
int idxOfEle = 5 ;
System.out.println(getIndexInSortedArray(arr,
n, idxOfEle));
}
}
|
Python3
def getIndexInSortedArray(arr, n, idx):
result = 0
for i in range (n):
if (arr[i] < arr[idx]):
result + = 1
if (arr[i] = = arr[idx] and i < idx):
result + = 1
return result;
arr = [ 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 ]
n = len (arr)
idxOfEle = 5
print (getIndexInSortedArray(arr, n, idxOfEle))
|
C#
using System;
class ArrayIndex {
static int getIndexInSortedArray( int [] arr,
int n, int idx)
{
int result = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] < arr[idx])
result++;
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
public static void Main()
{
int [] arr = { 3, 4, 3, 5, 2, 3, 4, 3, 1, 5 };
int n = arr.Length;
int idxOfEle = 5;
Console.WriteLine(getIndexInSortedArray(arr, n,
idxOfEle));
}
}
|
PHP
<?php
function getIndexInSortedArray( $arr , $n , $idx )
{
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] < $arr [ $idx ])
$result ++;
if ( $arr [ $i ] == $arr [ $idx ] and
$i < $idx )
$result ++;
}
return $result ;
}
$arr = array (3, 4, 3, 5, 2, 3, 4, 3, 1, 5);
$n = count ( $arr );
$idxOfEle = 5;
echo getIndexInSortedArray( $arr , $n ,
$idxOfEle );
?>
|
Javascript
<script>
function getIndexInSortedArray(arr,
n, idx)
{
let result = 0;
for (let i = 0; i < n; i++) {
if (arr[i] < arr[idx])
result++;
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
let arr = [ 3, 4, 3, 5, 2, 3, 4, 3, 1, 5 ];
let n = arr.length;
let idxOfEle = 5;
document.write(getIndexInSortedArray(arr,
n, idxOfEle));
</script>
|
Time Complexity: O(n) where n is the size of the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...