📌  相关文章
📜  在给定的索引范围内对数组进行排序

📅  最后修改于: 2021-04-22 02:20:42             🧑  作者: Mango

给定一个N个整数的数组arr []和一个索引范围[a,b] 。任务是在给定的索引范围内对数组进行排序,即,将数组中的元素从arr [a]arr [b]进行排序,同时保持其他元素的位置不变,并打印修改后的数组。
注意: ab之间没有关系,即a可以小于,等于或大于b 。同样, 0≤a,b

例子:

方法:

  1. 为数组的给定索引范围制作一个元素的临时数组。
  2. 对这个临时数组进行排序。
  3. 现在,使用给定索引范围的临时数组的这些排序元素来修改原始数组。

下面是上述方法的实现:

C++
// C++ program to sort the
// array in a given index range
 
#include 
using namespace std;
 
// Function to sort the elements of the array
// from index a to index b
void partSort(int arr[], int N, int a, int b)
{
    // Variables to store start and
    // end of the index range
    int l = min(a, b);
    int r = max(a, b);
 
    // Temporary array
    int temp[r - l + 1];
    int j = 0;
    for (int i = l; i <= r; i++) {
        temp[j] = arr[i];
        j++;
    }
 
    // Sort the temporary array
    sort(temp, temp + r - l + 1);
 
    // Modifying original array with
    // temporary array elements
    j = 0;
    for (int i = l; i <= r; i++) {
            arr[i] = temp[j];
            j++;
    }
 
    // Print the modified array
    for (int i = 0; i < N; i++) {
            cout << arr[i] << " " ;
        }
         
}
 
 
// Driver code
int main()
{
    int arr[] = { 7, 8, 4, 5, 2 } ;
    int a = 1 ;
    int b = 4;
 
    // length of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    partSort(arr, N, a, b);
    return 0;
}
// This code is contributed by Ryuga


Java
// Java program to sort the array in a given index range
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to sort the elements of the array
    // from index a to index b
    static void partSort(int[] arr, int N, int a, int b)
    {
        // Variables to store start and end of the index range
        int l = Math.min(a, b);
        int r = Math.max(a, b);
 
        // Temporary array
        int[] temp = new int[r - l + 1];
        int j = 0;
        for (int i = l; i <= r; i++) {
            temp[j] = arr[i];
            j++;
        }
 
        // Sort the temporary array
        Arrays.sort(temp);
 
        // Modifying original array with temporary array elements
        j = 0;
        for (int i = l; i <= r; i++) {
            arr[i] = temp[j];
            j++;
        }
 
        // Print the modified array
        for (int i = 0; i < N; i++) {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] arr = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
 
        // length of the array
        int N = arr.length;
 
        partSort(arr, N, a, b);
    }
}


Python3
# Python 3 program to sort the
# array in a given index range
 
# Function to sort the elements of
# the array from index a to index b
def partSort(arr, N, a, b):
     
    # Variables to store start and
    # end of the index range
    l = min(a, b)
    r = max(a, b)
 
    # Temporary array
    temp = [0 for i in range(r - l + 1)]
    j = 0
    for i in range(l, r + 1, 1):
        temp[j] = arr[i]
        j += 1
     
    # Sort the temporary array
    temp.sort(reverse = False)
 
    # Modifying original array with
    # temporary array elements
    j = 0
    for i in range(l, r + 1, 1):
            arr[i] = temp[j]
            j += 1
 
    # Print the modified array
    for i in range(0, N, 1):
            print(arr[i], end = " ")
 
# Driver code
if __name__ == '__main__':
    arr = [7, 8, 4, 5, 2]
    a = 1
    b = 4
 
    # length of the array
    N = len(arr)
 
    partSort(arr, N, a, b)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to sort the array in a given index range
using System;
class GFG {
 
    // Function to sort the elements of the array
    // from index a to index b
    static void partSort(int[] arr, int N, int a, int b)
    {
        // Variables to store start and end of the index range
        int l = Math.Min(a, b);
        int r = Math.Max(a, b);
 
        // Temporary array
        int[] temp = new int[r - l + 1];
        int j = 0;
        for (int i = l; i <= r; i++) {
            temp[j] = arr[i];
            j++;
        }
 
        // Sort the temporary array
        Array.Sort(temp);
 
        // Modifying original array with temporary array elements
        j = 0;
        for (int i = l; i <= r; i++) {
            arr[i] = temp[j];
            j++;
        }
 
        // Print the modified array
        for (int i = 0; i < N; i++) {
            Console.Write(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
 
        // length of the array
        int N = arr.Length;
 
        partSort(arr, N, a, b);
    }
}
// This code is contributed by anuj_67


PHP


C++
// C++ program to sort the array in a given index range
#include
using namespace std;
 
    // Function to sort the elements of the array
    // from index a to index b
    void partSort(int arr[], int N, int a, int b)
    {
        // Variables to store start and end
        // of the index range
        int l = min(a, b);
        int r = max(a, b);
         
        vector v(arr, arr + N);
 
        // Sort the subarray from arr[l] to
        // arr[r]
        sort(v.begin() + l, v.begin() + r + 1);
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            cout << v[i] << " ";
    }
 
    // Driver code
    int main()
    {
        int arr[] = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
        int N = sizeof(arr)/sizeof(arr[0]);
        partSort(arr, N, a, b);
    }
     
// This code is contributed by
// Sanjit_Prasad


Java
// Java program to sort the array in a given index range
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to sort the elements of the array
    // from index a to index b
    static void partSort(int[] arr, int N, int a, int b)
    {
        // Variables to store start and end
        // of the index range
        int l = Math.min(a, b);
        int r = Math.max(a, b);
 
        // Sort the subarray from arr[l] to
        // arr[r]
        Arrays.sort(arr, l, r + 1);
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] arr = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
        int N = arr.length;
        partSort(arr, N, a, b);
    }
}


