
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print n smallest elements from given array in their original order
Given with array of let’s say k elements the program must find the n smallest elements amongst them in their appearing order.
Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Ouput : 1, 2, 3 Input k is 3 it means 3 shortest elements among the set needs to be displayed in original order like 1 than 2 and than 3
Algorithm
START Step 1 -> start variables as int i, max, pos, j, k=4 and size for array size Step 2 -> Loop For i=k and i<size and i++ Set max = arr[k-1] pos = k-1 Loop For j=k-2 and j>=0 and j-- If arr[j]>max Set max = arr[j] Set pos = j End End IF max> arr[i] Set j = pos Loop While j < k-1 Set arr[j] = arr[j+1] Set j++ End Set arr[k-1] = arr[i] End IF End Step 3 -> Loop For i = 0 and i < k and i++ Print arr[i] STOP
Example
#include <stdio.h> int main() { int arr[] = {5,8,3,1,2,9}; int i, max, pos, j, k=4; int size = sizeof(arr)/sizeof(arr[0]); //Using insertion sort, Starting from k. for(i=k;i<size;i++){ max = arr[k-1]; pos = k-1; for(j=k-2;j>=0;j--) { if(arr[j]>max) { max = arr[j]; pos = j; } } if ( max> arr[i] ) { j = pos; while( j < k-1 ) { arr[j] = arr[j+1]; j++; } arr[k-1] = arr[i]; } } //Printing first k elements for (i = 0; i < k; i++) { printf("%d ", arr[i]); } return 0; }
Output
if we run above program then it will generate following output.
5 3 1 2
- Related Articles
- Retrieving n smallest numbers from an array in their original order in JavaScript
- Find k maximum elements of array in original order in C++
- JavaScript Program to Find k maximum elements of array in original order
- How to print the elements in a reverse order from an array in C?
- Print array elements in alternatively increasing and decreasing order in C++
- Print prime numbers from 1 to N in reverse order
- Print all possible combinations of r elements in a given array of size n in C++
- Python program to print the elements of an array in reverse order
- Find original array from encrypted array (An array of sums of other elements) using C++.
- Print Even Positions Elements from an Array in Java
- Return the sum of two consecutive elements from the original array in JavaScript
- Print the last occurrence of elements in array in relative order in C Program.
- Print All Distinct Elements of a given integer array in C++
- Print numbers in descending order along with their frequencies
- Maximum sum of increasing order elements from n arrays in C++

Advertisements