Skip to content

Instantly share code, notes, and snippets.

@slava-konashkov
Created February 17, 2014 18:49
Show Gist options
  • Select an option

  • Save slava-konashkov/9056569 to your computer and use it in GitHub Desktop.

Select an option

Save slava-konashkov/9056569 to your computer and use it in GitHub Desktop.
Разбить массив пополам и отсортировать каждую половинку
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class ArraySplit {
public static void main(String[] args) throws Exception {
int[] iaMain = null;
int[] iaTmp;
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
String sIn;
int iTmp;
while (true) {
try {
System.out.println("Введите число:");
sIn = d.readLine();
iTmp = Integer.parseInt(sIn);
if (null == iaMain) {
iaMain = new int[]{iTmp};
} else {
iaTmp = new int[iaMain.length+1];
System.arraycopy(iaMain, 0, iaTmp, 0, iaMain.length);
iaTmp[iaMain.length] = iTmp;
iaMain = iaTmp;
}
} catch (Exception e) {
System.out.println("Ввод закончен.");
break;
}
}
try {
int[] a1 = Arrays.copyOfRange(iaMain, 0, iaMain.length >> 1);
int[] a2 = Arrays.copyOfRange(iaMain, iaMain.length >> 1, iaMain.length);
Arrays.sort(a1);
Arrays.sort(a2);
System.out.println(Arrays.toString(a1));
System.out.println(Arrays.toString(a2));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment