2016-03-15 39 views
3

給定數字列表,您將按照非遞減的 順序對它們進行排序。輸入CodeChef TurboSort(使用int對整數進行排序)

t - 列表中的數字的數量,然後t行遵循[t < = 10^6]。 每行包含一個整數:N [0 < = N < = 10^6]輸出

以非遞減順序輸出給定的數字。實施例

輸入:5 5 3 6 7 1輸出:1 3 5 6 7

使用文字INT值,並使用該排序使用快速排序的文字的Arrays.sort()函數首先執行ALGO(最壞的情況下N^2,平均情況 - nlogn)

import java.io.*; 
import java.util.Arrays; 
import java.util.StringTokenizer; 

public class Main { 

    public static void main(String[] args) { 

    InputStream inputStream = System.in; 
    OutputStream outputStream = System.out; 
    InputReader in = new InputReader(inputStream); 
    PrintWriter out = new PrintWriter(outputStream); 

    int num = in.nextInt(); 

    int[] n = new int[num]; 

    for (int i = 0; i < n.length; i++) { 

     n[i] = in.nextInt(); 

    } 

    Arrays.sort(n); 


    for (int i = 0; i < n.length; i++) out.println(n[i]); 


    out.close(); 

    } 
} 


class InputReader { 
    private BufferedReader reader; 
    private StringTokenizer tokenizer; 

    public InputReader(InputStream stream) { 
    reader = new BufferedReader(new InputStreamReader(stream)); 
    tokenizer = null; 
    } 

    public String next() { 
    while (tokenizer == null || !tokenizer.hasMoreTokens()) { 
     try { 
     tokenizer = new StringTokenizer(reader.readLine()); 
     } catch (IOException e) { 
     throw new RuntimeException(e); 
     } 
    } 
    return tokenizer.nextToken(); 
    } 

    public int nextInt() { 
    return Integer.parseInt(next()); 
    } 

} 

下實現存儲和排序中的int文字作爲整型對象,並使用Arrays.sort()甲基OD,現在排序使用歸併ALGO,保證nlogn性能

import java.io.InputStreamReader; 
import java.io.IOException; 
import java.io.BufferedReader; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.math.BigInteger; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.StringTokenizer; 
import java.io.InputStream; 

/* Name of the class has to be "Main" only if the class is public. */ 
class Codechef { 
    public static void main(String[] args) { 
    InputStream inputStream = System.in; 
    OutputStream outputStream = System.out; 
    InputReader in = new InputReader(inputStream); 
    PrintWriter out = new PrintWriter(outputStream); 
    int T = in.nextInt(); 

    Integer[] ARR = new Integer[T]; 

    for (int i = 0; i < T; i++) ARR[i] = in.nextInt(); 

    Arrays.sort(ARR); 

    for (int i : ARR) out.println(i); 

    out.close(); 
    } 

} 

class InputReader { 
    private BufferedReader reader; 
    private StringTokenizer tokenizer; 

    public InputReader(InputStream stream) { 
    reader = new BufferedReader(new InputStreamReader(stream)); 
    tokenizer = null; 
    } 

    public String next() { 
    while (tokenizer == null || !tokenizer.hasMoreTokens()) { 
     try { 
     tokenizer = new StringTokenizer(reader.readLine()); 
     } catch (IOException e) { 
     throw new RuntimeException(e); 
     } 
    } 
    return tokenizer.nextToken(); 
    } 

    public int nextInt() { 
    return Integer.parseInt(next()); 
    } 

} 

然而現在的問題是,按照邏輯,歸併ALGO(即,整數對象排序實現)應該採取小於或等於時間的整數對象與IE瀏覽器中的int文字排序實現對於快速排序算法中)正在採取較小的時間...

Integer對象分類實施 - 0.94sec INT文字排序執行 - 0.53sec

我錯過了什麼嗎? 這個多餘的時間是什麼原因? 是不是因爲自動裝箱和autounboxing的?!是這個剩餘時間的原因...

+1

順便說一句,你知道你的元素是整數且<10^6,你可以用線性複雜度對其進行排序。 –

+0

可能是因爲你沒有測量你認爲你正在測量的東西。您可能正在測量HotSpot編譯時間,JVM啓動時間,磁盤吞吐量等。請先閱讀本文,然後發佈新的基準測試結果:http://stackoverflow.com/questions/504103/how-do-i-write -a-correct-micro-benchmark-in-java –

回答

0

我想你謝謝你提醒我,我有一個很久以前就被遺忘的codechef賬戶。這裏是我當時所做的解決方案,它花了我20秒來運行代碼有點大希望你發現這很有感謝..

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.PrintStream; 
import java.lang.reflect.Field; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

class Reader 
{ 
    private static final int BUFSIZE = 0x10000; 
    private final byte[]  buffer = new byte[BUFSIZE]; 
    private final ByteBuffer bb  = ByteBuffer.wrap(buffer); 
    private final FileChannel channel; 

    int      bufSize = -1;      // non empty buffer 
    int      bufOffset = 0;      // non valid buffer 

