These are the 3 test cases where they are sorted except for one out of place element:
{-17, -5, -5, -2, 1, 17, 289, 17, 17, 395} | Out of place element index is 6.
{289, 17, 17, 17, 100, 250, 300, 1000, 5000} | Out of place element index is 0.
{-1, 23, 70, 500, 1000, 7000, 23520, 10} | Out of place element index is 7.
This is the code I have so far:
int outlier;
int size = sizeof(arr) / sizeof(int)
for (int i = 0; i < size - 1; i++) {
if (arr[i] > arr[i + 1])
outlier = i;
}
For the first test case the outlier is 6 which is correct. For the second test case the outlier is 0 which is also correct. However when I try it on the third array I get the outlier as 6 which is not the position of the element in the incorrect position. It should equal 7. The goal is to write a function to: a) Find the index of the element where it is unsorted and b) Sort the array.
Copyright Notice:Content Author:「g3or3」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64562703/how-can-i-find-one-unsorted-element-in-otherwise-sorted-array-in-c