📌  相关文章
📜  根据给定值的绝对差对数组进行排序(“使用恒定的额外空间”)

📅  最后修改于: 2021-04-27 17:34:11             🧑  作者: Mango

给定n个不同元素的数组和数量x,根据与x的绝对差排列数组元素,即,具有最小差的元素排在最前面,依此类推,使用恒定的额外空间。
注意:如果两个或多个元素的距离相等,请按照与给定数组相同的顺序排列它们。

例子:

Input  : arr[] = {10, 5, 3, 9, 2}
             x = 7
Output : arr[] = {5, 9, 10, 3, 2}
Explanation : 
7 - 10 = 3(abs)
7 - 5 = 2
7 - 3 = 4 
7 - 9 = 2(abs)
7 - 2 = 5
So according to the difference with X, 
elements are arranged as 5, 9, 10, 3, 2.

Input  : arr[] = {1, 2, 3, 4, 5}
             x = 6
Output : arr[] = {5, 4, 3, 2, 1}

上面的问题已经在这里的上一篇文章中得到了解释。这需要O(n log n)时间和O(n)额外空间。下面的解决方案虽然具有相对较差的时间复杂度,即O(n ^ 2),但无需使用任何额外的空间或内存即可完成工作。

该解决方案基于插入排序。对于每个i(1 <= i

C++
// C++ program to sort an array based on absolute
// difference with a given value x.
#include 
using namespace std;
  
void arrange(int arr[], int n, int x)
{
    // Below lines are similar to insertion sort
    for (int i = 1; i < n; i++) {
        int diff = abs(arr[i] - x);
  
        // Insert arr[i] at correct place
        int j = i - 1;
        if (abs(arr[j] - x) > diff) {
            int temp = arr[i];
            while (abs(arr[j] - x) > diff && j >= 0) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = temp;
        }
    }
}
  
// Function to print the array
void print(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Main Function
int main()
{
    int arr[] = { 10, 5, 3, 9, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;
  
    arrange(arr, n, x);
    print(arr, n);
  
    return 0;
}


Java
// Java program to sort an array based on absolute
// difference with a given value x.
class GFG {
  
static void arrange(int arr[], int n, int x)
{
    // Below lines are similar to insertion sort
    for (int i = 1; i < n; i++) {
        int diff = Math.abs(arr[i] - x);
  
        // Insert arr[i] at correct place
        int j = i - 1;
        if (Math.abs(arr[j] - x) > diff)
        {
            int temp = arr[i];
            while (j >= 0 && Math.abs(arr[j] - x) > diff) 
            {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = temp;
        }
    }
}
  
// Function to print the array
static void print(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    System.out.print(arr[i] + " ");
}
  
// Driver code
public static void main(String[] args) {
    int arr[] = { 10, 5, 3, 9, 2 };
    int n = arr.length;
    int x = 7;
  
    arrange(arr, n, x);
    print(arr, n);
    }
}
  
// This code is contributed by PrinciRaj1992


Python 3
# Python 3 program to sort an array 
# based on absolute difference with
# a given value x.
def arrange(arr, n, x):
  
    # Below lines are similar to 
    # insertion sort
    for i in range(1, n) :
        diff = abs(arr[i] - x)
  
        # Insert arr[i] at correct place
        j = i - 1
        if (abs(arr[j] - x) > diff) :
            temp = arr[i]
            while (abs(arr[j] - x) > 
                       diff and j >= 0) :
                arr[j + 1] = arr[j]
                j -= 1
              
            arr[j + 1] = temp
  
# Function to print the array
def print_1(arr, n):
  
    for i in range(n):
        print(arr[i], end = " ")
  
# Driver Code
if __name__ == "__main__":
      
    arr = [ 10, 5, 3, 9, 2 ]
    n = len(arr)
    x = 7
  
    arrange(arr, n, x)
    print_1(arr, n)
  
# This code is contributed by ita_c


C#
// C# program to sort an array based on absolute
// difference with a given value x.
using System;
  
class GFG 
{
  
    static void arrange(int []arr, int n, int x)
    {
          
        // Below lines are similar to insertion sort
        for (int i = 1; i < n; i++)
        {
            int diff = Math.Abs(arr[i] - x);
  
            // Insert arr[i] at correct place
            int j = i - 1;
            if (Math.Abs(arr[j] - x) > diff)
            {
                int temp = arr[i];
                while (j >= 0 && Math.Abs(arr[j] - x) > diff) 
                {
                    arr[j + 1] = arr[j];
                    j--;
                }
                arr[j + 1] = temp;
            }
        }
    }
  
    // Function to print the array
    static void print(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
  
    // Driver code
    public static void Main() 
    {
        int []arr = { 10, 5, 3, 9, 2 };
        int n = arr.Length;
        int x = 7;
  
        arrange(arr, n, x);
        print(arr, n);
    }
}
  
// This code is contributed by 29AjayKumar


PHP
 $diff)
        {
            $temp = $arr[$i];
            while (abs($arr[$j] - $x) > 
                       $diff && $j >= 0) 
            {
                $arr[$j + 1] = $arr[$j];
                $j--;
            }
            $arr[$j + 1] = $temp;
        }
    }
    return $arr;
}
  
// Function to print the array
function print_arr($arr, $n)
{
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i] . " ";
}
  
// Driver Code
$arr = array(10, 5, 3, 9, 2);
$n = sizeof($arr);
$x = 7;
  
$arr1 = arrange($arr, $n, $x);
print_arr($arr1, $n);
  
// This code is contributed 
// by Akanksha Rai
?>


输出:

5 9 10 3 2

时间复杂度: O(n ^ 2),其中n是数组的大小。
辅助空间: O(1)