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++
// C++ program to get index of array element in
// sorted array
#include <bits/stdc++.h>
using namespace std;
// Method returns the position of arr[idx] after
// performing stable-sort on array
int getIndexInSortedArray(int arr[], int n, int idx)
{
/* Count of elements smaller than current
element plus the equal element occurring
before given index*/
int result = 0;
for (int i = 0; i < n; i++) {
// If element is smaller then increase
// the smaller count
if (arr[i] < arr[idx])
result++;
// If element is equal then increase count
// only if it occurs before
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
// Driver code to test above methods
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
// Java program to get index of array
// element in sorted array
class ArrayIndex {
// Method returns the position of
// arr[idx] after performing stable-sort
// on array
static int getIndexInSortedArray(int arr[],
int n, int idx)
{
/* Count of elements smaller than
current element plus the equal element
occurring before given index*/
int result = 0;
for (int i = 0; i < n; i++) {
// If element is smaller then
// increase the smaller count
if (arr[i] < arr[idx])
result++;
// If element is equal then increase
// count only if it occurs before
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
// Driver code to test above methods
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));
}
}
// This code is contributed by Raghav sharma
Python3
# Python program to get index of array element in
# sorted array
# Method returns the position of arr[idx] after
# performing stable-sort on array
def getIndexInSortedArray(arr, n, idx):
# Count of elements smaller than current
# element plus the equal element occurring
# before given index
result = 0
for i in range(n):
# If element is smaller then increase
# the smaller count
if (arr[i] < arr[idx]):
result += 1
# If element is equal then increase count
# only if it occurs before
if (arr[i] == arr[idx] and i < idx):
result += 1
return result;
# Driver code to test above methods
arr = [3, 4, 3, 5, 2, 3, 4, 3, 1, 5]
n = len(arr)
idxOfEle = 5
print (getIndexInSortedArray(arr, n, idxOfEle))
# Contributed by: Afzal Ansari
C#
// C# program to get index of array
// element in sorted array
using System;
class ArrayIndex {
// Method returns the position of
// arr[idx] after performing stable-sort
// on array
static int getIndexInSortedArray(int[] arr,
int n, int idx)
{
/* Count of elements smaller than
current element plus the equal element
occurring before given index*/
int result = 0;
for (int i = 0; i < n; i++) {
// If element is smaller then
// increase the smaller count
if (arr[i] < arr[idx])
result++;
// If element is equal then increase
// count only if it occurs before
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
// Driver code to test above methods
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));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to get index of
// array element in sorted array
// Method returns the position of
// arr[idx] after performing
// stable-sort on array
function getIndexInSortedArray( $arr, $n, $idx)
{
/* Count of elements smaller
than current element plus
the equal element occurring
before given index */
$result = 0;
for($i = 0; $i < $n; $i++)
{
// If element is smaller then
// increase the smaller count
if ($arr[$i] < $arr[$idx])
$result++;
// If element is equal then
// increase count only if
// it occurs before
if ($arr[$i] == $arr[$idx] and
$i < $idx)
$result++;
}
return $result;
}
// Driver Code
$arr = array(3, 4, 3, 5, 2, 3, 4, 3, 1, 5);
$n =count($arr);
$idxOfEle = 5;
echo getIndexInSortedArray($arr, $n,
$idxOfEle);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// JavaScript program to get index of array
// element in sorted array
// Method returns the position of
// arr[idx] after performing stable-sort
// on array
function getIndexInSortedArray(arr,
n, idx)
{
/* Count of elements smaller than
current element plus the equal element
occurring before given index*/
let result = 0;
for (let i = 0; i < n; i++) {
// If element is smaller then
// increase the smaller count
if (arr[i] < arr[idx])
result++;
// If element is equal then increase
// count only if it occurs before
if (arr[i] == arr[idx] && i < idx)
result++;
}
return result;
}
// Driver Code
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));
// This code is contributed by code_hunt.
</script>
Time Complexity: O(n) where n is the size of the array.
Auxiliary Space: O(1)
Similar Reads
Stable Selection Sort A sorting algorithm is said to be stable if two objects with equal or same keys appear in the same order in sorted output as they appear in the input array to be sorted.Any comparison based sorting algorithm which is not stable by nature can be modified to be stable by changing the key comparison op
6 min read
Unstable Sort meaning in DSA Unstable sorting algorithm can be defined as sorting algorithm in which the order of objects with the same values in the sorted array are not guaranteed to be the same as in the input array. Properties of Unstable Sorting Algorithm:They do not require the overhead of keeping the order of equal eleme
2 min read
Insertion Sort by Swapping Elements Insertion Sort is suitable for arrays of small size. It also achieves the best-case complexity of O(n) if the arrays are already sorted. We have discussed both Iterative Insertion Sort and Recursive Insertion Sort. In this article, slightly different implementations for both iterative and recursive
11 min read
Insertion sort to sort even and odd positioned elements in different orders We are given an array. We need to sort the even positioned elements in the ascending order and the odd positioned elements in the descending order. We must apply insertion sort to sort them.Examples: Input : a[] = {7, 10, 11, 3, 6, 9, 2, 13, 0} Output : 11 3 7 9 6 10 2 13 0 Even positioned elements
7 min read
Sorting all array elements except one Given an array, a positive integer, sort the array in ascending order such that the element at index K in the unsorted array stays unmoved and all other elements are sorted. Examples: Input : arr[] = {10, 4, 11, 7, 6, 20} k = 2; Output : arr[] = {4, 6, 11, 7, 10, 20} Input : arr[] = {30, 20, 10} k =
6 min read
Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
8 min read