📜  删除元素以使数组排序

📅  最后修改于: 2021-04-24 18:00:51             🧑  作者: Mango

给定一个整数数组,任务是从数组中删除元素以使数组排序。即,删除不按升序排列的元素。

例子:

方法:遍历给定的数组,对于大于或等于先前获取的元素的每个元素,将此元素添加到另一个数组,否则跳到下一个元素。最后,将根据斯大林的排序对新创建的数组进行排序。

  1. 对于每个元素,请检查该元素是否大于前一个元素。
    • 如果是,则检查下一个元素。
    • 否则删除该元素。
  2. 遍历所有元素后,我们将得到一个排序数组。

下面是上述方法的实现:

C++
// C++ implementation of the approach
  
#include 
using namespace std;
  
// Function to sort the array
// by removing misplaced elements
void removeElements(int arr[], int n)
{
  
    // brr[] is used to store
    // the sorted array elements
    int brr[n], l = 1;
  
    brr[0] = arr[0];
    for (int i = 1; i < n; i++) {
        if (brr[l - 1] <= arr[i]) {
            brr[l] = arr[i];
            l++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < l; i++)
        cout << brr[i] << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 10, 12, 9, 10, 2, 13, 14 };
    int n = sizeof(arr) / sizeof(arr[0]);
    removeElements(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG
{
      
// Function to sort the array
// by removing misplaced elements
static void removeElements(int []arr, int n)
{
  
    // brr[] is used to store
    // the sorted array elements
    int []brr = new int[n];
    int l = 1;
  
    brr[0] = arr[0];
    for (int i = 1; i < n; i++)
    {
        if (brr[l - 1] <= arr[i]) 
        {
            brr[l] = arr[i];
            l++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < l; i++)
        System.out.print(brr[i] + " ");
}
  
// Driver code
static public void main (String[] args)
{
    int []arr = { 10, 12, 9, 10, 2, 13, 14 };
    int n = arr.length;
    removeElements(arr, n);
}
}
  
// This code is contributed by Code_Mech.


Python3
# Python3 implementation of the approach 
  
# Function to sort the array 
# by removing misplaced elements 
def removeElements(arr,  n) : 
  
    # brr[] is used to store 
    # the sorted array elements 
    brr = [0]*n; l = 1; 
  
    brr[0] = arr[0]; 
    for i in range(1,n) :
        if (brr[l - 1] <= arr[i]) :
            brr[l] = arr[i]; 
            l += 1; 
  
    # Print the sorted array 
    for i in range(l) :
        print(brr[i],end=" "); 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 10, 12, 9, 10, 2, 13, 14 ]; 
    n = len(arr); 
    removeElements(arr, n); 
  
    # This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to sort the array
// by removing misplaced elements
static void removeElements(int []arr, int n)
{
  
    // brr[] is used to store
    // the sorted array elements
    int []brr = new int[n];
    int l = 1;
  
    brr[0] = arr[0];
    for (int i = 1; i < n; i++)
    {
        if (brr[l - 1] <= arr[i]) 
        {
            brr[l] = arr[i];
            l++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < l; i++)
        Console.Write(brr[i] + " ");
}
  
// Driver code
static public void Main ()
{
    int []arr = { 10, 12, 9, 10, 2, 13, 14 };
    int n = arr.Length;
    removeElements(arr, n);
}
}
  
// This code is contributed by jit_t


输出:
10 12 13 14

方法2:而不是使用额外的数组,将它们存储在同一数组中

// C++ implementation of the approach
  
#include 
using namespace std;
  
// Function to sort the array
// by removing misplaced elements
void removeElements(int arr[], int n)
{
  
    // l stores the index
    int l = 1;
    for (int i = 1; i < n; i++) {
        if (arr[l - 1] <= arr[i]) {
            arr[l] = arr[i];
            l++;
        }
    }
  
    // Print the sorted array
    for (int i = 0; i < l; i++)
        cout << arr[i] << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 10, 12, 9, 10, 2, 13, 14 };
    int n = sizeof(arr) / sizeof(arr[0]);
    removeElements(arr, n);
  
    return 0;
}
输出:
10 12 13 14