Algorithm
Problem link- https://www.spoj.com/problems/TSORT/
TSORT - Turbo Sort
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]
Output
Output given numbers in non decreasing order.
Example
Input:
5 5 3 6 7 1
Output:
1 3 5 6 7
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
N = int(input())
count = [0] * 1000001
for i in range(N):
count[int(input())] += 1
for i in range(1000001):
for j in range(count[i]):
print(i)
Copy The Code &
Try With Live Editor
Input
5
3
6
7
1
Output
3
5
6
7
#2 Code Example with Java Programming
Code -
Java Programming
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
for(int i=0;i<n;i++)
System.out.println(a[i]);
}
}
Copy The Code &
Try With Live Editor
Input
5
3
6
7
1
Output
3
5
6
7
#3 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t ;
cin >> t;
vector<int>v;
while(t--){
int n;
cin >> n;
v.push_back(n);
}
sort(v.begin(),v.end());
for(int x=0;x<v.size();x++)
cout << v[x] << '\n';
}
Copy The Code &
Try With Live Editor
Input
5
3
6
7
1
Output
3
5
6
7
Demonstration
SPOJ Solution-Turbo Sort-Solution in C, C++, Java, Python