    private FileInputStream getFileInputStream(InputStream in) 
    { 
     try 
     { 
      if (in instanceof BufferedInputStream) 
      { 
       Field field = in.getClass().getSuperclass().getDeclaredField("in"); 
       field.setAccessible(true); 
       return (FileInputStream) field.get(in); 
      } 
     } 
     catch (Throwable e) 
     { 
      e.printStackTrace(); 
     } 
     return (FileInputStream) in; 
    } 

    Reader(InputStream in) throws IOException 
    { 
     this.channel = this.getFileInputStream(in).getChannel(); 
    } 

    void fetchBuffer() throws IOException 
    { 
     bb.clear(); 
     bufSize = channel.read(bb); 
     bufOffset = 0; 
    } 

    boolean isFinished() 
    { 
     return bufSize <= 0; 
    } 

    private int peek() throws IOException 
    { 
     if (bufOffset < bufSize) 
      return buffer[bufOffset]; 
     fetchBuffer(); 
     if (bufSize > 0) 
      return buffer[0]; 
     return -1; 
    } 

    private void skipSpace() throws IOException 
    { 
     int v = peek(); 
     while (v <= ' ' && v != -1) 
     { 
      bufOffset++; 
      v = peek(); 
     } 
    } 

    void nextLine() throws IOException 
    { 
     int v = peek(); 
     while (v != -1 && v != '\n' && v != '\r') 
     { 
      bufOffset++; 
      v = peek(); 
     } 
     if (v == '\r') 
     { 
      bufOffset++; 
      v = peek(); 
      if (v == '\n') 
       bufOffset++; 
     } 
     else if (v == '\n') 
     { 
      bufOffset++; 
      v = peek(); 
      if (v == '\r') 
       bufOffset++; 
     } 
    } 

    int readInt() throws IOException 
    { 
     skipSpace(); 
     int result = 0; 
     int v = peek(); 
     while (v > ' ') 
     { 
      result = result * 10 + v - '0'; 
      bufOffset++; 
      v = peek(); 
     } 
     return result; 
    } 

} 

class Writer 
{ 
    private static final int  BUFSIZE = 0x10000; 
    private final FileOutputStream fos; 
    private final byte[]   buffer = new byte[BUFSIZE]; 
    private int     offset = 0; 

    private FileOutputStream getFileOutputStream(PrintStream out) 
    { 
     try 
     { 
      Field field = out.getClass().getSuperclass().getDeclaredField("out"); 
      field.setAccessible(true); 
      OutputStream os = (OutputStream) field.get(out); 
      if (os instanceof BufferedOutputStream) 
      { 
       BufferedOutputStream bos = (BufferedOutputStream) os; 
       field = bos.getClass().getSuperclass().getDeclaredField("out"); 
       field.setAccessible(true); 
       return (FileOutputStream) field.get(bos); 
      } 
      return (FileOutputStream) field.get(out); 
     } 
     catch (Throwable e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    Writer(PrintStream out) throws IOException 
    { 
     fos = getFileOutputStream(out); 
    } 

    private static final int[] boundaries = new int[] 
    { 
     9, 99, 999, 9999, 99999, 999999, 9999999, 
     99999999, 999999999 
    }; 
    private static final int[] divs  = new int[] 
    { 
     1, 10, 100, 1000, 10000, 100000, 1000000, 
     10000000, 100000000 
    }; 
    private static final byte[] numbers = "".getBytes(); 

    void writeln(int number) throws IOException 
    { 
     if (offset > BUFSIZE - 100) 
      flush(); 
     int index; 
     for (index = 0; index < boundaries.length; index++) 
      if (number <= boundaries[index]) 
       break; 
     for (; index >= 0; index--) 
     { 
      int mult = number/divs[index]; 
      buffer[offset++] = numbers[mult]; 
      number -= mult * divs[index]; 
     } 
     buffer[offset++] = '\n'; 
    } 

    void flush() throws IOException 
    { 
     if (offset > 0) 
     { 
      fos.write(buffer, 0, offset); 
      offset = 0; 
     } 
    } 
} 



class Solution { 

    public static void main(String[] args) throws java.lang.Exception { 
     Reader r=new Reader(System.in); 
     Writer w=new Writer(System.out); 

     int x,k; 
     int[] arr2 = new int[1000000]; 
     x = r.readInt(); 
     for (int i = 0; i < x; i++) { 
      arr2[r.readInt()]++; 
     } 
     for (int i = 0; i < 1000000; i++) { 

       k= arr2[i]; 
       while(k-- > 0){ 
        w.writeln(i); 
       } 


     } 
     w.flush(); 
    } 
} 
+0

對不起.21秒運行.. – SmashCode

+0

整數是對象,它通常需要很長時間來處理。 – SmashCode

+0

哇......這是抽象的,因爲難以理解......(我是一個新手)所以,如果你能解釋這裏發生的事情,那將是很好的一種情況.....否則我只會訴諸隨機谷歌搜索一如既往,並希望我瞭解此代碼..謝謝! – Vonn

1

對於初學者來說,都歸併排序和快速排序在實踐中也有類似表現。事實上,快速排序通常比隨機數據稍微好一些。但即使合併排序略好一些,排序整數總是會明顯變慢,因爲排序對象比基元更難。它們不適用於緩存以及原語。

1

排序需要更長的時間,主要是因爲使用整數,您正在存儲一個對象,這是一個很大的開銷。

相關問題