Cod sursa(job #1896888)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 28 februarie 2017 23:01:10
Problema Sortare prin comparare Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 2.9 kb
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package merge.sort;

import java.io.*;
import java.util.*;

class MergeSort {

    static int v[];
    static int n = -1;
    
    public static void interclasare(int p, int m, int q){
        int r[] = new int[n];
        int i = p, j = m+1, k = -1;
        while(i <= m && j <= q)
            if(v[i] < v[j])
                r[++k] = v[i++];
            else
                r[++k] = v[j++];
        while(i <= m)
            r[++k] = v[i++];
        while(j <= q)
            r[++k] = v[j++];
        for (int w = p; w <= q; ++w)
            v[w] = r[w-p];
    }
    
    public static void mergeSort(int p, int q){
        if(q > p){
            int m = (p+q)/2;
            mergeSort(p, m);
            mergeSort(m+1, q);
            interclasare(p, m, q);
        }
    }
    
    public static void main(String[] args) {
        
        try{
            Reader sc = new Reader (new FileInputStream("algsort.in"));
            //File fin = new File("algsort.in");
            //Scanner sc = new Scanner(fin);
            n = sc.nextInt();
            v = new int[n];
            for (int i = 0; i < n; ++i)
                v[i] = sc.nextInt();
        }catch(Exception e){
            System.out.println("yea boi, ya fuckd");
        }
        
        mergeSort(0, n-1);
        
        try{
            BufferedWriter fout = new BufferedWriter(new FileWriter("algsort.out"));
            for (int i = 0; i < n; ++i)
                fout.write(v[i] + " ");
            fout.write("\n");
            fout.close();
        }catch(Exception e){
            System.out.println("I am out of witty texts");
        }
        
    }
    
    private static class Reader {
        private BufferedInputStream stream;
        private byte[] buffer;
        int buffSize;
        private int buffPointer;
 
        protected final int BUFF_CAPACITY = 5000;
 
        protected Reader(FileInputStream file) {
            stream = new BufferedInputStream(file);
            buffer = new byte[BUFF_CAPACITY];
            buffPointer = buffSize = 0;
        }
 
        private void fillBuffer() throws IOException {
            buffSize = stream.read(buffer, 0, BUFF_CAPACITY);
            buffPointer = 0;
        }
 
        private byte readByte() throws IOException {
            if (buffPointer >= buffSize) {
                fillBuffer();
            }
            return buffer[buffPointer++];
        }
 
        protected int nextInt() throws IOException {
            byte b = readByte();
            while (b < '0' || b > '9') {
                b = readByte();
            }
 
            int num = 0;
            while (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
                b = readByte();
            }
            return num;
        }
    }
}