Pagini recente » Cod sursa (job #477111) | Cod sursa (job #1165274) | Cod sursa (job #3225220) | Cod sursa (job #1882573) | Cod sursa (job #1988914)
package Sorting;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
public class Main
{
public static int[] t;
public static void merge(int[] t, int st, int dr, int m)
{
int current1 = st;
int current2 = m + 1;
int posToInsert = 0;
int[] arr = new int[dr - st + 1];
while (current1 <= m && current2 <= dr) {
if (t[current1] < t[current2]) {
arr[posToInsert] = t[current1]; current1++;
} else {
arr[posToInsert] = t[current2]; current2++;
}
posToInsert++;
}
while (current1 <= m) { arr[posToInsert] = t[current1]; current1++; posToInsert++; }
while (current2 <= dr) { arr[posToInsert] = t[current2]; current2++; posToInsert++; }
for (int i = st; i <= dr; i++) t[i] = arr[i - st];
}
public static void mergeSort(int[] t, int st, int dr)
{
if (st >= dr) return;
int m = (st + dr) / 2;
mergeSort(t, st, m);
mergeSort(t, m + 1, dr);
merge(t, st, dr, m);
}
public static void main(String[] args)
{
try
{
InputReader in = new InputReader(new FileInputStream("algsort.in"));
PrintWriter out = new PrintWriter(new FileOutputStream("algsort.out"));
int n = in.nextInt();
t = new int[n];
for (int i = 0; i < n; i++) t[i] = in.nextInt();
mergeSort(t, 0, t.length - 1);
String ans = "";
for (int i = 0; i < n; i++)
if (i < n - 1) ans = ans + t[i] + " "; else
ans = ans + t[i];
out.write(ans);
out.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[2048];
private int curChar;
private int numChars;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek()
{
if (numChars == -1)
return -1;
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x)
{
while (x-- > 0)
read();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public String nextString()
{
return next();
}
public String next()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1)
{
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean hasNext()
{
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}