Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the array can be sorted with a single swap of 2 elements

I am trying to write a function that will check if the array can be sorted with a single swap of the values in the array.

For example: array(1,3,5,3,7) must return true, but array(1,3,5,3,4) must return false.

I tried the following code below, but I'm stuck with it:

$noOfIterations = 0;

for($x = 0; $x < count($a)-2; $x++) {

    if($a[$x] > $a[$x+1]) {
        $noOfIterations ++;
    }

}

return $noOfIterations >1; 

// The below solution helped as well.

//$arr = [1, 3, 5, 3, 7];  //[1, 3, 5, 3, 4]
$arr = [1, 3, 5, 3, 4];
$sortedArr = $arr;
sort($sortedArr);
print_r(array_intersect_assoc($arr,$sortedArr));
like image 801
Abbasi Avatar asked Apr 23 '15 00:04

Abbasi


People also ask

How do you sort an array with two elements?

Step 1 : Here we can take two pointers type0 (for element 0) starting from beginning (index = 0) and type1 (for element 1) starting from end index. Step 2: We intend to put 1 to the right side of the array. Once we have done this then 0 will definitely towards left side of array to achieve this we do following.

How do you sort an array with swapping?

Given an array of length n + 1, containing elements 1 through n and a space, Requires the use of a given swap (index i, index j) function to sort the array, You can only swap the gap and a number, in the end, put the gap at the end.

How do you check if an array is already sorted?

The basic idea for the recursive approach: 1: If size of array is zero or one, return true. 2: Check last two elements of array, if they are sorted, perform a recursive call with n-1 else, return false. If all the elements will be found sorted, n will eventually fall to one, satisfying Step 1.

How do you swap two numbers in an array?

The built-in swap() function can swap two values in an array . template <class T> void swap (T& a, T& b); The swap() function takes two arguments of any data type, i.e., the two values that need to be swapped.


2 Answers

This should work for you:

(Here I first make a copy of the original array to then sort() it. After this I loop through both arrays with array_map() and look how many position has changed. With array_filter() I sort the elements out where no position has changed. Then you can simply check if 2 or more position has changed and print either FALSE or TRUE)

<?php

    $arr = [1, 3, 5, 3, 7];  //[1, 3, 5, 3, 4]
    $sortedArr = $arr;
    sort($sortedArr);

    $filtered = array_filter(
        array_map(function($v1, $v2){
            return ($v1 == $v2 ?FALSE:TRUE);
        }, $arr, $sortedArr)
    );

    var_dump(count($filtered) > 2 ? FALSE : TRUE);

?>

output:

TRUE  //FALSE
like image 128
Rizier123 Avatar answered Nov 14 '22 22:11

Rizier123


Execute the sort, then compare the original array with the sorted array using array_intersect_assoc().... if the difference is more than two elements, then the answer is 'no'

like image 38
Mark Baker Avatar answered Nov 14 '22 23:11

Mark Baker