VECTORS [STL :C++]
Want to sort an array of numbers but failing to choose a perfect sorting algorithm for the same looks cumbersome ? Want to optimize your solutions on sites like Codechef , HackerRank ?
Then my friend C++ has the answer for you stored in it's STL library.
Question :
Given the list of numbers, you are to sort them in non decreasing order.
Input
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]
Each line contains one integer: N [0 <= N <= 10^6]
Output
Output given numbers in non decreasing order.
Example
Input:
5 5 3 6 7 1
Output:
1 3 5 6 7
Answer :
#include <iostream>
#include <vector> //for vector
#include <algorithm> //for sort
#include <stdio.h>
using namespace std;
int main()
{
long int t,i,temp;
vector<long int> v; //Creating a Vector v
scanf("%ld",&t);
for(i=0;i<t;i++)
{
scanf("%ld",&temp);
v.push_back(temp); //adding the element at the end of the vector
}
sort(v.begin(),v.end()); //the function "sort" is defined in <algorithm.h>
//It takes parameters from the vector as to range which it has to sort
for(i=0;i<t;i++)
{
printf("%ld\n",v[i]);
}
return 0;
}