📌  相关文章
📜  按原始顺序打印给定数组中的 n 个最小元素

📅  最后修改于: 2021-10-27 07:15:32             🧑  作者: Mango

给定一个 m 个元素的数组,我们需要从数组中找到 n 个最小的元素,但它们的顺序必须与给定数组中的顺序相同。
例子:

Input : arr[] = {4, 2, 6, 1, 5}, 
        n = 3
Output : 4 2 1
Explanation : 
1, 2 and 4 are 3 smallest numbers and
4 2 1 is their order in given array.

Input : arr[] = {4, 12, 16, 21, 25},
        n = 3
Output : 4 12 16
Explanation : 
4, 12 and 16 are 3 smallest numbers and 
4 12 16 is their order in given array.

制作原始数组的副本,然后对副本数组进行排序。对复制数组进行排序后,保存所有 n 个最小的数字。进一步对于原始数组中的每个元素,如果它出现在 n-smallest 数组中,检查它是否在 n-smallest number 中,然后打印它,否则向前移动。

以下是上述方法的实现:

C++
// CPP for printing smallest n number in order
#include 
using namespace std;
 
// Function to print smallest n numbers
void printSmall(int arr[], int asize, int n)
{
    // Make copy of array
    vector copy_arr(arr, arr + asize);
 
    // Sort copy array
    sort(copy_arr.begin(), copy_arr.begin() + asize);
 
    // For each arr[i] find whether
    // it is a part of n-smallest
    // with binary search
    for (int i = 0; i < asize; ++i)
        if (binary_search(copy_arr.begin(),
                copy_arr.begin() + n, arr[i]))
            cout << arr[i] << " ";
}
 
// Driver program
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int asize = sizeof(arr) / sizeof(arr[0]);   
    int n = 5;
    printSmall(arr, asize, n);
    return 0;
}


Java
// Java for printing smallest n number in order
import java.util.*;
 
class GFG
{
 
 
// Function to print smallest n numbers
static void printSmall(int arr[], int asize, int n)
{
    // Make copy of array
    int []copy_arr = Arrays.copyOf(arr,asize);
 
    // Sort copy array
    Arrays.sort(copy_arr);
 
    // For each arr[i] find whether
    // it is a part of n-smallest
    // with binary search
    for (int i = 0; i < asize; ++i)
    {
        if (Arrays.binarySearch(copy_arr,0,n, arr[i])>-1)
            System.out.print(arr[i] + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int asize = arr.length;
    int n = 5;
    printSmall(arr, asize, n);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 for printing smallest n number in order
 
# Function for binary_search
def binary_search(arr, low, high, ele):
    while low < high:
        mid = (low + high) // 2
        if arr[mid] == ele:
            return mid
        elif arr[mid] > ele:
            high = mid
        else:
            low = mid + 1
    return -1
 
# Function to print smallest n numbers
def printSmall(arr, asize, n):
 
    # Make copy of array
    copy_arr = arr.copy()
 
    # Sort copy array
    copy_arr.sort()
 
    # For each arr[i] find whether
    # it is a part of n-smallest
    # with binary search
    for i in range(asize):
        if binary_search(copy_arr, low = 0,
                         high = n, ele = arr[i]) > -1:
            print(arr[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
    arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0]
    asize = len(arr)
    n = 5
    printSmall(arr, asize, n)
 
# This code is conributed by
# sanjeev2552


C#
// C# for printing smallest n number in order
using System;    
 
class GFG
{
 
 
// Function to print smallest n numbers
static void printSmall(int []arr, int asize, int n)
{
    // Make copy of array
    int []copy_arr = new int[asize];
    Array.Copy(arr, copy_arr, asize);
 
    // Sort copy array
    Array.Sort(copy_arr);
 
    // For each arr[i] find whether
    // it is a part of n-smallest
    // with binary search
    for (int i = 0; i < asize; ++i)
    {
        if (Array.BinarySearch(copy_arr, 0, n, arr[i])>-1)
            Console.Write(arr[i] + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int asize = arr.Length;
    int n = 5;
    printSmall(arr, asize, n);
}
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出 :

1 3 4 2 0 

时间复杂度: O(n * log(n))

辅助空间: O(n)
为了复制数组,我们需要 O(n) 的空间复杂度,然后为了排序,我们需要 O(n log n) 阶的复杂度。此外,对于 arr[] 中的每个元素,我们正在 copy_arr[] 中执行搜索,这将导致线性搜索的 O(n) 但我们可以通过应用二分搜索来改进它,因此我们的整体时间复杂度将为O(n log n) .

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程