-5

Given an index of an element in the unsorted array, how do we find the index of the element in the sorted array? I will explain the problem with an example. 5,4,1,2,3 was my unsorted array. I am given an index. Lets take the 3rd index, the value at 3rd index is 2. Now I have to find the index of value 2 after the array has been sorted. Please write sample code. Thank you :)

codelyzer
  • 467
  • 1
  • 4
  • 17

1 Answers1

1

Assuming that all elements in the array are unique, scan through the array and count the number of items that would sort before the specified element. If there can be other elements that are "equal" to the specified element, then its position in a sorted array depends on the details of the sorting algorithm used (whether it is stable or not, and how it works if unstable).

user3553031
  • 5,990
  • 1
  • 20
  • 40