Python3
# Python3 program to sort the
# array in a given index range
 
# Function to sort the elements of
# the array from index a to index b
def partSort(arr, N, a, b):
     
    # Variables to store start and
    # end of the index range
    l = min(a, b)
    r = max(a, b)
 
    arr = (arr[0 : l] +
    sorted(arr[l : r + 1]) +
           arr[r : N])
 
    # Print the modified array
    for i in range(0, N, 1):
            print(arr[i], end = " ")
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 7, 8, 4, 5, 2 ]
    a = 1
    b = 4
 
    # Length of the array
    N = len(arr)
 
    partSort(arr, N, a, b)
 
# This code is contributed by grand_master


C#
// C# program to sort the array in a given index range
using System;
 
class GFG {
 
    // Function to sort the elements of the array
    // from index a to index b
    static void partSort(int[] arr, int N, int a, int b)
    {
        // Variables to store start and end
        // of the index range
        int l = Math.Min(a, b);
        int r = Math.Max(a, b);
 
        // Sort the subarray from arr[l] to
        // arr[r]
        Array.Sort(arr, l, r);
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            Console.Write(arr[i] + " ");
    }
 
    // Driver code
    static void Main()
    {
        int[] arr = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
        int N = arr.Length;
        partSort(arr, N, a, b);
    }
}
 
// This code is contributed by mits


输出:
7 2 4 5 8




以下是使用Arrays.sort()的直接解决方案

C++

// C++ program to sort the array in a given index range
#include
using namespace std;
 
    // Function to sort the elements of the array
    // from index a to index b
    void partSort(int arr[], int N, int a, int b)
    {
        // Variables to store start and end
        // of the index range
        int l = min(a, b);
        int r = max(a, b);
         
        vector v(arr, arr + N);
 
        // Sort the subarray from arr[l] to
        // arr[r]
        sort(v.begin() + l, v.begin() + r + 1);
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            cout << v[i] << " ";
    }
 
    // Driver code
    int main()
    {
        int arr[] = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
        int N = sizeof(arr)/sizeof(arr[0]);
        partSort(arr, N, a, b);
    }
     
// This code is contributed by
// Sanjit_Prasad

Java

// Java program to sort the array in a given index range
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to sort the elements of the array
    // from index a to index b
    static void partSort(int[] arr, int N, int a, int b)
    {
        // Variables to store start and end
        // of the index range
        int l = Math.min(a, b);
        int r = Math.max(a, b);
 
        // Sort the subarray from arr[l] to
        // arr[r]
        Arrays.sort(arr, l, r + 1);
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] arr = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
        int N = arr.length;
        partSort(arr, N, a, b);
    }
}

Python3

# Python3 program to sort the
# array in a given index range
 
# Function to sort the elements of
# the array from index a to index b
def partSort(arr, N, a, b):
     
    # Variables to store start and
    # end of the index range
    l = min(a, b)
    r = max(a, b)
 
    arr = (arr[0 : l] +
    sorted(arr[l : r + 1]) +
           arr[r : N])
 
    # Print the modified array
    for i in range(0, N, 1):
            print(arr[i], end = " ")
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 7, 8, 4, 5, 2 ]
    a = 1
    b = 4
 
    # Length of the array
    N = len(arr)
 
    partSort(arr, N, a, b)
 
# This code is contributed by grand_master

C#

// C# program to sort the array in a given index range
using System;
 
class GFG {
 
    // Function to sort the elements of the array
    // from index a to index b
    static void partSort(int[] arr, int N, int a, int b)
    {
        // Variables to store start and end
        // of the index range
        int l = Math.Min(a, b);
        int r = Math.Max(a, b);
 
        // Sort the subarray from arr[l] to
        // arr[r]
        Array.Sort(arr, l, r);
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            Console.Write(arr[i] + " ");
    }
 
    // Driver code
    static void Main()
    {
        int[] arr = { 7, 8, 4, 5, 2 };
        int a = 1, b = 4;
        int N = arr.Length;
        partSort(arr, N, a, b);
    }
}
 
// This code is contributed by mits
输出:
7 2 4 5 8