- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
Latest commit
61 lines (53 loc) · 1.43 KB
/
Copy pathSort.java
File metadata and controls
61 lines (53 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.StringTokenizer;
/**
* @Baekjun 1083
* @author MoonDooo
*/
publicclassSort {
publicstaticvoidmain(String[] args) {
try {
newSortArray();
}
catch(IOExceptione) {
e.printStackTrace();
}
}
}
classSortArray{
privateint[] array;
privateintarraySize;
privateintarraySortCounter;
publicSortArray() throwsIOException{
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
arraySize = Integer.parseInt(br.readLine());
StringTokenizerst = newStringTokenizer(br.readLine());
array = newint[arraySize];
for(inti = 0; i<arraySize; i++) {
array[i] = Integer.parseInt(st.nextToken());
}
arraySortCounter = Integer.parseInt(br.readLine());
for(inti = 0; arraySortCounter>0&& i<arraySize; i++) {
intmaxIndexInPossibleRange = i;
for(intj = i; j<=i+arraySortCounter&&j<arraySize; j++) {
if(array[maxIndexInPossibleRange]<array[j]) {
maxIndexInPossibleRange = j;
}
}
for(ints = maxIndexInPossibleRange; s > i; s--) {
swap(s-1, s);
}
}
for(inti = 0; i<arraySize; i++) {
System.out.print(array[i]+ " ");
}
}
publicvoidswap(intArraySmallerElement, intArrayBiggerElement) {
arraySortCounter--;
inttmp = array[ArraySmallerElement];
array[ArraySmallerElement] = array[ArrayBiggerElement];
array[ArrayBiggerElement] = tmp;
}
}