Tuesday, December 3, 2013

my c++ solution to codechef "Turbo Sort" - TSORT problem

codechef "Turbo Sort" - TSORT problem: http://www.codechef.com/problems/TSORT/

Turbo Sort


All submissions for this problem are available.

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

Author:admin
Tagsadmin
Date Added:1-12-2008
Time Limit:5 sec
Source Limit:50000 Bytes
Languages:ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.8.1, CPP11, CS2, D, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC













and here is my c++ solution to codechef "Turbo Sort" - TSORT problem: http://ideone.com/HyUN3W

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int a, arr[1000001]={0}, b;
    scanf("%d", &a);
    while(a--) {
        scanf("%d", &b);
        arr[b]++;
    }
    for(int i=0; i<1000001; i++) {
        while(arr[i]>0) {
            printf("%d\n", i);
            arr[i]--;
        }
    }
    return 0;
}


and here is my solution VERSION 2: http://ideone.com/drtodU
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int lim=1e6+5;
int a[lim];

int main() {
    int t;
    scanf("%d", &t);
    for(int i=0; i<t; i++) scanf("%d", &a[i]);
    sort(a, a+t);
    for(int i=0; i<t; i++) printf("%d\n", a[i]);
    return 0;
}


NOTE: "scanf" & "printf" stuff is more quick than "cin" & "cout" in processing (or, at least, it seems so to me). the problem asks you to sort the given numbers in increasing order - "Given the list of numbers, you are to sort them in non decreasing order." means that. 

6 comments:

  1. Hi could u please explain how this sorting is being done? I tried going through the code line by line but I don't get it. Acc to my understanding, for an input of 5 length array {5, 3 , 6, 7, 1} it should be printing 000000 1111 2222222 and so on coz the value i is being printed value of the array[i]+1 times. Please explain.

    ReplyDelete
    Replies
    1. nope. lemme explain my solution version 1, scanf() scans all inputs and it just increments corresponding index of array:
      scanf("%d", &b);
      arr[b]++;
      dont forget that all values of array are 0(zero) at the beginning. for 5 input 5, 3, 6, 7, 1 array updates those indices: arr[5] had value 0, and now it is incremented to 1, arr[3] value was 0, and now it is 1, and so on. and after scanning all inputs we go through 0 to 1000000 and if we find any value greater than zero we print it out. if you dont understand read my solution verison 2.

      Delete
  2. solution 1 did the work on codechef, but it is not running in my own computer(i am using codeblocks). It just stops as i run it. Why is it so?

    ReplyDelete