📌  相关文章
📜  只能通过交换特殊元素来对数组进行排序

📅  最后修改于: 2021-04-24 16:24:50             🧑  作者: Mango

给定长度为n + 1的数组,其中包含元素1到n和一个空格,需要使用给定的swap(索引i,索引j)函数对数组进行排序,您只能在中间交换空格和数字。最后,把差距放在最后。
数组中的数字999将作为间隙或间隔。
例子:

Input  : arr = {1, 5, 4, 999, 3, 2}
Output : arr = {1, 2, 3, 4, 5, 999}
We need to sort only by moving 

Input  : arr = {1, 5, 4, 3, 2, 8, 7, 999, 6}
Output : arr = {1, 2, 3, 4, 5, 6, 7, 8, 999}

我们遵循一种递归方法来解决此问题。因为我们只能用空格交换数字。首先我们找到空间的索引。如果索引是数组的开始,则通过与右边的每个数字交换将空间移到倒数第二个索引。
如果space既不是数组的开始,也不是array和element之前的最后一个元素,而不是空格旁边的元素,请执行以下操作。

否则,将对index旁边的元素进行排序,并将其与上一个元素交换。再次调用sort函数,将数组的大小减小1,将空间索引减小1,因为我们每次都会得到一个排序的元素。

C++
// CPP program to sort an array by moving one
// space around.
#include 
using namespace std;
  
// n is total number of elements.
// index is index of 999 or space.
// k is number of elements yet to be sorted.
void sortRec(int arr[], int index, int k, int n)
{
    // print the sorted array when loop reaches
    // the base case
    if (k == 0) {
        for (int i = 1; i < n; i++)
            cout << arr[i] << " ";
        cout << 999;
        return;
    }
  
    // else if k>0 and space is at 0th index swap
    // each number with space and store index at
    // second last index
    else if (k > 0 && index == 0) {
        index = n - 2;
        for (int i = 1; i <= index; i++) {
            arr[i - 1] = arr[i];
        }
        arr[index] = 999;
    }
  
    // if space is neither start of array nor last
    // element of array and element before it greater
    // than/ the element next to space
    if (index - 1 >= 0 && index + 1 < n && 
       arr[index - 1] > arr[index + 1]) {
  
        // first swap space and element next to space
        // in case of {3, 999, 2} make it {3, 2, 999}
        swap(arr[index], arr[index + 1]);
  
        // than swap space and greater element
        // convert {3, 2, 999} to {999, 2, 3}
        swap(arr[index - 1], arr[index + 1]);
    }
  
    else
        swap(arr[index], arr[index - 1]);
  
    sortRec(arr, index - 1, k - 1, n);
}
  
// Wrapper over sortRec.
void sort(int arr[], int n)
{
    // Find index of space (or 999)
    int index = -1;
    for (int i = 0; i < n; i++) {
        if (arr[i] == 999) {
            index = i;
            break;
        }
    }
  
    // Invalid input
    if (index == -1)
        return;
  
    sortRec(arr, index, n, n);
}
  
  
// driver program
int main()
{
    int arr[] = { 3, 2, 999, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, n);
    return 0;
}


Java
// Java program to sort an array by moving one 
// space around. 
  
class GFG
{
  
// n is total number of elements. 
// index is index of 999 or space. 
// k is number of elements yet to be sorted. 
static void sortRec(int arr[], int index, int k, int n) 
{ 
    // print the sorted array when loop reaches 
    // the base case 
    if (k == 0)
    { 
        for (int i = 1; i < n; i++) 
                        System.out.print(arr[i] + " "); 
                System.out.println(999); 
        return; 
    } 
  
    // else if k>0 and space is at 0th index swap 
    // each number with space and store index at 
    // second last index 
    else if (k > 0 && index == 0)
    { 
        index = n - 2; 
        for (int i = 1; i <= index; i++)
        { 
            arr[i - 1] = arr[i]; 
        } 
        arr[index] = 999; 
    } 
  
    // if space is neither start of array nor last 
    // element of array and element before it greater 
    // than/ the element next to space 
    if (index - 1 >= 0 && index + 1 < n && 
    arr[index - 1] > arr[index + 1])
    { 
  
        // first swap space and element next to space 
        // in case of {3, 999, 2} make it {3, 2, 999} 
        swap(arr,index, index + 1); 
  
        // than swap space and greater element 
        // convert {3, 2, 999} to {999, 2, 3} 
        swap(arr,index - 1, index + 1); 
    } 
  
    else
        swap(arr,index, index - 1); 
  
    sortRec(arr, index - 1, k - 1, n); 
} 
  
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
}
  
// Wrapper over sortRec. 
static void sort(int arr[], int n) 
{ 
    // Find index of space (or 999) 
    int index = -1; 
    for (int i = 0; i < n; i++)
    { 
        if (arr[i] == 999)
        { 
            index = i; 
            break; 
        } 
    } 
  
    // Invalid input 
    if (index == -1) 
        return; 
  
    sortRec(arr, index, n, n); 
} 
  
  
// Driver code 
public static void main(String[] args)
{
    int arr[] = { 3, 2, 999, 1 }; 
    int n = arr.length; 
    sort(arr, n); 
}
}
  
// This code contributed by Rajput-Ji


C#
// C# program to sort an array by moving one 
// space around. 
using System;
  
class GFG 
{ 
  
// n is total number of elements. 
// index is index of 999 or space. 
// k is number of elements yet to be sorted. 
static void sortRec(int []arr, int index, int k, int n) 
{ 
    // print the sorted array when loop reaches 
    // the base case 
    if (k == 0) 
    { 
        for (int i = 1; i < n; i++) 
                        Console.Write(arr[i] + " "); 
                Console.WriteLine(999); 
        return; 
    } 
  
    // else if k>0 and space is at 0th index swap 
    // each number with space and store index at 
    // second last index 
    else if (k > 0 && index == 0) 
    { 
        index = n - 2; 
        for (int i = 1; i <= index; i++) 
        { 
            arr[i - 1] = arr[i]; 
        } 
        arr[index] = 999; 
    } 
  
    // if space is neither start of array nor last 
    // element of array and element before it greater 
    // than/ the element next to space 
    if (index - 1 >= 0 && index + 1 < n && 
    arr[index - 1] > arr[index + 1]) 
    { 
  
        // first swap space and element next to space 
        // in case of {3, 999, 2} make it {3, 2, 999} 
        swap(arr,index, index + 1); 
  
        // than swap space and greater element 
        // convert {3, 2, 999} to {999, 2, 3} 
        swap(arr,index - 1, index + 1); 
    } 
  
    else
        swap(arr,index, index - 1); 
  
    sortRec(arr, index - 1, k - 1, n); 
} 
  
static int[] swap(int []arr, int i, int j) 
{ 
    int temp = arr[i]; 
    arr[i] = arr[j]; 
    arr[j] = temp; 
    return arr; 
} 
  
// Wrapper over sortRec. 
static void sort(int []arr, int n) 
{ 
    // Find index of space (or 999) 
    int index = -1; 
    for (int i = 0; i < n; i++) 
    { 
        if (arr[i] == 999) 
        { 
            index = i; 
            break; 
        } 
    } 
  
    // Invalid input 
    if (index == -1) 
        return; 
  
    sortRec(arr, index, n, n); 
} 
  
  
// Driver code 
public static void Main(String[] args) 
{ 
    int []arr = { 3, 2, 999, 1 }; 
    int n = arr.Length; 
    sort(arr, n); 
} 
} 
  
// This code has been contributed by 29AjayKumar



输出:

1 2 3 999

时间复杂度-O(n 2 )