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.
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:
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Stable Selection Sort
- Stable sort for descending order
- Sort an array without changing position of negative numbers
- Find the position of the last removed element from the array
- Number of shuffles required for each element to return to its initial position
- Sort the array according to their cubes of each element
- Sort integers in array according to their distance from the element K
- Python | Sort a list according to the second element in sublist
- Sort an array with swapping only with a special element is allowed
- Stable QuickSort
- Count of smaller elements on right side of each element in an Array using Merge sort
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Add elements in start to sort the array | Variation of Stalin Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Insertion sort to sort even and odd positioned elements in different orders
- Program to sort an array of strings using Selection Sort
- Largest element smaller than current element on left for every element in Array
- Odd Even Transposition Sort / Brick Sort using pthreads
- Java Program for Odd-Even Sort / Brick Sort
Improved By : vt_m