How can I find one unsorted element in otherwise sorted array? (In C)
NickName:g3or3 Ask DateTime:2020-10-28T04:49:35

How can I find one unsorted element in otherwise sorted array? (In C)

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

More about “How can I find one unsorted element in otherwise sorted array? (In C)” related questions

How can I find one unsorted element in otherwise sorted array? (In C)

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, ...

Show Detail

Find all unsorted pairs in partially sorted array

I have to find (or atleast count) all pairs of (not necessarily adjacent) unsorted elements in a partially sorted array. If we assume the sorting to be ascending, the array [1 4 3 2 5] has the foll...

Show Detail

The kth smallest number in two arrays, one sorted the other unsorted

There is already an answer for two sorted arrays. However, in my question one of the arrays is unsorted. Suppose X[1..n] and Y[1..m] where n &lt; m. X is sorted and Y is unsorted. What is the effi...

Show Detail

Find Nth element from the two unsorted Array

Given two unsorted int arrays, find the kth element in the merged, sorted array. example: int[] a= [3 1 7] int[] b = [4 9] k: 3 return 4 (non-Zero based index) Please do not provide the straight f...

Show Detail

find the order of unsorted array

I have an array long[100] arr; and I want to "rank" the elements in the array. Meaning I want to know which one is the smallest, which one is the second smallest and so on. I'm aware that I can ma...

Show Detail

Best way to find position of element in unsorted array after it gets sorted

We have an unsorted array, need to print the position of each element assuming that it gets sorted. e.g.: we have an array. arr[] = {3, 2, 6, 1, 4} //index: 1 2 3 4 5 Index of elements 1-bas...

Show Detail

Is deletion of an element faster in unsorted array?

I read it somewhere that deletion of an element is faster in an unsorted array but I am not sure if that's correct. According to my understanding if we want to delete some particular element then i...

Show Detail

Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end

def InsSort(myList): UBound = len(myList) for i in range(1,UBound): NextItem = myList[i] position = i - 1 while position&gt;=0 and myList[position]&gt;NextItem: ...

Show Detail

Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end

def InsSort(myList): UBound = len(myList) for i in range(1,UBound): NextItem = myList[i] position = i - 1 while position&gt;=0 and myList[position]&gt;NextItem: ...

Show Detail

How to get index of smallest element from an unsorted array?

Hi I know how i can do it on sorted array, But i don't know how i can on unsorted in C? int smallest_element_index(int work_array[], int max_j) { int index = 0; int i; for(...

Show Detail